日期:2014-05-20  浏览次数:20687 次

各位朋友,问个java读取google网页内容的问题,请进来看
我的代码是这样的:
Java code

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)
这段代码有时候能正确执行,有时候就报上面的错误,哪位朋友指点一下。

------解决方案--------------------
服务器的原因吧 google 本来就不稳定
------解决方案--------------------
探讨

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

------解决方案--------------------
你把 User-Agent 的值设得更新一些的,你那个 UA 太旧了,估计 Google 都不支持了