我正在尝试使用java应用程序中的网站,就像通过浏览器一样;这是我第一次尝试这样的东西,我担心我遗漏了一些东西。
我使用apache httpcore库通过post方法处理http请求,使用wireshark我已经看到post请求中的参数,并将它们添加到我使用java处理的请求中;头文件也是如此。
如果我嗅探用java发出的请求,我不能捕获http post请求,只能捕获tcp流量。
我是这样做这个请求的:
HttpPost httpPost = new HttpPost("http://xxx.xxx");
httpPost.setHeader("Host", "xxx.xxx:xxxx");
.
.
.
HttpParams params = new BasicHttpParams();
params.setParameter("aaaa", "bbbb");
.
.
.
HttpResponse response = httpclient.execute(httpPost);我是不是遗漏了什么?
我应该检查其他的东西吗?
非常感谢你的帮助!
发布于 2012-08-29 18:36:50
您必须为post请求提供一个主体,这是通过在HttpPost上调用.setEntity(HttpEntity)方法来实现的。
private void sendToPostProxy(HttpServletRequest request,
HttpServletResponse response) throws IOException {
//the url to forward too
String url = "http://127.0.0.1:"+proxyPort+request.getRequestURI()
+(request.getQueryString()==null?"":"?"+request.getQueryString());
HttpPost get = new HttpPost(url);
//I am streaming requests straight through, but there are many Entity types you can use
get.setEntity(new InputStreamEntity(request.getInputStream(), request.getContentLength()));
sendToProxy(request, response, get);
}
private void sendToProxy(HttpServletRequest request,
HttpServletResponse response,HttpRequestBase get) throws IOException {
DefaultHttpClient client = new DefaultHttpClient();
Enumeration headers = request.getHeaderNames();
//copy headers
while(headers.hasMoreElements()){
String next = String.valueOf(headers.nextElement());
String header = request.getHeader(next);
if (!get.containsHeader(next)&&!"Content-Length".equalsIgnoreCase(next))
get.addHeader(next, header);
}
try{
//perform post
HttpResponse proxied = client.execute(get);
//set client headers
for (Header h : proxied.getAllHeaders()){
response.setHeader(h.getName(), h.getValue());
}
//stream to client
HttpEntity body = proxied.getEntity();
body.writeTo(response.getOutputStream());
response.setStatus(HttpServletResponse.SC_OK);
}catch(Exception e){
e.printStackTrace();
get.abort();
}
}https://stackoverflow.com/questions/12073545
复制相似问题