Skip to content

Instantly share code, notes, and snippets.

@bouzuya
Created March 28, 2013 22:03
Show Gist options
  • Select an option

  • Save bouzuya/5267222 to your computer and use it in GitHub Desktop.

Select an option

Save bouzuya/5267222 to your computer and use it in GitHub Desktop.
package info.bouzuya.http_client;
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.HttpMethodBase;
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 {
public static interface Api<T> {
public String getMethod();
public String getPath();
public Map<String, String> getParameters();
public void setParameters(Map<String, String> parameters);
public T handleResponse(int code, String body);
}
// post /json?k=...&p1=...&p2=...
public static class JsonApi implements Api<String> {
private static final String method = "post";
private static final String pathFormat = "/json?k=%s";
private final String pathParameter;
private Map<String, String> parameters;
public JsonApi(String p1) {
this.pathParameter = p1;
}
@Override
public String getMethod() {
return method;
}
@Override
public void setParameters(Map<String, String> parameters) {
this.parameters = parameters; // FIXME
}
@Override
public Map<String, String> getParameters() {
return this.parameters; // FIXME
}
@Override
public String getPath() {
return String.format(pathFormat, pathParameter);
}
@Override
public String handleResponse(int code, String body) {
System.out.println("Response Code: " + Integer.toString(code));
System.out.println("Response Body: " + body);
return body;
}
}
public static class Client {
private static final int RESPONSE_BODY_MAX_LENGTH = 100 * 1024;
private static final boolean DONT_USE_RETRY = true;
private static final int port = 3000;
private final String protocol;
private final String host;
private final String p1;
public Client(String protocol, String host, String p1) {
this.protocol = protocol;
this.host = host;
this.p1 = p1;
}
public String json(String p1, String p2) throws Exception {
Api<String> api = new JsonApi(this.p1);
Map<String, String> parameters = new HashMap<String, String>();
parameters.put("p1", p1);
parameters.put("p2", p2);
api.setParameters(parameters);
return call(api);
}
private <T> T call(Api<T> api) throws Exception {
String url = protocol + "://" + host + ":" + Integer.toString(port)
+ api.getPath();
HttpClient client = new HttpClient();
HttpMethodBase method = newHttpMethod(api.getMethod(), url, api
.getParameters());
if (DONT_USE_RETRY) {
method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
new DefaultHttpMethodRetryHandler(0, false));
}
int code = client.executeMethod(method);
String body = method
.getResponseBodyAsString(RESPONSE_BODY_MAX_LENGTH);
return api.handleResponse(code, body);
}
private <T> HttpMethodBase newHttpMethod(String method, String url,
Map<String, String> parameters) {
if (method.equals("post")) {
PostMethod m = new PostMethod(url);
NameValuePair[] pairs = toNameValuePairs(parameters);
m.addParameters(pairs);
return m;
} else if (method.equals("get")) {
GetMethod m = new GetMethod(url);
NameValuePair[] pairs = toNameValuePairs(parameters);
m.setQueryString(pairs);
return m;
} else {
throw new IllegalArgumentException();
}
}
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;
}
}
public static void main(String[] args) throws Exception {
Client client = new Client("http", "www.bouzuya.info", "kkk");
String result = client.json("v1", "v2");
System.out.println(result);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment