1---2title: 'HTTP 500 - squid vs java.net.URL'3date: '2011-01-04'4published_at: '2011-01-04T16:21:00.004+11:00'5tags: ['development', 'java', 'squid', 'tomcat']6author: 'Gavin Jackson'7excerpt: 'Ran into an interesting bug today that I thought I should share with the interwebs. We have a web application that supports RESTful http requests to interact with the system. Seems to work fine using...'8updated_at: '2011-01-04T16:37:40.989+11:00'9legacy_url: 'http://www.gavinj.net/2011/01/http-500-squid-vs-javaneturl.html'10---1112Ran into an interesting bug today that I thought I should share with the interwebs. We have a web application that supports RESTful http requests to interact with the system.1314Seems to work fine using browsers/wget etc. But when trying to use java.net.URL (as used by several JUnit tests), I kept getting an http 500 (internal server) error message.1516Tomcat logs showed no interaction with the web server, indicating that it must be something wrong with the Squid reverse proxy server (which we have set up to cache outbound web traffic). SQUID LOGS1718```19From java test harness (java.net.URL)201294109122.110 1175 127.0.0.1 TCP_MISS/500 280 GET http://localhost:3128/geoserver/rest/gazetteer/latlon/-35.0,145.3 - FIRST_UP_PARENT/myAccel -211294109146.216 0 127.0.0.1 TCP_NEGATIVE_HIT/500 306 GET http://localhost:3128/geoserver/rest/gazetteer/latlon/-35.0,145.3 - NONE/- -221294109191.214 0 127.0.0.1 TCP_NEGATIVE_HIT/500 306 GET http://localhost:3128/geoserver/rest/gazetteer/latlon/-35.0,145.3 - NONE/- -23...24```2526Always comes back with 500 error code.2728From browser:2930```311294109466.962 1226 127.0.0.1 TCP_MISS/200 698 GET http://localhost:3128/geoserver/rest/gazetteer/latlon/-35.0,145.3 - FIRST_UP_PARENT/myAccel application/xml32```3334Note that the error code is 200 (not 500). 200 == HTTP OK 500 == HTTP Internal Server Error Subsequent harness requests come back ok (once it is in the cache). WHAT IS GOING OVER THE WIRE? So the question is, how do the http GET requests differ? Wireshark dump (from harness):3536```37GET /gazetteer/latlon/-35.0,145.4 HTTP/1.138User-Agent: Java/1.6.0_2239Host: spatial-dev.ala.org.auAccept: text/html, image/gif, image/jpeg, *; q=.2, */*; q=.240Connection: keep-alive41HTTP/1.1 500 Internal Server Error42Date: Tue, 04 Jan 2011 02:58:29 GMT43Server: Apache-Coyote/1.144Age: 12945Content-Length: 046X-Cache: HIT from ala-devmaps.vm.csiro.auX-Cache-Lookup: HIT from ala-devmaps.vm.csiro.au:3128Via: 1.1 ala-devmaps.vm.csiro.au:3128 (squid/2.7.STABLE5)47Connection: close48Content-Type: text/plain49```5051Wireshark dump (from browser):5253```54GET /gazetteer/latlon/-35.0,145.5 HTTP/1.155Host: spatial-dev.ala.org.auUser-Agent: Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.6; en-US; rv:1.9.2.6) Gecko/20100625 Firefox/3.6.656Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.857Accept-Language: en-us,en;q=0.558Accept-Encoding: gzip,deflate59Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.760Keep-Alive: 11561Connection: keep-alive62Cookie: __utma=100819395.1000872318.1288999346.1292997025.1293056871.6; __utmz=100819395.1288999346.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none)63HTTP/1.1 200 OK64Date: Tue, 04 Jan 2011 03:04:26 GMT65Server: Noelios-Restlet-Engine/1.0..866Expires: Mon, 04 Jan 2021 03:04:26 GMT67Content-Encoding: gzip68Content-Type: application/xml;charset=ISO-8859-169Content-Length: 28670X-Cache: MISS from ala-devmaps.vm.csiro.auX-Cache-Lookup: MISS from ala-devmaps.vm.csiro.au:3128Via: 1.1 ala-devmaps.vm.csiro.au:3128 (squid/2.7.STABLE5)71Keep-Alive: timeout=15, max=10072Connection: Keep-Alive73.............N.0.E.~E....W...q?.].$....N;y-2..B....,..k..mr.f...Kv:(m....}....S......4T@.$..4.7.e..R....b>OK..M...B...De#.b.?.....Ph.7kt..o74...).).....g.8 ..~.G..D..;m[..z...9...a4/...........75!..........K-..`...\dkB/[...l.....uKKb.U...........(?...s_g...19.j..j..MW{\...........xnt....76```7778Note that there are additional fields set in the GET request, after a bit more investigation, this is the GET request header that you need to set for this to work properly: Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8 So the following code works (note that if you drop the gzip,deflate option, you don't need the GZIPInputStream stuff):7980```81...82 URL url = new URL("http://spatial.ala.org.au/gazetteer/search?lon=148.866⪫=-36.195");83 URLConnection connection = url.openConnection();84 connection.setRequestProperty("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");85 connection.setRequestProperty("Accept-Encoding", "gzip,deflate");86 connection.setDoInput(true);87 InputStream inStream = connection.getInputStream();88 GZIPInputStream gis = new GZIPInputStream(inStream);89 BufferedReader input =90 new BufferedReader(new InputStreamReader(gis));91 String line = "";92 while ((line = input.readLine()) != null)93 System.out.println(line);94...95```969798