Created
March 20, 2013 13:58
-
-
Save bouzuya/5204840 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?xml version="1.0" encoding="UTF-8" standalone="no"?> | |
| <project basedir="." name="HttpClientExample" xmlns:ivy="antlib:org.apache.ivy.ant"> | |
| <target name="retrieve"> | |
| <ivy:retrieve /> | |
| </target> | |
| </project> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package info.bouzuya.http_client; | |
| import java.io.IOException; | |
| import java.util.ArrayList; | |
| import java.util.HashMap; | |
| import java.util.List; | |
| import java.util.Map; | |
| import org.apache.commons.httpclient.DefaultHttpMethodRetryHandler; | |
| import org.apache.commons.httpclient.HttpClient; | |
| import org.apache.commons.httpclient.HttpException; | |
| import org.apache.commons.httpclient.NameValuePair; | |
| import org.apache.commons.httpclient.methods.GetMethod; | |
| import org.apache.commons.httpclient.methods.PostMethod; | |
| import org.apache.commons.httpclient.params.HttpMethodParams; | |
| public class HttpClientExample { | |
| private static final int RESPONSE_BODY_MAX_LENGTH = 100 * 1024; | |
| private static final String URI = "http://foobar/json"; | |
| private static final boolean DONT_USE_RETRY = true; | |
| public static void main(String[] args) throws HttpException, IOException { | |
| Map<String, String> parameters = new HashMap<String, String>(); | |
| parameters.put("name1", "value1"); | |
| parameters.put("name2", "value2"); | |
| sendHttpGet(parameters); | |
| sendHttpPost(parameters); | |
| } | |
| private static void sendHttpPost(Map<String, String> parameters) | |
| throws IOException, HttpException { | |
| HttpClient client = new HttpClient(); | |
| PostMethod method = new PostMethod(URI); | |
| if (DONT_USE_RETRY) { | |
| method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, | |
| new DefaultHttpMethodRetryHandler(0, false)); | |
| } | |
| NameValuePair[] pairs = toNameValuePairs(parameters); | |
| method.addParameters(pairs); | |
| int code = client.executeMethod(method); | |
| String body = method.getResponseBodyAsString(RESPONSE_BODY_MAX_LENGTH); | |
| System.out.println("Response Code: " + Integer.toString(code)); | |
| System.out.println("Response Body: " + body); | |
| } | |
| private static void sendHttpGet(Map<String, String> parameters) | |
| throws IOException, HttpException { | |
| HttpClient client = new HttpClient(); | |
| GetMethod method = new GetMethod(URI); | |
| if (DONT_USE_RETRY) { | |
| method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, | |
| new DefaultHttpMethodRetryHandler(0, false)); | |
| } | |
| NameValuePair[] pairs = toNameValuePairs(parameters); | |
| method.setQueryString(pairs); | |
| int code = client.executeMethod(method); | |
| String body = method.getResponseBodyAsString(RESPONSE_BODY_MAX_LENGTH); | |
| System.out.println("Response Code: " + Integer.toString(code)); | |
| System.out.println("Response Body: " + body); | |
| } | |
| private static NameValuePair[] toNameValuePairs( | |
| Map<String, String> parameters) { | |
| List<NameValuePair> paramsList = new ArrayList<NameValuePair>(); | |
| for (Map.Entry<String, String> entry : parameters.entrySet()) { | |
| paramsList.add(new NameValuePair(entry.getKey(), entry.getValue())); | |
| } | |
| NameValuePair[] paramsArray = paramsList.toArray(new NameValuePair[0]); | |
| return paramsArray; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?xml version="1.0" encoding="UTF-8"?> | |
| <ivy-module version="2.0" | |
| xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | |
| xsi:noNamespaceSchemaLocation="http://ant.apache.org/ivy/schemas/ivy.xsd" | |
| xmlns:e="http://ant.apache.org/ivy/extra"> | |
| <info organisation="info.bouzuya" module="info.bouzuya.http_client" /> | |
| <dependencies> | |
| <dependency org="commons-httpclient" name="commons-httpclient" | |
| rev="3.1" /> | |
| </dependencies> | |
| </ivy-module> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment