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

请教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试试

    已采纳该答案
    打赏 评论

相关推荐 更多相似问题