Getting HTTP 406 while calling external site from within servlet

白昼怎懂夜的黑 提交于 2019-12-11 10:57:10

问题


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


回答1:


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)




回答2:


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.




回答3:


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




回答4:


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.



来源:https://stackoverflow.com/questions/6055930/getting-http-406-while-calling-external-site-from-within-servlet

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!