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 7938659
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T22:52:23+00:00 2026-06-03T22:52:23+00:00

I have a String that contains an HTTP header. I want to turn this

  • 0

I have a String that contains an HTTP header. I want to turn this into an Apache HttpComponents HttpRequest object. Is there a way to do this without picking apart the string myself?

This tutorial: http://hc.apache.org/httpcomponents-core-dev/tutorial/html/fundamentals.html#d5e56 and the javadoc does not indicate as much.

  • 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-03T22:52:24+00:00Added an answer on June 3, 2026 at 10:52 pm

    A class to convert a string to apache request:

    import org.apache.http.*;
    import org.apache.http.impl.DefaultHttpRequestFactory;
    import org.apache.http.impl.entity.EntityDeserializer;
    import org.apache.http.impl.entity.LaxContentLengthStrategy;
    import org.apache.http.impl.io.AbstractSessionInputBuffer;
    import org.apache.http.impl.io.HttpRequestParser;
    import org.apache.http.io.HttpMessageParser;
    import org.apache.http.io.SessionInputBuffer;
    import org.apache.http.message.BasicHttpEntityEnclosingRequest;
    import org.apache.http.message.BasicLineParser;
    import org.apache.http.params.BasicHttpParams;
    
    import java.io.ByteArrayInputStream;
    import java.io.IOException;
    
    /**
     *
     */
    public class ApacheRequestFactory {
        public static HttpRequest create(final String requestAsString) {
            try {
                SessionInputBuffer inputBuffer = new AbstractSessionInputBuffer() {
                    {
                        init(new ByteArrayInputStream(requestAsString.getBytes()), 10, new BasicHttpParams());
                    }
    
                    @Override
                    public boolean isDataAvailable(int timeout) throws IOException {
                        throw new RuntimeException("have to override but probably not even called");
                    }
                };
                HttpMessageParser parser = new HttpRequestParser(inputBuffer, new BasicLineParser(new ProtocolVersion("HTTP", 1, 1)), new DefaultHttpRequestFactory(), new BasicHttpParams());
                HttpMessage message = parser.parse();
                if (message instanceof BasicHttpEntityEnclosingRequest) {
                    BasicHttpEntityEnclosingRequest request = (BasicHttpEntityEnclosingRequest) message;
                    EntityDeserializer entityDeserializer = new EntityDeserializer(new LaxContentLengthStrategy());
                    HttpEntity entity = entityDeserializer.deserialize(inputBuffer, message);
                    request.setEntity(entity);
                }
                return (HttpRequest) message;
            } catch (IOException e) {
                throw new RuntimeException(e);
            } catch (HttpException e) {
                throw new RuntimeException(e);
            }
        }
    }
    

    and a test class showing how to use it:

    import org.apache.http.HttpRequest;
    import org.apache.http.NameValuePair;
    import org.apache.http.client.utils.URLEncodedUtils;
    import org.apache.http.message.BasicHttpEntityEnclosingRequest;
    import org.junit.Test;
    
    import java.io.IOException;
    import java.net.URI;
    import java.util.List;
    
    import static org.junit.Assert.*;
    
    /**
     *
     */
    public class ApacheRequestFactoryTest {
        @Test
        public void testGet() {
            String requestString = "GET /?one=aone&two=atwo HTTP/1.1\n" +
                    "Host: localhost:7788\n" +
                    "Connection: Keep-Alive\n" +
                    "User-Agent: Apache-HttpClient/4.0.1 (java 1.5)";
    
            HttpRequest request = ApacheRequestFactory.create(requestString);
            assertEquals("GET", request.getRequestLine().getMethod());
            List<NameValuePair> pairs = URLEncodedUtils.parse(URI.create(request.getRequestLine().getUri()), "ISO-8859-1");
            checkPairs(pairs);
        }
    
        @Test
        public void testPost() throws IOException {
            String requestString = "POST / HTTP/1.1\n" +
                    "Content-Length: 17\n" +
                    "Content-Type: application/x-www-form-urlencoded; charset=ISO-8859-1\n" +
                    "Host: localhost:7788\n" +
                    "Connection: Keep-Alive\n" +
                    "User-Agent: Apache-HttpClient/4.0.1 (java 1.5)\n" +
                    "\n" +
                    "one=aone&two=atwo";
    
            HttpRequest request = ApacheRequestFactory.create(requestString);
            assertEquals("POST", request.getRequestLine().getMethod());
            List<NameValuePair> pairs = URLEncodedUtils.parse(((BasicHttpEntityEnclosingRequest)request).getEntity());
            checkPairs(pairs);
        }
    
        private void checkPairs(List<NameValuePair> pairs) {
            for (NameValuePair pair : pairs) {
                if (pair.getName().equals("one")) assertEquals("aone", pair.getValue());
                else if (pair.getName().equals("two")) assertEquals("atwo", pair.getValue());
                else assertTrue("got more parameters than expected:"+pair.getName(), false);
            }
        }
    }
    

    And a small rant:

    WHAT ARE THE APACHE HTTP TEAM THINKING ? The api is incredibly awkward to use. Developers around the world are wasting time writing wrapper and conversion classes for what should be run of the mill every day usage (like this example the simple act of converting a string to an apache http request, and the bizarre way you need to extract the form parameters (also having to do it in two different ways depending on what type of request was made)). The global time wasted because of this is huge. When you write an API from the bottom up, starting with the specs, you MUST then start a layer from the top down (top being an interface where you can get typical work done without having to understand or look at the way the code is implemented), making every day usage of the library CONVENIENT and intuitive. Apache http libraries are anything but. It’s almost a miracle that its the standard library for this type of task.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string that contains a url which looks like this: http://www.test.com/images/tony 's
I have a string that contains both double-quotes and backslashes that I want to
I have a string that contains a czech character. the string is 0bálka This
I have PHP variable that contains string like: http://domain.com/uploads/image1.jpg|||http://domain.com/uploads/image2.jpg|||http://domain.com/uploads/image3.jpg|||... I need to get first
I have a string that contains some unicode, how do I convert it to
I have a string that contains the representation of a date. It looks like:
I have a string that contains a known number of double values. What's the
If I have a string that contains a url (for examples sake, we'll call
Suppose I have a string that contains Ü. How would I find all those
If I have a string that contains the html from a page I just

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.