简体   繁体   English

从Servlet调用外部站点时获取HTTP 406

[英]Getting HTTP 406 while calling external site from within servlet

I have the following code in my servlet: 我的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. 在tomcat中运行此servlet给我一个http 406错误。

What I try to do is from within my servlet call google site search and I would like to parse the receieved (XML) result. 我想做的是从我的servlet调用Google网站搜索中进行的,我想解析接收到的(XML)结果。 (For now I just print te received result). (目前,我只打印接收到的结果)。 Trying the url in a browser is giving the correct result. 在浏览器中尝试该URL可以给出正确的结果。

What am I missing here? 我在这里想念什么?

Kind regards, Werner 亲切的问候,沃纳

A 406 HTTP error means that the server couldn't build a response to your request with an acceptable content type. 406 HTTP错误表示服务器无法使用可接受的内容类型构建对您请求的响应。 It means that your URLConnection asks the server for a given content type, and the server can't find an appropriate one. 这意味着您的URLConnection向服务器询问给定的内容类型,而服务器找不到合适的内容类型。

You can change the content type requested by your URLConnection using the setRequestProperty(String, String) method. 您可以使用setRequestProperty(String, String)方法更改URLConnection请求的内容类型。 You will have to add something like: 您将必须添加以下内容:

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

(This supposes the server sends XML back to you) (这假设服务器将XML发送回给您)

I solved the problem. 我解决了问题。
I used wireshark to investigate what was send across the wire. 我用wireshark研究了通过电线发送的内容。
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: 如前所述,我想联系google搜索,我的网址看起来像:

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. 这在浏览器地址栏中起作用,但在Java中不起作用。

With wireshark I found out that my request header contained: 通过wireshark,我发现我的请求标头包含:

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'. 它应该全部是一个称为“请求URI”的字段。
Changing the space into '%20' solved the problem. 将空格更改为'%20'解决了该问题。

Check the server for Content-Type response header. 检查服务器上的Content-Type响应头。 It should return : 它应该返回:

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

charset=UTF-8 should be there in response. charset = UTF-8应该在响应中。 If not add it to header if server is in your control. 如果没有,则将其添加到标头中(如果服务器在您的控制范围内)。

I think it has to do with Accept Headers. 我认为这与“接受标头”有关。 Can you check the accept-headers exchanged. 您可以检查交换的接受标头吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM