获取充分,原始的HTTP请求会从HttpPost发送消息 [英] Get the full, raw HTTP request message that would be sent from an HttpPost
问题描述
我想建立一个原始的HTTP POST请求。我不想真正连接到服务器并发送邮件,但是。
我一直在打探将Apache HTTP库,希望我能创造一个HttpPost对象,设置实体,然后抓住它会创建消息。到目前为止,我可以转储实体,而不是整个请求,因为它会出现在服务器侧
任何想法?除了刚刚重新创建轮,当然。
解决方案
我重构ShyJ的反应成一对静态类的,但是原始响应工作得很好。这里有两个类:
公共静态最后一类LoopbackPostMethod扩展的PostMethod {
私有静态最后弦乐STATUS_LINE =HTTP / 1.1 200 OK; @覆盖
保护无效readResponse(州的HttpState,康涅狄格州的HttpConnection)抛出IOException异常,HttpException {
状态行=新的状态行(STATUS_LINE);
}
}公共静态最后一类LoopbackHttpConnection扩展的HttpConnection {
私有静态最后弦乐HOST =127.0.0.1;
私有静态最终诠释PORT = 80; 私人最终的OutputStream fOutputStream; 公共LoopbackHttpConnection(OutputStream中的OutputStream){
超(主机,端口);
fOutputStream =的OutputStream;
} @覆盖
公共无效flushRequestOutputStream()抛出IOException异常{/ *什么也不做* /} @覆盖
公众的OutputStream getRequestOutputStream()抛出IOException异常,IllegalStateException异常{
返回fOutputStream;
} @覆盖
公共无效写入(字节[]数据)抛出IOException异常,IllegalStateException异常{
fOutputStream.write(数据);
}
}
下面是工厂方法,我使用了我自己的实现,作为一个例子:
私人的ByteBuffer createHtt prequest(ByteBuffer的数据)抛出HttpException,IOException异常{
LoopbackPostMethod的PostMethod =新LoopbackPostMethod();
最后ByteArrayOutputStream的OutputStream =新ByteArrayOutputStream();
postMethod.setRequestEntity(新ByteArrayRequestEntity(data.array()));
postMethod.execute(新的HttpState(),新LoopbackHttpConnection(OutputStream中));
字节[]字节= outputStream.toByteArray();
ByteBuffer的缓冲= ByteBuffer.allocate(bytes.length);
buffer.put(字节);
返回缓冲区;
}
这可以和 achived HTTP客户端,并伪造一些方法。我用的 3.1
版本 HTTP客户端
。
的示例的
这code:
进口java.io.ByteArrayOutputStream中;
进口java.io.IOException异常;
进口java.io.OutputStream中;进口org.apache.commons.httpclient.HttpConnection;
进口org.apache.commons.httpclient.HttpException;
进口org.apache.commons.httpclient.HttpState;
进口org.apache.commons.httpclient.StatusLine;
进口org.apache.commons.httpclient.methods.PostMethod;公共类主要{
公共静态无效的主要(字串[] args)抛出异常{
最后ByteArrayOutputStream BAOS =新ByteArrayOutputStream();
的PostMethod方法=新的PostMethod(){
@覆盖
保护无效readResponse(州的HttpState,康涅狄格州的HttpConnection)
抛出IOException异常,HttpException {
状态行=新状态行(HTTP / 1.1 200 OK);
}
};
method.addParameter(AA,B); method.execute(新的HttpState(),新的HttpConnection(http://www.google.abc/hi,80){ @覆盖
公共无效flushRequestOutputStream()抛出IOException
} @覆盖
公众的OutputStream getRequestOutputStream()抛出IOException异常,
IllegalStateException异常{
返回BAOS;
} @覆盖
公共无效写入(字节[]数据)抛出IOException异常,
IllegalStateException异常{
baos.write(数据);
} }); 最后弦乐postBody =新的String(baos.toByteArray()); 的System.out.println(postBody);
}
}
将返回
POST / HTTP / 1.1
用户代理:Jakarta Commons的-的HttpClient / 3.1
主持人:http://www.google.abc/hi
内容长度:4
内容类型:应用程序/ x-WWW的形式urlen codeDAA = B
I'm trying to build a raw HTTP POST request. I don't want to actually connect to the server and send the message, however.
I've been poking around the Apache HTTP libraries, hoping that I could just create an HttpPost object, set the entity, and then grab the message that it would have created. So far, I can dump the entity, but not the entire request as it'd appear on the server-side.
Any ideas? Aside from just re-creating the wheel, of course.
Solution
I refactored ShyJ's response into a pair of static classes, however the original response works just fine. Here are the two classes:
public static final class LoopbackPostMethod extends PostMethod {
private static final String STATUS_LINE = "HTTP/1.1 200 OK";
@Override
protected void readResponse(HttpState state, HttpConnection conn) throws IOException, HttpException {
statusLine = new StatusLine (STATUS_LINE);
}
}
public static final class LoopbackHttpConnection extends HttpConnection {
private static final String HOST = "127.0.0.1";
private static final int PORT = 80;
private final OutputStream fOutputStream;
public LoopbackHttpConnection(OutputStream outputStream) {
super(HOST, PORT);
fOutputStream = outputStream;
}
@Override
public void flushRequestOutputStream() throws IOException { /* do nothing */ }
@Override
public OutputStream getRequestOutputStream() throws IOException, IllegalStateException {
return fOutputStream;
}
@Override
public void write(byte[] data) throws IOException, IllegalStateException {
fOutputStream.write(data);
}
}
Here's the factory method that I'm using for my own implementation, as an example:
private ByteBuffer createHttpRequest(ByteBuffer data) throws HttpException, IOException {
LoopbackPostMethod postMethod = new LoopbackPostMethod();
final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
postMethod.setRequestEntity(new ByteArrayRequestEntity(data.array()));
postMethod.execute(new HttpState(), new LoopbackHttpConnection(outputStream));
byte[] bytes = outputStream.toByteArray();
ByteBuffer buffer = ByteBuffer.allocate(bytes.length);
buffer.put(bytes);
return buffer;
}
This can be achived with http-client and faking some methods. I used the 3.1
version of http-client
.
Example
This code:
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import org.apache.commons.httpclient.HttpConnection;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpState;
import org.apache.commons.httpclient.StatusLine;
import org.apache.commons.httpclient.methods.PostMethod;
public class Main {
public static void main(String[] args) throws Exception {
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
PostMethod method = new PostMethod () {
@Override
protected void readResponse(HttpState state, HttpConnection conn)
throws IOException, HttpException {
statusLine = new StatusLine ("HTTP/1.1 200 OK");
}
};
method.addParameter("aa", "b");
method.execute(new HttpState (), new HttpConnection("http://www.google.abc/hi", 80) {
@Override
public void flushRequestOutputStream() throws IOException {
}
@Override
public OutputStream getRequestOutputStream() throws IOException,
IllegalStateException {
return baos;
}
@Override
public void write(byte[] data) throws IOException,
IllegalStateException {
baos.write(data);
}
});
final String postBody = new String (baos.toByteArray());
System.out.println(postBody);
}
}
will return
POST / HTTP/1.1
User-Agent: Jakarta Commons-HttpClient/3.1
Host: http://www.google.abc/hi
Content-Length: 4
Content-Type: application/x-www-form-urlencoded
aa=b
这篇关于获取充分,原始的HTTP请求会从HttpPost发送消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!