发送一个HTTP POST请求,但JBOSS收到一个空的GET请求
问题描述:
我有一个运行在jboss 4.2.2服务器上的jsp页面。在jsp中,我试图使用request.getMethod()和request.getHeaderNames()方法来打印http post请求的头文件。我确定该请求是一个http post,因为我从另一个程序生成它,但是jsp print是一个http 获取请求,而打印的内容长度是-1,并且打印的标头不匹配发送的标头。发送一个HTTP POST请求,但JBOSS收到一个空的GET请求
生成请求是更多或更少,这样的应用程序的代码:
URL url = new URL(the_url);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setDoOutput(true);
con.setDoInput(true);
con.setRequestMethod("POST");
con.setRequestProperty("Content-type", "application/timestamp-query");
con.setRequestProperty("Content-length", String.valueOf(data.length));
out = con.getOutputStream();
out.write(data);
out.flush();
其中即时试图打印请求报头中的应用程序的代码:
<%@page import="java.io.InputStream"%>
<%@page import="java.util.Date"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"
import="java.util.Enumeration"
import="java.io.File"
import="java.io.FileOutputStream"
import="java.util.Date"%><%
File logfile = new File("/home/tsa/logfile");
FileOutputStream log_os = new FileOutputStream(logfile);
Enumeration header_names = request.getHeaderNames();
log_os.write(("cont-len: "+request.getContentLength()+"\r\n").getBytes());
log_os.write(("method: "+request.getMethod()+"\r\n").getBytes());
while (header_names.hasMoreElements()) {
String name = (String)header_names.nextElement();
log_os.write((name+": "+request.getHeader(name)+"\r\n").getBytes());
}
log_os.close();%>
HTTP数据,我应该接受:
POST/HTTP/1.1
Content-type: application/timestamp-query
User-Agent: Java/1.7.0_05
Host: localhost:8080
Accept: text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2
Connection: keep-alive
Content-Length: 51
MDECAQEwITAJBgUrDgMCGgUABBSg8UkKINAhHJl7RLw1fhly3quK4wYGBACPZwEBAQH/
HTTP数据我收到:
User-Agent: Java/1.7.0_05
Host: 192.168.56.101:8080
Accept: text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2
Connection: keep-alive
The method reported is GET and the Content-Length: -1
谢谢你的帮忙。
答
我有同样的问题。我更新到JDK 1.7.0_u21,问题消失了。
您是否尝试过使用其他网站进行调试? 我使用http://requestb.in/。它会显示收到的数据。只需暂时更改网址即可指向该网站。 如果失败,请尝试使用[Fiddler](http://www.fiddler2.com/)检查您的请求。 – mercutio 2012-08-02 10:51:25
尝试将'Content-type'设置为'application/x-www-form-urlencoded' – 2012-08-02 11:14:18
或者您可以使用Apache HttpPost,请参阅此处:http://www.vogella.com/articles/ApacheHttpClient/article.html# example_post – 2012-08-02 11:29:25