httpClient发送post请求
来源:互联网 发布:淘宝网防晒服 编辑:程序博客网 时间:2024/06/02 12:39
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;
}
}
- Httpclient发送POST请求
- httpClient发送post请求
- HttpClient发送post请求
- httpclient发送post请求
- HttpClient 发送 POST 请求
- httpclient发送post请求
- httpClient发送post请求
- httpclient 发送post请求
- 用HttpClient发送post请求
- 使用HttpClient发送POST请求
- 使用httpclient发送post请求
- java httpclient发送post get 请求
- HttpClient发送get和post请求
- android使用apache httpclient发送post请求
- 使用HttpClient发送Post Http请求
- 使用HttpClient发送Post Http请求
- HttpClient构造post请求,发送一串字符串
- 使用Apache HttpClient发送get、post请求
- 数理逻辑:命题逻辑(2)命题逻辑公式
- 客户区和非客户区指的什么?窗口客户区和视图客户区的区别
- lotus中Excel的导出
- 正则表达式_百科
- 修正dedecms无法生成联动类别数据的js文件
- httpClient发送post请求
- 数理逻辑:命题逻辑(3)永真公式与可满足公式
- 设计响应式导航菜单的五大法则
- Android3.1后Boot_COMPLETED广播收不到的问题及解决办法
- 提高C#编程水平的50个要点
- 数理逻辑:命题逻辑(4)等效关系
- 外交部:密切关注日美夺岛军演保留反应权利-钓鱼岛问题-夺岛军演-中国军舰
- SharePoint 2010 master page 控件介绍:ribbon
- C#桌面歌词原理