when get pagecontent from URL the connect alway return nopermistion?
I have a methor to return pagecontent of link
but when it run, alway return "Do not perrmisson ", plesea check it
here is code to return string pagecontent
public static String getPageContent(String targetURL) throws Exception {
Hashtable contentHash = new Hashtable();
URL url;
URLConnection conn;
// The data streams used to read from and write to the URL connection.
DataOutputStr开发者_StackOverflow中文版eam out;
DataInputStream in;
// String returned as the result .
String returnString = "";
// Create the URL object and make a connection to it.
url = new URL(targetURL);
conn = url.openConnection();
// check out permission of acess URL
if (conn.getPermission() != null) {
returnString = "Do not Permission access URL ";
} else {
// Set connection parameters. We need to perform input and output,
// so set both as true.
conn.setDoInput(true);
conn.setDoOutput(true);
// Disable use of caches.
conn.setUseCaches(false);
// Set the content type we are POSTing. We impersonate it as
// encoded form data
conn.setRequestProperty("Content-Type",
"application/x-www-form-urlencoded");
// get the output stream .
out = new DataOutputStream(conn.getOutputStream());
String content = "";
// Create a single String value pairs for all the keys
// in the Hashtable passed to us.
Enumeration e = contentHash.keys();
boolean first = true;
while (e.hasMoreElements()) {
// For each key and value pair in the hashtable
Object key = e.nextElement();
Object value = contentHash.get(key);
// If this is not the first key-value pair in the hashtable,
// concantenate an "&" sign to the constructed String
if (!first)
content += "&";
// append to a single string. Encode the value portion
content += (String) key + "="
+ URLEncoder.encode((String) value);
first = false;
}
// Write out the bytes of the content string to the stream.
out.writeBytes(content);
out.flush();
out.close();
// check if can't read from URL
// Read input from the input stream.
in = new DataInputStream(conn.getInputStream());
String str;
while (null != ((str = in.readLine()))) {
returnString += str + "\n";
}
in.close();
}
// return the string that was read.
return returnString;
}
Are you sure you interpret the meaning of getPermission()
correctly? The javadoc on getPermission()
[1] states:
Returns:
the permission object representing the permission necessary to make the connection represented by this URLConnection.
Means: Whether a Permission
object is returned or not just tells you whether your JVM may or may not need any action granted in the JVMs security policy to connect to the URL specified. You never actually check the permission yourself in your code example above.
Usually you don't have to do this, as most runtime classes do this on their own anyway internally. The SecurityManager
involved will throw an appropriate exception if the actual permisson is not granted.
My suggestion is to just open the connection and start work, without testing for the getPermission()
, as you'll know it anyway when the permission is not set upon the first invocation when the SecurityException
is thrown.
[1] http://java.sun.com/j2se/1.5.0/docs/api/java/net/URLConnection.html#getPermission()
The permissions associated with the connection object have nothing to do with the permission of the HTTP server at the other end of the connection. As Ralf mentioned, that pertains to the local JVM security policy. One interprets the status of HTTP service responses (e.g. 200, 403, etc. ) via HttpURLConnection.getResponseCode()
.
精彩评论