1

I have the following code in my servlet:

/**
 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
 */
public void doIt(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    URL url = new URL("http://some.url.that.works.well.nl/q=hello&ie=nl&cx=hdyehgfyegywjehdkwed:7364du7");

    URLConnection conn = url.openConnection();
    conn.connect();

    BufferedReader br = new BufferedReader(
        new InputStreamReader(conn.getInputStream()));  // This line is generating the error
    String line = "";
    PrintWriter pw = response.getWriter();
    while((line = br.readLine()) != null) {
        pw.println(line);
    } 
}

running this servlet in tomcat gives me an http 406 error.

What I try to do is from within my servlet call google site search and I would like to parse the receieved (XML) result. (For now I just print te received result). Trying the url in a browser is giving the correct result.

What am I missing here?

Kind regards, Werner

Rob The Ranger
  • 389
  • 1
  • 3
  • 17
  • 1
    HTTP 406 stands for not acceptable. Could you try ading the accept header for XML to your connection? conn.setRequestProperty("Accept","text/xml") – Jacob May 19 '11 at 08:45
  • Tried that already. No change in the output. Still getting 406 error. tried various Capitalization of the wordt accept as I don't know if that could be the problem. The result stays the same. – Rob The Ranger May 19 '11 at 11:00

4 Answers4

4

A 406 HTTP error means that the server couldn't build a response to your request with an acceptable content type. It means that your URLConnection asks the server for a given content type, and the server can't find an appropriate one.

You can change the content type requested by your URLConnection using the setRequestProperty(String, String) method. You will have to add something like:

conn.setRequestProperty("accept", "text/xml");

(This supposes the server sends XML back to you)

Vivien Barousse
  • 20,555
  • 2
  • 63
  • 64
3

I solved the problem.
I used wireshark to investigate what was send across the wire.
My url contained a space and that was causing all the problems.

As told before I wanted to contact google search and my url looked something like:

http://www.google.com/search?q=golden handpressure&ie=8758438&cx=hjfweufhweufwef:9e

this is working in the browser address bar but not in java.

With wireshark I found out that my request header contained:

Request URI: http://www.google.com/search?q=golden
Request version: handpressure&ie=8758438&cx=hjfweufhweufwef:9e

This is ofcourse not correct. it should all be one field called 'Request URI'.
Changing the space into '%20' solved the problem.

Rob The Ranger
  • 389
  • 1
  • 3
  • 17
  • Spot on. Many Libraries and tools (Like postman) encode the url internally so that , it works like right away. But some libraries do not encode the URL . So either you may have to find a library to encode URL or do it manually. – madhairsilence Dec 21 '16 at 07:32
0

Check the server for Content-Type response header. It should return :

Content-Type:text/xml; charset=UTF-8

charset=UTF-8 should be there in response. If not add it to header if server is in your control.

Sandeep Kumar
  • 1,193
  • 13
  • 14
0

I think it has to do with Accept Headers. Can you check the accept-headers exchanged.

Suraj Chandran
  • 24,433
  • 12
  • 63
  • 94