import org.apache.commons.httpclient.methods.ByteArrayRequestEntity;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.params.HttpMethodParams;
import com.vness.common.logger.writeLog;
/*
* author:v.tone
* time:2012.05
*/
public class VnHttpConnector {
public static final String METHOD_TYPE_GET = "GET";
public static final String METHOD_TYPE_POST = "POST";
public static final String RET_HEAD = "HEAD";
public static final String RET_STATUS = "STATUS";
public static final String RET_MESSAGE = "MESSAGE";
public static Map
int timeout, JSONObject header) throws HttpException, IOException {
timeout = timeout * 1000;
Map
HttpClient client = new HttpClient();
client.getParams().setSoTimeout(timeout);
client.getHttpConnectionManager().getParams().setConnectionTimeout(
timeout);
writeLog.write("dispatch_","request mothed type:"+methodType);
HttpMethod method = null;
try {
if (METHOD_TYPE_GET.equals(methodType)) {
// GET
GetMethod get = new GetMethod(url);
if (null != data && data.length > 0) {
get.setQueryString(new String(data));
}
method = get;
} else {
PostMethod post = new PostMethod(url);
if (null != data && data.length > 0) {
ByteArrayRequestEntity entity = new ByteArrayRequestEntity(
data);
post.setRequestEntity(entity);
}
method = post;
}
writeLog.write("dispatch_","request data."+ new String(data));
method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
new DefaultHttpMethodRetryHandler(0, false));
if (null != header) {
// HTTP HEADER
Iterator< > i = header.keySet().iterator();
String key = null;
String value = null;
while (i.hasNext()) {
key = (String) i.next();
value = (String) header.get(key);
method.setRequestHeader(key, value);
}
}
writeLog.write("dispatch_","will execute send.");
int status = client.executeMethod(method);
writeLog.write("dispatch_","dispatcher return code:"+status);
response.put(RET_STATUS, status);
response.put(RET_MESSAGE, method.getResponseBody());
response.put(RET_HEAD, header2Map(method.getResponseHeaders()));
} finally {
if (null != method) {
method.releaseConnection();
}
client = null;
}
return response;
}
public static Map< , > request(String url, byte[] data, String methodType,
int timeout, String jsonFmtHeaders) throws HttpE