22

I have HTML based queries in my code and one specific kind seems to give rise to IOExceptions upon receiving 505 response from the server. I have looked up the 505 response along with other people who seemed to have similar problems. Apparently 505 stands for HTTP version mismatch, but when I copy the same query URL to any browser (tried firefox, seamonkey and Opera) there seems to be no problem. One of the posts I read suggested that the browsers might automatically handle the version mismatch problem..

I have tried to dig in deeper by using the nice developer tool that comes with Opera, and it looks like there is no mismatch in versions (I believe Java uses HTTP 1.1) and a nice 200 OK response is received. Why do I experience problems when the same query goes through my Java code?

     private InputStream openURL(String urlName) throws IOException{    
    URL url = new URL(urlName);
    URLConnection urlConnection = url.openConnection();
    return urlConnection.getInputStream();
 }

sample link: http://www.uniprot.org/uniprot/?query=mnemonic%3aNUGM_HUMAN&format=tab&columns=id,entry%20name,reviewed,organism,length

Angelo Fuchs
  • 9,825
  • 1
  • 35
  • 72
posdef
  • 6,498
  • 11
  • 46
  • 94

5 Answers5

45

There has been some issues in Tomcat with URLs containing space in it. To fix the problem, you need to encode your url with URLEncoder.

Example (notice the space):

String url="http://example.org/test test2/index.html";
String encodedURL=java.net.URLEncoder.encode(url,"UTF-8");
System.out.println(encodedURL); //outputs http%3A%2F%2Fexample.org%2Ftest+test2%2Findex.html
  • I had too the same issue with Tomcat 7.0.8 and needed to encode one of the parameter. But there is more : the request was perfectly handled by Tomcat if done with Firefox ! – Pragmateek May 17 '11 at 22:21
  • 3
    I replaced the spaces in my url with %20. Thank you, this fixed issue – Chris Oct 30 '13 at 16:50
  • My solution was, as @Chris replacing spaces with "%20", but this post made me think :) Thanks! – InsaurraldeAP Nov 25 '15 at 20:41
  • Thank you very much for your simple yet very useful solution, @user508434! – CodeBreaker Feb 23 '16 at 16:54
  • If you are targetting a rest service with pathParams, you must encode only the values of the params as noted here https://stackoverflow.com/a/13253031/111110 – Broken_Window Feb 02 '21 at 00:50
8

AS a developer at www.uniprot.org I have the advantage of being able to look in the request logs. In the last year according to the logs we have not send a 505 response code. In any case our servers do understand http 1 requests as well as the default http1.1 (though you might not get the results that you expect).

That makes me suspect there was either some kind of data corruption on the way. Or you where affected by a hardware failure (lately we have had some trouble with a switch and a whole datacentre ;). In any case if you ever have questions or problems with uniprot.org please contact help@uniprot.org then we can see if we can help/fix the problem.

Your code snippet seems normal and should work.

Regards, Jerven Bolleman

Jerven
  • 582
  • 3
  • 7
  • Hi Jerven, Thanks a lot for your reply, even though it adds to my confusion (the fact that you have never sent a 505) it is appreciated that you took your time to go through the logs, and try to track down the possible causes. I have however send a mail to helpdesk a couple of days ago, when I first started experiencing the problem and still haven't gotten a reply. I figured that there are quite a number of issues you probably need to attend to there, so I just wanted to make sure if there was anything I could do on my end. :) – posdef Sep 03 '10 at 09:21
3

Are you behind a proxy? This code works for me and prints out the same text I see through a browser.

final URL url = new URL("http://www.uniprot.org/uniprot/?query=mnemonic%3aNUGM_HUMAN&format=tab&columns=id,entry%20name,reviewed,organism,length");
final URLConnection conn = url.openConnection();
final InputStream is = conn.getInputStream();
System.out.println(IOUtils.toString(is));

conn is an instance of HttpURLConnection

Jon Freedman
  • 9,469
  • 4
  • 39
  • 58
  • No there is no proxy here, as far as I know.. I have tried out the simplified case as you have written here and it seems to work for me too, odd.. Running the fullscale test again to see if the problem still persists, however it takes a long while to see the results.. In case I still get the exception, I will update the post with the stack trace.. – posdef Sep 02 '10 at 14:17
3

from the API documentation for the URL class:

The URL class does not itself encode or decode any URL components [...]. 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.

so if you have any spaces in your url-str encode it before calling new URL(url-str)

posdef
  • 6,498
  • 11
  • 46
  • 94
Legna
  • 1,632
  • 15
  • 15
2

@posdef I was having same HTTP error code 505 problem. When I pasted URL that I was using in Java code in Firefox, Chrome it worked. But through code was giving IOException. But at last I came to know that in url string there were brackets '(' and ')', by removing them it worked so it seems I needed URLEncodeing same like browsers.

shreyas
  • 1,360
  • 11
  • 11