public Object executeHttpRequest(String url, Map<String, String> keyValueMap) {c++
HttpClient client = new HttpClient();json
PostMethod postMethod = new PostMethod(url);網絡
try {app
//設置請求參數post
if (keyValueMap != null) {編碼
Iterator it = keyValueMap.entrySet().iterator();url
NameValuePair[] parameters = new NameValuePair[keyValueMap.size()];debug
int c = 0;xml
while (it.hasNext()) {utf-8
Map.Entry<String, String> entry = (Map.Entry<String, String>) it.next();
NameValuePair nvp = new NameValuePair();
nvp.setName(entry.getKey());
nvp.setValue(entry.getValue());
parameters[c] = nvp;
c++;
}
postMethod.addParameters(parameters);
}
log.debug("query uri ===============" + postMethod.getURI());
postMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler());
postMethod.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET,"gbk");
//todo:設置超時時間
postMethod.getParams().setParameter(HttpMethodParams.SO_TIMEOUT, 200000);
int statusCode = client.executeMethod(postMethod);
if (statusCode != HttpStatus.SC_OK) {
log.info("request '" + url + "' failed,the status is not 200,status:" + statusCode);
return "";
}
byte[] responseBody = postMethod.getResponseBody();
return responseBody;
} catch (Exception e) {
log.error("noTopStat 發生異常!請檢查網絡和參數",e);
} finally {
postMethod.releaseConnection();
}
return null;
}
public String executeHttpRequestString(String url, Map<String, String> keyValueMap) {
HttpClient client = new HttpClient();
PostMethod postMethod = new PostMethod(url);
try {
//設置請求參數
if (keyValueMap != null) {
Iterator it = keyValueMap.entrySet().iterator();
NameValuePair[] parameters = new NameValuePair[keyValueMap.size()];
int c = 0;
while (it.hasNext()) {
Map.Entry<String, String> entry = (Map.Entry<String, String>) it.next();
NameValuePair nvp = new NameValuePair();
nvp.setName(entry.getKey());
nvp.setValue(entry.getValue());
parameters[c] = nvp;
c++;
}
postMethod.addParameters(parameters);
}
log.debug("query uri ===============" + postMethod.getURI());
postMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler());
postMethod.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, "gbk");
postMethod.getParams().setParameter(HttpMethodParams.SO_TIMEOUT, 200000);
int statusCode = client.executeMethod(postMethod);
if (statusCode != HttpStatus.SC_OK) {
log.info("request '" + url + "' failed,the status is not 200,status:" + statusCode);
return "";
}
String responseBody = postMethod.getResponseBodyAsString();
return responseBody;
} catch (Exception e) {
log.error("發生異常!請檢查網絡和參數", e);
} finally {
postMethod.releaseConnection();
}
return null;
}
public String executeHttpRequestByGetType(String url, Map<String, String> keyValueMap) {
HttpClient client = new HttpClient();
StringBuffer sb = new StringBuffer(url);
PostMethod postMethod = null;
try {
//設置請求參數
if (keyValueMap != null) {
Iterator it = keyValueMap.entrySet().iterator();
if (keyValueMap.size() > 0) {
sb.append("?");
while (it.hasNext()) {
Map.Entry<String, String> entry = (Map.Entry<String, String>) it.next();
sb.append(entry.getKey() + "=" + entry.getValue() + "&");
}
sb.deleteCharAt(sb.length() - 1);
}
}
log.debug("query uri ===============" + sb.toString());
postMethod = new PostMethod(sb.toString());
log.debug("query uri ===============" + postMethod.getURI());
postMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler());
postMethod.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, "gbk");
postMethod.getParams().setParameter(HttpMethodParams.SO_TIMEOUT, 200000);
int statusCode = client.executeMethod(postMethod);
if (statusCode != HttpStatus.SC_OK) {
log.info("request '" + url + "' failed,the status is not 200,status:" + statusCode);
return "";
}
String responseBody = postMethod.getResponseBodyAsString();
return responseBody;
} catch (Exception e) {
log.error("發生異常!請檢查網絡和參數", e);
} finally {
if (postMethod != null) {
postMethod.releaseConnection();
}
}
return null;
}
public String executeHttpRequestByGet(String url, Map<String, String> keyValueMap) {
HttpClient client = new HttpClient();
GetMethod getMethod = new GetMethod(url);
try {
//設置請求參數
if (keyValueMap != null) {
Iterator it = keyValueMap.entrySet().iterator();
NameValuePair[] parameters = new NameValuePair[keyValueMap.size()];
int c = 0;
while (it.hasNext()) {
Map.Entry<String, String> entry = (Map.Entry<String, String>) it.next();
NameValuePair nvp = new NameValuePair();
nvp.setName(entry.getKey());
nvp.setValue(entry.getValue());
parameters[c] = nvp;
c++;
}
}
log.debug("query uri ===============" + getMethod.getURI());
getMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler());
getMethod.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET,"utf-8");
getMethod.getParams().setParameter(HttpMethodParams.SO_TIMEOUT, 3000);
int statusCode = client.executeMethod(getMethod);
if (statusCode != HttpStatus.SC_OK) {
log.info("request '" + url + "' failed,the status is not 200,status:" + statusCode);
return "";
}
String responseBody = getMethod.getResponseBodyAsString();
return responseBody;
} catch (Exception e) {
log.error("noTopStat 發生異常!請檢查網絡和參數",e);
} finally {
getMethod.releaseConnection();
}
return null;
}
public String executeHttpRequestTuanGou(String url,String requestContent) {
PostMethod postMethod = new PostMethod(url);//請求的action
int soTimeOut = 5000000; //超時
String charSet = "utf-8"; //編碼
try {
String str = "";//返回的字符串
// /傳入的xml或json字符串,不爲空
HttpClient client = new HttpClient();
postMethod.setRequestEntity(new StringRequestEntity(requestContent, "text/xml; charset=" + charSet, charSet));
postMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler());
postMethod.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, charSet);
postMethod.getParams().setParameter(HttpMethodParams.SO_TIMEOUT, soTimeOut);
int statusCode = client.executeMethod(postMethod);
BufferedReader in = new BufferedReader(new InputStreamReader(postMethod.getResponseBodyAsStream(), charSet));
StringBuffer sb = new StringBuffer();
String line;
while ((line = in.readLine()) != null) {
sb.append(line);
}
in.close();
//返回碼不是200
if (statusCode != HttpStatus.SC_OK) {
return null ;
}
str = sb.toString();
return str;
} catch (Exception e) {
} finally {
postMethod.releaseConnection();
}
return "";
}