我試圖構建原始HTTP POST請求。但是,我不想實際連接到服務器併發送消息。獲取將從HttpPost發送的完整的原始HTTP請求消息
我一直在探討Apache HTTP庫,希望能夠創建一個HttpPost對象,設置實體,然後獲取它將創建的消息。到目前爲止,我可以轉儲實體,但不是整個請求,因爲它出現在服務器端。
任何想法?當然,除了重新創建輪子之外。
解決方案
我重構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;
}