I'm using the following code to send a GET request and then receive response:
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:
The expected output is xml, smth like this:
<?xml version="1.0" encoding="UTF-8"?>
<response>
<tag1>00000</tag1>
<tag2>0</tag2>
...
</response>
5 of 10 attempts do the job.
Other 5 attempts return:
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)
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.
The IDE is 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.
Several things you should examine:
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