各位朋友,问个java读取google网页内容的问题,请进来看

清柠 2012-04-05 04:12:51
我的代码是这样的:

import java.net.*;
import java.io.*;

public class testUrl
{
public static void main(String[] args)
{
StringBuffer document = new StringBuffer();
try
{
URL url = new URL("http://scholar.google.com.hk/scholar?hl=zh-CN&q=Still+stratus+not+altocumulus%3A+further+evidence+against+the+date%2Fparty+hub+distinction&lr=&as_ylo=&as_vis=1");
URLConnection conn = url.openConnection();
conn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");
BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line = null;
while ((line = reader.readLine()) != null)
document.append(line + " ");
reader.close();
}
catch (MalformedURLException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
System.out.println(document.toString());
}
}

eclipse编译报错:
java.io.IOException: Server returned HTTP response code: 503 for URL: http://www.google.com.hk/sorry/?continue=http://scholar.google.com.hk/scholar%3Fhl%3Dzh-CN%26q%3DStill%2Bstratus%2Bnot%2Baltocumulus%253A%2Bfurther%2Bevidence%2Bagainst%2Bthe%2Bdate%252Fparty%2Bhub%2Bdistinction%26lr%3D%26as_ylo%3D%26as_vis%3D1
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1305)
at testUrl.main(testUrl.java:14)
这段代码有时候能正确执行,有时候就报上面的错误,哪位朋友指点一下。
...全文
200 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
清柠 2012-04-06
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 的回复:]
你把 User-Agent 的值设得更新一些的,你那个 UA 太旧了,估计 Google 都不支持了
[/Quote]
请问应该更新成什么,我对User-Agent 不了解。
清柠 2012-04-06
  • 打赏
  • 举报
回复
顶。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。
  • 打赏
  • 举报
回复
你把 User-Agent 的值设得更新一些的,你那个 UA 太旧了,估计 Google 都不支持了
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 的回复:]

503是一种HTTP状态码。英文名503 Service Unavailable与404(404 Service Unavailable)是同属一种网页状态出错码。前者是服务器出错的一种返回状态,后者是网页程序没有相关结果后返回的一种状态,需要优化网站的时候通常须要制作404出错页以便网站整体优化。
[/Quote]

不是吧?503 跟 404 差别很大的吧?

5xx 属于服务器端错误
4xx 属于客户端错误

404 我记得是 Not Found 啊,不是 Service Unavailable

出现 503 的话可能服务器端在重启服务,或者正在进行维护工作,你等等再试试。
清柠 2012-04-05
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 的回复:]
你程序在我电脑上运行成功
如果google不能读 那就换个网站试试看
[/Quote]
在我的电脑上就报错,其他网上是可以的,可我就需要读google
代号裤子 2012-04-05
  • 打赏
  • 举报
回复
你程序在我电脑上运行成功
如果google不能读 那就换个网站试试看
清柠 2012-04-05
  • 打赏
  • 举报
回复
顶。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。
清柠 2012-04-05
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 的回复:]
服务器的原因吧 google 本来就不稳定
[/Quote]
那有没有什么好方法呢?
creso 2012-04-05
  • 打赏
  • 举报
回复
503是一种HTTP状态码。英文名503 Service Unavailable与404(404 Service Unavailable)是同属一种网页状态出错码。前者是服务器出错的一种返回状态,后者是网页程序没有相关结果后返回的一种状态,需要优化网站的时候通常须要制作404出错页以便网站整体优化。
贪睡的兔子 2012-04-05
  • 打赏
  • 举报
回复
服务器的原因吧 google 本来就不稳定

62,614

社区成员

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

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