从servlet内部调用外部站点时获取HTTP 406 [英] Getting HTTP 406 while calling external site from within servlet

查看:120
本文介绍了从servlet内部调用外部站点时获取HTTP 406的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  / ** 
* @see HttpServlet#doPost( HttpServletResponse响应)
* @see HttpServlet#doGet(HttpServletRequest请求,HttpServletResponse响应)
* / $ b $ public void doIt(HttpServletRequest请求,HttpServletResponse响应)抛出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())); //此行生成错误
String line =;
PrintWriter pw = response.getWriter(); ((line = br.readLine())!= null){
pw.println(line);
while


$ / code>

在tomcat中运行这个servlet会给我一个http 406错误。

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



我在这里错过了什么?



亲切的问候,
维尔纳

解决方案

我解决了这个问题。调查通过网络发送的内容。

我的网址包含一个空格,导致了所有问题。



正如前面所述我想联系谷歌搜索,我的网址看起来像这样:

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

这是在浏览器地址栏但在java中没有。



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

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

这当然不正确。它应该都是一个名为'Request URI'的字段。

将空间改为'%20'解决了这个问题。


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

解决方案

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.

这篇关于从servlet内部调用外部站点时获取HTTP 406的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆