Get the full, raw HTTP request message that would

2019-05-15 06:24发布

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

2条回答
Viruses.
2楼-- · 2019-05-15 06:58

What I would do is implement one or more of HttpClient's interfaces, and use my no-op implementations.

Look at ClientConnectionManager and AbstractHttpClient, for example.any connection.

查看更多
forever°为你锁心
3楼-- · 2019-05-15 07:14

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
查看更多
登录 后发表回答