//请求发送工具类
public class HttpClientUtil {
private static HttpClient httpClient = new HttpClient();
private static Logger log = Logger.getLogger(HttpClientUtil.class);
public static String doPost(String url, NameValuePair[] postData) {
PostMethod method = new PostMethod(url);
method.setRequestBody(postData);
int statusCode = 0;
String response = null;
try {
statusCode = httpClient.executeMethod(method);
if (statusCode == HttpStatus.SC_OK) {
response = new String(method.getResponseBodyAsString().getBytes("GBK"));
}
} catch (Exception e) {
e.printStackTrace();
log.error(e, e.fillInStackTrace());
} finally {
method.releaseConnection();
}
return response;
}
}
=====================================================================
//发送方法
public String senda2b(JsonObject jsonObject){
String currentTime = getCurrentTimeString();
List<NameValuePair> postData = new ArrayList<NameValuePair>();
postData.add(new NameValuePair("venderId", "123456"));
postData.add(new NameValuePair("timestamp", currentTime));
...
if(jsonObject != null){
for(Entry<String,JsonElement> entry: jsonObject.entrySet()){
if(!entry.getValue().isJsonObject()){//判断获取的值是不是JsonOb类型的
postData.add(new NameValuePair(entry.getKey(), entry.getValue().getAsString()));
}else{
postData.add(new NameValuePair(entry.getKey(), entry.getValue().toString().replaceAll("\\{|\\}", "\"")));
}
}
}
String response = HttpClientUtil.doPost(url, (NameValuePair[])postData.toArray(new NameValuePair[0]));
if(response == null){
response = "request error: method: "+method;
}
return response;
}
public String getCurrentTimeString() {
Date nowDate=new Date(System.currentTimeMillis());
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
return sdf.format(nowDate);
}
分享到:
相关推荐
HTTP发送POST请求的工具类
http请求工具类 常见的post get请求
Java发送http请求、https请求,post和get都支持
java发送http请求的工具类,常用工具类,避免每次都要去网上查
这是一个java发送get、post请求,并得到返回结果的工具类。
用于发送http请求的一个简单的工具类,实现比较简单,可以参考。
Java发 Https请求工具类 支持SSL。 自定义请求方式 GET POST... 微信自用, 发Https请求工具类,支持SSL SSL Https
HttpClientUtil工具类发送get和post请求,支持http和https,支持发送文件
下面小编就为大家带来一篇HttpUtils 发送http请求工具类(实例讲解)。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
主要介绍了C#实现发送简单HTTP请求的方法,涉及C#操作http的技巧,具有一定参考借鉴价值,需要的朋友可以参考下
封装了https发送Get和Post请求工的具类。直接看Demo例子。样例实现参考 HTTP服务POST方式代码
向指定URL发送GET方法的请求 获取网络图片返回byte数组 向指定 URL 发送POST方法的请求 向指定URL发送POST方法的请求(application/json格式发送数据) 向指定URL发送POST方法的请求(application/xml格式发送数据)...
Java 编写的使用HttpClient发送http请求工具类(解决了session无法保留问题)
用户在开发或者调试网络程序或者是网页B/S模式的程序的时候是需要一些方法来跟踪网页请求的,用户可以使用一些网络的监视工具比如著名的...Postman在发送网络HTTP请求方面可以说是Chrome插件类产品中的代表产品之一。
java的get和post请求,获取json的工具类,https时会存在ssl校验的问题,工具会自动去除ssl校验。
HTTP网络客户端请求基础工具类,可发送常见的GET、POST等请求方式,中文编码已处理,基于SSL连接,依赖的jar包有:commons-lang-2.6.jar、httpclient-4.3.2.jar、httpcore-4.3.1.jar、commons-io-2.4.jar
httpclient get/post请求工具类(map参数封装),方便调用
http请求工具类 用于发送HTTP请求 发送后得到相应结果 并进行解析处理
HttpClient发送http请求(post+get)需要的jar包+内符java代码案例+注解详解(这款工具类是我精心挑选适合广大java程序猿使用)