我有一个在 jboss 4.2.2 服务器上运行的 jsp 页面。在 jsp 中,我尝试使用 request.getMethod() 和 request.getHeaderNames() 等方法打印 http post 请求的标头。我确定该请求是一个 http 帖子,因为我从另一个程序生成它,但是 jsp 打印的是一个 http get请求,打印的内容长度是-1,并且打印的标题与我发送的标题不匹配。
生成请求的应用程序代码或多或少是这样的:
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
谢谢您的帮助。