JAVA HttpClient(普通参数、JSON格式参数)
2017-08-09 19:52
447 查看
完整示例:点击下载
代码如下:
import com.alibaba.fastjson.JSONObject;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
/**
* HttpClient客户端
* Created by itdragons on 2017-8-8.
*/
public class HttpClient {
/**
* post请求-json参数
*/
public static String postHttp(String url, JSONObject paramJson) {
return postHttp(url, new StringEntity(paramJson.toString(),"UTF-8"));
}
/**
* post请求-参数
*/
public static String postHttp(String url, Map<String, String> paramMap) throws UnsupportedEncodingException {
List<NameValuePair> params = new ArrayList<NameValuePair>();
if(paramMap != null){
for(Map.Entry<String,String> entry : paramMap.entrySet()){
params.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
}
}
return postHttp(url, new UrlEncodedFormEntity(params, "UTF-8"));
}
/**
* post请求
*/
private static String postHttp(String url, HttpEntity formEntity) {
CloseableHttpClient client = null;
HttpPost post = null;
String result = "";
try {
client = HttpClients.createDefault();
//HttpClient httpClient = new DefaultHttpClient();
post = new HttpPost(url);
post.setEntity(formEntity);
HttpResponse resp = client.execute(post);
HttpEntity entity = resp.getEntity();
result = EntityUtils.toString(entity, "UTF-8");
} catch (Exception e) {
e.printStackTrace();
} finally {
//关闭连接,释放资源
//httpClient.getConnectionManager().shutdown();httpClient = null;
if (post != null) {
post.releaseConnection();
}
if (client != null) {
try {
client.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return result;
}
/**
* get请求
*/
public static String getHttp(String url) {
CloseableHttpClient client = null;
HttpGet get = null;
String result = "";
try {
client = HttpClients.createDefault();
get = new HttpGet(url);
HttpResponse resp = client.execute(get);
HttpEntity entity = resp.getEntity();
result = EntityUtils.toString(entity, "UTF-8");
} catch (Exception e) {
e.printStackTrace();
} finally {
//关闭连接,释放资源
if (get != null) {
get.releaseConnection();
}
if (client != null) {
try {
client.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return result;
}
}
代码如下:
import com.alibaba.fastjson.JSONObject;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
/**
* HttpClient客户端
* Created by itdragons on 2017-8-8.
*/
public class HttpClient {
/**
* post请求-json参数
*/
public static String postHttp(String url, JSONObject paramJson) {
return postHttp(url, new StringEntity(paramJson.toString(),"UTF-8"));
}
/**
* post请求-参数
*/
public static String postHttp(String url, Map<String, String> paramMap) throws UnsupportedEncodingException {
List<NameValuePair> params = new ArrayList<NameValuePair>();
if(paramMap != null){
for(Map.Entry<String,String> entry : paramMap.entrySet()){
params.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
}
}
return postHttp(url, new UrlEncodedFormEntity(params, "UTF-8"));
}
/**
* post请求
*/
private static String postHttp(String url, HttpEntity formEntity) {
CloseableHttpClient client = null;
HttpPost post = null;
String result = "";
try {
client = HttpClients.createDefault();
//HttpClient httpClient = new DefaultHttpClient();
post = new HttpPost(url);
post.setEntity(formEntity);
HttpResponse resp = client.execute(post);
HttpEntity entity = resp.getEntity();
result = EntityUtils.toString(entity, "UTF-8");
} catch (Exception e) {
e.printStackTrace();
} finally {
//关闭连接,释放资源
//httpClient.getConnectionManager().shutdown();httpClient = null;
if (post != null) {
post.releaseConnection();
}
if (client != null) {
try {
client.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return result;
}
/**
* get请求
*/
public static String getHttp(String url) {
CloseableHttpClient client = null;
HttpGet get = null;
String result = "";
try {
client = HttpClients.createDefault();
get = new HttpGet(url);
HttpResponse resp = client.execute(get);
HttpEntity entity = resp.getEntity();
result = EntityUtils.toString(entity, "UTF-8");
} catch (Exception e) {
e.printStackTrace();
} finally {
//关闭连接,释放资源
if (get != null) {
get.releaseConnection();
}
if (client != null) {
try {
client.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return result;
}
}
相关文章推荐
- JAVA HTTP POST请求(请求参数格式为json)
- asynchttpclient的post方法发送json格式参数和表单格式参数
- HttpClient用post方式传JSON格式字符串参数
- java代码发送JSON格式的httpPOST请求
- Java:HttpClient篇,HttpClient4.2在Java中的几则应用:Get、Post参数、Session(会话)保持、Proxy(代理服务器)设置,多线程设置...
- Java后台如何接收并处理前台传过来的json格式的数组参数
- java代码发送JSON格式的httpPOST请求
- httpclient的post请求并处理json格式响应数据
- http post 请求参数格式是json
- HttpClient与参数json
- Java HttpClient 发送multipart/form-data带有Json文件的Post请求
- java实现HTTP的post请求 json格式中文乱码问题
- Java后台如何接收并处理前台传过来的json格式的数组参数
- java代码发送JSON格式的httpPOST请求
- java处理http发送的数据转化为json格式并返回遇到的问题
- httpclient 上传及返回参数类型,解析json 或xml
- java实现HTTP的post请求 key-value格式与json格式的区别
- Java:HttpClient篇,HttpClient4.2在Java中的几则应用:Get、Post参数、Session(会话)保持、Proxy(代理服务器)设置,多线程设置...
- httpclient提交json参数
- Java:HttpClient篇,HttpClient4.2在Java中的几则应用:Get、Post参数、Session(会话)保持、Proxy(代理服务器)设置,多线程设置...