url中参数的问题

Mark31 2008-10-31 11:49:30
这是一个通过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) {}
}
}
...全文
195 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
楼主可以看看这个帖子,帖主在请求时设置了一下 HTTP 的头(就在楼主的页面中有代码)

http://topic.csdn.net/u/20080528/19/4455daae-e771-4445-be83-aa794cd70244.html

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧