Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8869595
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T17:30:56+00:00 2026-06-14T17:30:56+00:00

I’m trying to build a raw HTTP POST request. I don’t want to actually

  • 0

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;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T17:30:57+00:00Added an answer on June 14, 2026 at 5:30 pm

    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
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I'm trying to create an if statement in PHP that prevents a single post
I would like my Web page http://www.gmarks.org/math_in_e-mail.txt on my Apache 2.2.14 server to display
Let's say I'm outputting a post title and in our database, it's Hello Y’all
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.