java 服务器返回 HTTP 响应代码:400
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12470141/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Server returned HTTP response code: 400
提问by user837306
I am reading data from a webservice. The issue if I put the link on the browser it works fine. When I run like this give me error. I am suspecting is it due to the way how I send my parameters. My paramater list has this dID=1,5,7,11,14,18,26&FromDate=18 Sep 2012 00:00 am&ToDate=18 Sep 2012 10:00 am
. Do I need to do some encoding here?
我正在从网络服务读取数据。如果我把链接放在浏览器上它工作正常的问题。当我像这样运行时会给我错误。我怀疑是不是因为我发送参数的方式。我的参数列表有这个dID=1,5,7,11,14,18,26&FromDate=18 Sep 2012 00:00 am&ToDate=18 Sep 2012 10:00 am
。我需要在这里做一些编码吗?
URL xmlURLDM = new URL(urlDM);
InputStream xml2 = xmlURLDM.openStream();
I get this error
我收到这个错误
java.io.IOException: Server returned HTTP response code: 400 for URL:
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1612)
at java.net.URL.openStream(URL.java:1035)
at xmlreader.main(xmlreader.java:172)
回答by Paul Jowett
You do need encoding, most likley it is the spaces in your URL that is causing the trouble. Use Javas built in url-encoding. eg:
您确实需要编码,最有可能的是您的 URL 中的空格导致了问题。使用内置 url 编码的 Java。例如:
String encoded = URLEncoder.encode(myUrl, "UTF-8");
... call web service with encoded as URL
... 调用编码为 URL 的 Web 服务
There can be other reasons for the status code being 400, but this encoding issue is probably your first stumbling block.
状态代码为 400 可能还有其他原因,但此编码问题可能是您的第一个绊脚石。
回答by Santosh
The Documentation of URL says,
URL 的文档说,
The URL class does not itself encode or decode any URL components according to the escaping mechanism defined in RFC2396. It is the responsibility of the caller to encode any fields, which need to be escaped prior to calling URL, and also to decode any escaped fields, that are returned from URL. Furthermore, because URL has no knowledge of URL escaping, it does not recognise equivalence between the encoded or decoded form of the same URL.
URL 类本身并不根据 RFC2396 中定义的转义机制对任何 URL 组件进行编码或解码。调用者有责任对需要在调用 URL 之前转义的任何字段进行编码,并解码从 URL 返回的任何转义字段。此外,由于 URL 不知道 URL 转义,因此它无法识别同一 URL 的编码或解码形式之间的等效性。
So please use URLEncoder.encode()
before you invoke URL()
所以请URLEncoder.encode()
在调用之前使用URL()