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.

3
  • 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? Commented Oct 4, 2012 at 1:41
  • @Vikdor that was experimenting...earlier found some posts advising this.. Commented Oct 4, 2012 at 1:48
  • Edited the question, removed incorrect headers Commented Oct 4, 2012 at 1:49

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

2 Comments

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!
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.