ptnest29 2013-05-14 11:05
浏览 463
已采纳

请教Java 网络编程 Server returned HTTP response code: 400

下面是简单的代码:
URL url = new URL(uri);
URLConnection rulConnection = url.openConnection();
HttpURLConnection httpUrlConnection = (HttpURLConnection) rulConnection;

httpUrlConnection.setDoInput(true);

httpUrlConnection.setUseCaches(false);

httpUrlConnection.setRequestMethod("POST");
httpUrlConnection.connect();

InputStreamReader isr =new InputStreamReader(httpUrlConnection.getInputStream(),"GB2312");

如果我的uri 是http://search.jd.com/Search?keyword=华为&enc=utf-8&area=2 没问题,能够读取网页内容
可是换成http://item.jd.com/827017.html 就会抛异常

java.io.IOException: Server returned HTTP response code: 400 for URL: http://item.jd.com/827017.html
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1441)
at ReadURL.getBufferedFromURL(ReadURL.java:12)
at ReadURL.getItemDetail(ReadURL.java:29)
at ReadURL.main(ReadURL.java:83)
java.lang.NullPointerException

请问这是为什么同一个网站的不同网址,为什么有的能访问,有的抛异常?

  • 写回答

1条回答 默认 最新

  • tianchao_ 2013-05-14 11:16
    关注

    你设置的是post请求 换get试试

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?