获取将从HttpPost发送的完整的原始HTTP请求消息

我试图build立一个原始的HTTP POST请求。 但是,我不想实际连接到服务器并发送消息。

我一直在围绕Apache HTTP库,希望能够创build一个HttpPost对象,设置实体,然获取它将创build的消息。 到目前为止,我可以转储实体,但不是整个请求,因为它出现在服务器端。

有任何想法吗? 除了重新创build轮子,当然。

我将ShyJ的响应重构为一对静态类,但是原始响应工作得很好。 这里是两个类:

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); } } 

下面是我用于自己实现的工厂方法,例如:

 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; } 

这可以通过http-client来实现,并伪装一些方法。 我使用了http-client3.1版本。

此代码:

 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); } } 

将返回

 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 

我会做的是实现一个或多个HttpClient的接口,并使用我的无操作的实现。

ClientConnectionManagerAbstractHttpClient ,例如。任何连接。