从 servlet 内调用外部站点时获取 HTTP 406
我的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
406 HTTP 错误意味着服务器无法使用可接受的内容类型构建对您的请求的响应。这意味着您的
URLConnection
向服务器请求给定的内容类型,但服务器找不到合适的内容类型。您可以使用
setRequestProperty(String, String)
方法。您必须添加类似以下内容:(这假设服务器将 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 thesetRequestProperty(String, String)
method. You will have to add something like:(This supposes the server sends XML back to you)
我解决了这个问题。
我使用wireshark 来调查通过网络发送的内容。
我的 url 包含一个空格,这导致了所有问题。
正如之前所说,我想联系 google 搜索,我的 url 看起来像这样:
这在浏览器地址栏中工作,但在 java 中不起作用。
使用wireshark,我发现我的请求标头包含:
这当然不正确。它应该都是一个名为“请求 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:
this is working in the browser address bar but not in java.
With wireshark I found out that my request header contained:
This is ofcourse not correct. it should all be one field called 'Request URI'.
Changing the space into '%20' solved the problem.
我认为这与接受标头有关。您可以检查交换的接受标头吗?
I think it has to do with Accept Headers. Can you check the accept-headers exchanged.
检查服务器的 Content-Type 响应标头。它应该返回:
charset=UTF-8 应该作为响应。如果不将其添加到标头(如果服务器在您的控制范围内)。
Check the server for Content-Type response header. It should return :
charset=UTF-8 should be there in response. If not add it to header if server is in your control.