java 在 HttpURLConnection 连接上出现 HTTP 415 错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12719106/
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
Getting HTTP 415 error on HttpURLConnection connect
提问by horgh
I'm using the following code to send a GET request and then receive response:
我正在使用以下代码发送 GET 请求,然后接收响应:
try {
URL url = new URL(strurl);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setRequestMethod("GET");
con.setDoOutput(true);
con.connect();
BufferedReader is = new BufferedReader(new InputStreamReader(con.getInputStream()));
String line;
String vAnswerStr="";
String lineSeparator = System.getProperty("line.separator");
while ((line = is.readLine()) != null) {
vAnswerStr = vAnswerStr + line + lineSeparator;
}
is.close();
} catch (IOException ex) {
ex.printStackTrace();
}
The strurl
is smth like this, though I do not think it's format may be connected with the problem:
该strurl
是水木清华这样的,但我不认为它的格式可能与问题进行连接的更多信息:
The expected output is xml, smth like this:
预期的输出是 xml,像这样:
<?xml version="1.0" encoding="UTF-8"?>
<response>
<tag1>00000</tag1>
<tag2>0</tag2>
...
</response>
5 of 10 attempts do the job.
10 次尝试中有 5 次可以完成这项工作。
Other 5 attempts return:
其他 5 次尝试返回:
java.io.IOException: Server returned HTTP response code: 415 for URL: https://somesite.ru/?arg1=val1&arg2=val2
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1615) at sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:254)
java.io.IOException:服务器返回 HTTP 响应代码:URL 为 415:https://somesite.ru/ ?arg1=val1&arg2=val2
在 sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1615) 在 sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:254)
I've read several posts on SO about the HTTP 415 error. But they don't seem to help. I've experimented with different request properties, but either failed to find the one, or it's not the case.
我已经阅读了几篇关于 HTTP 415 错误的文章。但他们似乎没有帮助。我尝试了不同的请求属性,但要么找不到,要么不是。
The IDE is NetBeans 7.0
IDE 是 NetBeans 7.0
Could anyone give me the right direction to solve the problem?
谁能给我正确的方向来解决问题?
EDIT
编辑
Forgot to say that when doing the same request from browser, it works in 100% of attempts.
忘了说,当从浏览器执行相同的请求时,它在 100% 的尝试中都有效。
采纳答案by Paul Jowett
Several things you should examine:
您应该检查几件事:
- if you have access to the server, is it logging anything?
what content-types does the destination support? You should probably specify the content type so the remote server knows what you are sending it:
con.setRequestProperty("Content-Type", "text/html; charset=utf-8");
you may need to encode your text - look at Java's URLEncoder
- 如果您有权访问服务器,它是否会记录任何内容?
目标支持哪些内容类型?您可能应该指定内容类型,以便远程服务器知道您发送的是什么:
con.setRequestProperty("Content-Type", "text/html; charset=utf-8");
您可能需要对文本进行编码 - 查看 Java 的 URLEncoder