package com.yystar.sb2ph.utility;
import java.io.IOException;
import java.util.Map;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.params.HttpMethodParams;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
public class HttpUtility {
private static Log log = LogFactory.getLog(HttpUtility.class);
public static String sendPostRequest(String url){
return sendPostRequest(url,null);
}
public static String sendPostRequest(String url,Map<String, String> postParam){
// Create an instance of HttpClient.
HttpClient client = new HttpClient();
client.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET,"utf-8");
// Create a method instance.
PostMethod method = new PostMethod(url);
if(postParam != null && postParam.size() != 0){
for (String param : postParam.keySet()) {
method.setParameter(param, postParam.get(param));
}
}
// Provide custom retry handler is necessary
//method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler(3, false));
try {
// Execute the method.
long time = System.currentTimeMillis();
int statusCode = client.executeMethod(method);
time = System.currentTimeMillis() - time;
log.info("["+url+"] process time: " + time + " ms");
if (statusCode != HttpStatus.SC_OK) {
log.error("Method failed: " + method.getStatusLine());
}
// Read the response body.
byte[] responseBody = method.getResponseBody();
// Deal with the response.
// Use caution: ensure correct character encoding and is not binary
// data
return new String(responseBody);
} catch (HttpException e) {
log.error("Fatal protocol violation: " + e.getMessage());
e.printStackTrace();
} catch (IOException e) {
log.error("Fatal transport error: " + e.getMessage());
e.printStackTrace();
} finally {
// Release the connection.
method.releaseConnection();
}
return null;
}
}
import java.io.IOException;
import java.util.Map;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.params.HttpMethodParams;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
public class HttpUtility {
private static Log log = LogFactory.getLog(HttpUtility.class);
public static String sendPostRequest(String url){
return sendPostRequest(url,null);
}
public static String sendPostRequest(String url,Map<String, String> postParam){
// Create an instance of HttpClient.
HttpClient client = new HttpClient();
client.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET,"utf-8");
// Create a method instance.
PostMethod method = new PostMethod(url);
if(postParam != null && postParam.size() != 0){
for (String param : postParam.keySet()) {
method.setParameter(param, postParam.get(param));
}
}
// Provide custom retry handler is necessary
//method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler(3, false));
try {
// Execute the method.
long time = System.currentTimeMillis();
int statusCode = client.executeMethod(method);
time = System.currentTimeMillis() - time;
log.info("["+url+"] process time: " + time + " ms");
if (statusCode != HttpStatus.SC_OK) {
log.error("Method failed: " + method.getStatusLine());
}
// Read the response body.
byte[] responseBody = method.getResponseBody();
// Deal with the response.
// Use caution: ensure correct character encoding and is not binary
// data
return new String(responseBody);
} catch (HttpException e) {
log.error("Fatal protocol violation: " + e.getMessage());
e.printStackTrace();
} catch (IOException e) {
log.error("Fatal transport error: " + e.getMessage());
e.printStackTrace();
} finally {
// Release the connection.
method.releaseConnection();
}
return null;
}
}