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

发布于 2024-11-08 12:52:31 字数 989 浏览 0 评论 0原文

我的 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);
    } 
}

在 tomcat 中运行此 servlet 会出现 http 406 错误。

我尝试做的是从我的 servlet 中调用 google 站点搜索,我想解析收到的(XML)结果。 (现在我只是打印收到的结果)。 在浏览器中尝试该 url 会给出正确的结果。

我在这里缺少什么?

亲切的问候, 维尔纳

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

指尖凝香 2024-11-15 12:52:31

406 HTTP 错误意味着服务器无法使用可接受的内容类型构建对您的请求的响应。这意味着您的 URLConnection 向服务器请求给定的内容类型,但服务器找不到合适的内容类型。

您可以使用 setRequestProperty(String, String) 方法。您必须添加类似以下内容:(

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

这假设服务器将 XML 发送回给您)

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)

习惯成性 2024-11-15 12:52:31

我解决了这个问题。
我使用wireshark 来调查通过网络发送的内容。
我的 url 包含一个空格,这导致了所有问题。

正如之前所说,我想联系 google 搜索,我的 url 看起来像这样:

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

这在浏览器地址栏中工作,但在 java 中不起作用。

使用wireshark,我发现我的请求标头包含:

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

这当然不正确。它应该都是一个名为“请求 URI”的字段。
将空格更改为“%20”解决了问题。

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.

a√萤火虫的光℡ 2024-11-15 12:52:31

我认为这与接受标头有关。您可以检查交换的接受标头吗?

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

兰花执着 2024-11-15 12:52:31

检查服务器的 Content-Type 响应标头。它应该返回:

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

charset=UTF-8 应该作为响应。如果不将其添加到标头(如果服务器在您的控制范围内)。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文