我的代码是这样的:
- 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 本来就不稳定
------解决方案--------------------
------解决方案--------------------
你把 User-Agent 的值设得更新一些的,你那个 UA 太旧了,估计 Google 都不支持了