HttpClientUtil发送json格式请求
2017-04-10 10:23
483 查看
package http;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import net.sf.json.JSONObject;
import util.Base64;
import org.apache.commons.httpclient.params.HttpMethodParams;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.apache.log4j.Logger;
/*
* 利用HttpClient进行post请求的工具类
*/
public class HttpClientUtil {
private static final Logger logger = Logger.getLogger(HttpClientUtil.class);
static{
try {
System.setProperty("jsse.enableSNIExtension", "false");
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 发送post请求
* @param url 请求路径
* @param param 请求json数据
* @return
*/
public static String doPost(String url,JSONObject param){
HttpPost httpPost = null;
String result = null;
try{
HttpClient client = new SSLClient();
httpPost = new HttpPost(url);
if(param != null){
StringEntity se = new StringEntity(param.toString(),"utf-8");
// se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json; charset="));
httpPost.setEntity(se); //post方法中,加入json数据
httpPost.setHeader("Content-Type","application/json");
}
HttpResponse response = client.execute(httpPost);
if(response != null){
HttpEntity resEntity = response.getEntity();
if(resEntity != null){
result = EntityUtils.toString(resEntity,"utf-8");
}
}
}catch(Exception ex){
ex.printStackTrace();
}
System.out.println("返回结果:\n"+result);
return result;
}
/**
* 发送post请求
* @param url 请求路径
* @param jsonparam 请求json数据字符串
* @return
*/
public static String taikangPost(String url,String jsonparam){
HttpPost httpPost = null;
String result = null;
try{
HttpClient client = new SSLClient();
httpPost = new HttpPost(url);
logger.info("请求路径:\n"+url);
logger.info("请求参数:\n"+jsonparam);
if(jsonparam != null){
jsonparam = Base64.encodeGBK(jsonparam.getBytes("GBK"));
StringEntity se = new StringEntity(jsonparam,"GBK");
httpPost.setEntity(se); //post方法中,加入json数据
httpPost.setHeader("Content-Type","application/text");
}
HttpResponse response = client.execute(httpPost);
if(response != null){
HttpEntity resEntity = response.getEntity();
if(resEntity != null){
result = EntityUtils.toString(resEntity,"GBK");
logger.info("返回结果原始字符串:\n"+result);
result = Base64.decode(result,"GBK");
}
}
}catch(Exception ex){
ex.printStackTrace();
}
logger.info("返回结果:\n"+result);
return result;
}
/**
* 发送post请求
* @param url 请求路径
* @param param 请求参数数据
* @return
*/
@SuppressWarnings("unchecked")
public static String doPostMap(String url,Map<String, String> map){
HttpClient httpClient = null;
HttpPost httpPost = null;
String result = null;
try{
httpClient = new SSLClient();
httpPost = new HttpPost(url);
//设置参数
List<NameValuePair> list = new ArrayList<NameValuePair>();
Iterator iterator = map.entrySet().iterator();
while(iterator.hasNext()){
Entry<String,String> elem = (Entry<String, String>) iterator.next();
list.add(new BasicNameValuePair(elem.getKey(),elem.getValue()));
}
if(list.size() > 0){
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(list,"UTF-8");
httpPost.setEntity(entity);
}
HttpResponse response = httpClient.execute(httpPost);
if(response != null){
HttpEntity resEntity = response.getEntity();
if(resEntity != null){
result = EntityUtils.toString(resEntity,"UTF-8");
}
}
}catch(Exception ex){
ex.printStackTrace();
}
logger.info("返回结果"+result);
return result;
}
/**
* 发送get请求
* @param url 请求路径
* @param param 请求json数据
* @return
*/
public static String doGet(String url){
String result = null;
try{
HttpClient client = new SSLClient();
//用get方法发送http请求
HttpGet get = new HttpGet(url);
CloseableHttpResponse httpResponse = null;
//发送get请求
httpResponse = (CloseableHttpResponse) client.execute(get);
try{
//response实体
HttpEntity entity = httpResponse.getEntity();
if (null != entity){
result = EntityUtils.toString(entity,"utf-8");
}
}
finally{
httpResponse.close();
}
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
public static void main(String[] args) {
JSONObject obj = new JSONObject();
obj.element("a", 1);
// HttpClientUtil.doPost("http://180.168.131.15/cpf/tianan_cpf/access/car/queryCarModel.mvc",obj);
HttpClientUtil.taikangPost("http://ecuat.tk.cn/tkcoop_zz/service/proposalEntrance/proposalCreateEntrance?sign=ehaitun&comboid=1007A00901&fromId=62667",obj.toString());
}
}
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import net.sf.json.JSONObject;
import util.Base64;
import org.apache.commons.httpclient.params.HttpMethodParams;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.apache.log4j.Logger;
/*
* 利用HttpClient进行post请求的工具类
*/
public class HttpClientUtil {
private static final Logger logger = Logger.getLogger(HttpClientUtil.class);
static{
try {
System.setProperty("jsse.enableSNIExtension", "false");
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 发送post请求
* @param url 请求路径
* @param param 请求json数据
* @return
*/
public static String doPost(String url,JSONObject param){
HttpPost httpPost = null;
String result = null;
try{
HttpClient client = new SSLClient();
httpPost = new HttpPost(url);
if(param != null){
StringEntity se = new StringEntity(param.toString(),"utf-8");
// se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json; charset="));
httpPost.setEntity(se); //post方法中,加入json数据
httpPost.setHeader("Content-Type","application/json");
}
HttpResponse response = client.execute(httpPost);
if(response != null){
HttpEntity resEntity = response.getEntity();
if(resEntity != null){
result = EntityUtils.toString(resEntity,"utf-8");
}
}
}catch(Exception ex){
ex.printStackTrace();
}
System.out.println("返回结果:\n"+result);
return result;
}
/**
* 发送post请求
* @param url 请求路径
* @param jsonparam 请求json数据字符串
* @return
*/
public static String taikangPost(String url,String jsonparam){
HttpPost httpPost = null;
String result = null;
try{
HttpClient client = new SSLClient();
httpPost = new HttpPost(url);
logger.info("请求路径:\n"+url);
logger.info("请求参数:\n"+jsonparam);
if(jsonparam != null){
jsonparam = Base64.encodeGBK(jsonparam.getBytes("GBK"));
StringEntity se = new StringEntity(jsonparam,"GBK");
httpPost.setEntity(se); //post方法中,加入json数据
httpPost.setHeader("Content-Type","application/text");
}
HttpResponse response = client.execute(httpPost);
if(response != null){
HttpEntity resEntity = response.getEntity();
if(resEntity != null){
result = EntityUtils.toString(resEntity,"GBK");
logger.info("返回结果原始字符串:\n"+result);
result = Base64.decode(result,"GBK");
}
}
}catch(Exception ex){
ex.printStackTrace();
}
logger.info("返回结果:\n"+result);
return result;
}
/**
* 发送post请求
* @param url 请求路径
* @param param 请求参数数据
* @return
*/
@SuppressWarnings("unchecked")
public static String doPostMap(String url,Map<String, String> map){
HttpClient httpClient = null;
HttpPost httpPost = null;
String result = null;
try{
httpClient = new SSLClient();
httpPost = new HttpPost(url);
//设置参数
List<NameValuePair> list = new ArrayList<NameValuePair>();
Iterator iterator = map.entrySet().iterator();
while(iterator.hasNext()){
Entry<String,String> elem = (Entry<String, String>) iterator.next();
list.add(new BasicNameValuePair(elem.getKey(),elem.getValue()));
}
if(list.size() > 0){
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(list,"UTF-8");
httpPost.setEntity(entity);
}
HttpResponse response = httpClient.execute(httpPost);
if(response != null){
HttpEntity resEntity = response.getEntity();
if(resEntity != null){
result = EntityUtils.toString(resEntity,"UTF-8");
}
}
}catch(Exception ex){
ex.printStackTrace();
}
logger.info("返回结果"+result);
return result;
}
/**
* 发送get请求
* @param url 请求路径
* @param param 请求json数据
* @return
*/
public static String doGet(String url){
String result = null;
try{
HttpClient client = new SSLClient();
//用get方法发送http请求
HttpGet get = new HttpGet(url);
CloseableHttpResponse httpResponse = null;
//发送get请求
httpResponse = (CloseableHttpResponse) client.execute(get);
try{
//response实体
HttpEntity entity = httpResponse.getEntity();
if (null != entity){
result = EntityUtils.toString(entity,"utf-8");
}
}
finally{
httpResponse.close();
}
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
public static void main(String[] args) {
JSONObject obj = new JSONObject();
obj.element("a", 1);
// HttpClientUtil.doPost("http://180.168.131.15/cpf/tianan_cpf/access/car/queryCarModel.mvc",obj);
HttpClientUtil.taikangPost("http://ecuat.tk.cn/tkcoop_zz/service/proposalEntrance/proposalCreateEntrance?sign=ehaitun&comboid=1007A00901&fromId=62667",obj.toString());
}
}
相关文章推荐
- 通过httpClient发送json格式数据请求
- Ajax核心XMLHttpRequest对象、(发送请求、接收)方法和属性介绍、AJAX开发框架、数据格式提要(XML、JSON、HTML)
- java代码发送JSON格式的httpPOST请求
- http请求远程接口获得Json数据(2) --- 利用HttpClient发送post/get请求
- asynchttpclient的post方法发送json格式参数和表单格式参数
- HttpClient 发送get请求并返回Json数据
- Ajax核心XMLHttpRequest对象、(发送请求、接收)方法和属性介绍、AJAX开发框架、数据格式提要(XML、JSON、HTML)
- C# HttpWebRequest\HttpWebResponse\WebClient发送请求解析json数据
- Ajax核心XMLHttpRequest对象、(发送请求、接收)方法和属性介绍、AJAX开发框架、数据格式提要(XML、JSON、HTML)
- java代码发送JSON格式的httpPOST请求
- http发送json格式数据请求 demo示例
- java发送JSON格式的http通讯的post请求
- java httpclient发送json 请求 ,go服务端接收
- 使用apache的httpclient发送post请求传送和解析json
- Ajax核心XMLHttpRequest对象、(发送请求、接收)方法和属性介绍、AJAX开发框架、数据格式提要(XML、JSON、HTML)
- HttpClient发送get请求,post请求,携带cookie访问,json提交
- (转)java代码发送JSON格式的httpPOST请求
- httpClient 发送 post请求 json数据
- httpclient的post请求并处理json格式响应数据
- java代码发送JSON格式的httpPOST请求