Get提交:
public static String invokeUrl(String url, String param,String charset) {
if(StringUtils.isBlank(charset)){
charset="UTF-8";
}
HttpClient httpClient = new HttpClient();
PostMethod post = null;
String resp = null;
try {
String invokeUrl = url+(StringUtils.isBlank(param)?"":("?"
+ URLEncoder.encode(URLEncoder.encode(param, "UTF-8"),
"UTF-8")));
// 设置超时时间
int connectionTime = 20*1000;
httpClient.getHttpConnectionManager().getParams()
.setConnectionTimeout(connectionTime);
httpClient.getHttpConnectionManager().getParams()
.setSoTimeout(connectionTime);
post = new PostMethod(invokeUrl);
post.setRequestHeader("Content-type", "text/html; charset="+charset);
// 使用系统提供的默认的恢复策略
post.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
new DefaultHttpMethodRetryHandler());
// 执行getMethod
int statusCode = httpClient.executeMethod(post);
if (statusCode != HttpStatus.SC_OK) {
System.err.println("Method failed:" + post.getStatusLine());
}
// 读取内容
InputStream responseBody = post.getResponseBodyAsStream();
resp = IOUtils.toString(responseBody, "GBK");
LOG.warn("resp:" + resp);
} catch (Exception e) {
LOG.error("调用URL异常",e);
} finally {
// 释放连接
if (post != null)
post.releaseConnection();
}
return resp;
}
Post提交:
public static String getPostResponse(String url,NameValuePair[] data) throws Exception {
HttpClient httpClient = new HttpClient();
PostMethod post = null;
String resp = null;
try {
// 设置超时时间
int connectionTime = 120*1000;
httpClient.getHttpConnectionManager().getParams()
.setConnectionTimeout(connectionTime);
httpClient.getHttpConnectionManager().getParams()
.setSoTimeout(connectionTime);
post = new PostMethod(url);
post.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, "UTF-8");
// 使用系统提供的默认的恢复策略
post.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler());
// 设置提交参数
if(data!=null && data.length>0){
post.addParameters(data);
}
// 执行getMethod
int statusCode = httpClient.executeMethod(post);
if (statusCode != HttpStatus.SC_OK) {
System.err.println("Method failed:" + post.getStatusLine());
}
// 读取内容
InputStream responseBody = post.getResponseBodyAsStream();
resp = IOUtils.toString(responseBody, "GBK");
System.out.println("resp:" + resp);
} catch (Exception e) {
e.printStackTrace();
} finally {
// 释放连接
if (post != null)
post.releaseConnection();
}
return resp;
}
提交JSON:
JSONObject jsonParams = new JSONObject();
jsonParams.put("keyIndex", "");
jsonParams.put("encryKey", "");
jsonParams.put("encryStr", "");
jsonParams.put("interCode", "INTER.SYSTEM.001");
String requestBody = jsonParams.toJSONString();
HttpClient httpClient = new HttpClient();
PostMethod post = null;
String resp = "";
try {
post = new PostMethod("https://capi.bestpay.com.cn/common/interface");
//post.setParameter("requestdata",requestBody);
//post.setRequestHeader("Content-type", "application/json; charset=UTF-8");
// 使用系统提供的默认的恢复策略
post.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,new DefaultHttpMethodRetryHandler());
RequestEntity se = new StringRequestEntity(requestBody, "application/json", "UTF-8");
post.setRequestEntity(se);
int statusCode = httpClient.executeMethod(post);
//System.out.println(statusCode);
if (statusCode != HttpStatus.SC_OK) {
System.err.println("Method failed:" + post.getStatusLine());
}
// 读取内容
InputStream responseBody = post.getResponseBodyAsStream();
ret = IOUtils.toString(responseBody, "utf8");
System.out.println(resp);
} catch (Exception e) {
e.printStackTrace();
} finally {
// 释放连接
if (post != null)
post.releaseConnection();
}