Class for converting a string to an 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 testing 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 little flatulence:
WHAT IS APACHE HTTP MATERIAL? Api is incredibly inconvenient to use. Developers around the world spend time creating shells and conversion classes for what needs to be run on the mill every day (for example, this simple example of converting a string to an apache HTTP request and a strange way to extract form parameters (you also need to do this in two different ways in depending on what type of request was made)). Because of this, global time is wasted. When you write the API from the bottom up, starting with the specifications, you MUST start the layer from top to bottom (the top is the interface where you can get a typical job without having to understand or look at how the code is implemented), making daily use of the CONVENIENT library and intuitive. Apache http libraries are nothing more than. It is almost a miracle that its standard library for this type of task.
source share