JAVA HttpClient(普通参数、JSON格式参数)
来源:互联网 发布:淘宝上找货源 编辑:程序博客网 时间:2024/06/15 03:00
完整示例:点击下载
代码如下:
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;}}
阅读全文
0 0
- JAVA HttpClient(普通参数、JSON格式参数)
- Java HttpClient Post多层json格式参数
- httpclient提交json参数
- httpclient提交json参数
- HttpClient与参数json
- httpclient的几种post参数格式
- httpClient发送key_value、json参数及文件
- httpclient使用post提交json参数
- HTTPPOST 发送JSON格式参数
- 参数以json格式上传
- 传入json格式的参数
- Jmeter请求参数Json格式
- java后台接口参数param的json格式
- java服务器端接收json格式传递的参数
- java解析json格式请求的url中参数
- JAVA HTTP POST请求(请求参数格式为json)
- java服务器端接收json格式传递的参数
- Httpclient 参数
- nginx.conf配置详解
- Xamrin.Forms 用户界面——控件——Style——设备样式
- LeetCode547. Friends Circles 利用union find | bfs | dfs三种方法解决
- 开启科研之路,写此文以作纪念!
- Linux 问题解决拾遗
- JAVA HttpClient(普通参数、JSON格式参数)
- Mysql读写分离实现的三种方式
- 【牛腩】LinkButton
- SQL FULL JOIN 关键字
- C语言(18)A+B Problem II
- Storm基本知识
- Android 项目架构探索
- java 打印素数
- C语言,念整数