url中参数的问题
这是一个通过url获取网页的小函数。
为什么当url是
http://www.google.cn 和 http://www.baidu.com/s?wd=abc
时程序能获取到网页,而当url是
http://www.google.cn/search?q=abc
时,程序抛出了这样的异常:
java.io.IOException: Server returned HTTP response code: 403 for URL: http://www.google.cn/search?q=abc
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1241)
这是怎么回事呢?怎么解决这个问题呢?
下面是函数代码:
private void gotoURL(java.awt.event.ActionEvent evt) {
// After clicked the button:
BufferedReader infile = null;
URL url = null;
HttpURLConnection httpURLCon = null;
try {
url = new URL(jtfURL.getText().trim());
httpURLCon = (HttpURLConnection)url.openConnection();
httpURLCon.connect();
InputStream is = httpURLCon.getInputStream();
infile = new BufferedReader(new InputStreamReader(is));
String inLine;
while ((inLine = infile.readLine()) != null ) {
jtaContent.append(inLine + '\n');
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
infile.close();
}
catch (IOException ex) {}
}
}