4

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:

https://somesite.ru/?arg1=val1&arg2=val2

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.

horgh
  • 17,918
  • 22
  • 68
  • 123
  • What is the purpose of `Expect` header? doesn't seem to be a standard http request header, btw `Content-Type` is a response header, any reason for using that in request header list? – Vikdor Oct 04 '12 at 01:41
  • @Vikdor that was experimenting...earlier found some posts advising this.. – horgh Oct 04 '12 at 01:48
  • Edited the question, removed incorrect headers – horgh Oct 04 '12 at 01:49

1 Answers1

2

Several things you should examine:

  1. if you have access to the server, is it logging anything?
  2. 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");

  3. you may need to encode your text - look at Java's URLEncoder

Paul Jowett
  • 6,513
  • 2
  • 24
  • 19
  • In the end the issue was on the server. Though the provider, whose server we communicated with, refused to explain its essense, there was nothing to do in out java app. So, the closest idea, I've got from this post, is your point 1. Anyway, thanks! – horgh Oct 05 '12 at 02:13
  • i had the same error (415) and i want to add a fourth point, in case some frustrated programmer bumps into this post: (4) Check the server's 'web.xml' file. There is the possibility that you have added the library for JSON/xml functionality (eg Jackson) but you forgot to set the dependency. This can lead to a 415 error also. – Mario Jan 17 '15 at 18:16