使用httpclient发送get/post请求
来源:互联网 发布:方块导航网源码 编辑:程序博客网 时间:2024/06/05 07:24
当我们需要调用http接口时,可以使用httpclient以get/post方式发送请求,获取返回的json数据
maven依赖如下
<dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpcore</artifactId> <version>4.4.4</version> </dependency> <!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpclient --> <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient</artifactId> <version>4.5.2</version> </dependency>
package com.yingjun.ssm;import com.alibaba.fastjson.JSON;import com.alibaba.fastjson.JSONObject;import org.apache.http.HttpEntity;import org.apache.http.HttpResponse;import org.apache.http.client.HttpClient;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.HttpClients;import org.apache.http.util.EntityUtils;/** * Created by cjq on 2017/8/23. */public class HttpUtil { /** * 以get方式发送请求 * @param url * @return */ public static JSONObject doGetStr(String url){ HttpClient httpClient= HttpClients.custom().build(); HttpGet httpGet=new HttpGet(url); JSONObject jsonpObject=null; try { HttpResponse response = httpClient.execute(httpGet); HttpEntity entity=response.getEntity(); if(entity!=null){ String result= EntityUtils.toString(entity,"UTF-8"); jsonpObject= JSON.parseObject(result); } }catch (Exception e){ } return jsonpObject; } /** * post请求 * @param url * @param outStr * @return JSONObject /String */ public static JSONObject doPostStr(String url,String outStr){ HttpClient httpClient=HttpClients.custom().build(); HttpPost httpPost=new HttpPost(url); JSONObject jsonObject=null; String result=""; try { httpPost.setEntity(new StringEntity(outStr, "UTF-8")); HttpResponse respons=httpClient.execute(httpPost); result=EntityUtils.toString(respons.getEntity(),"UTF-8"); jsonObject= JSON.parseObject(result); }catch (Exception e){ e.printStackTrace(); } return jsonObject; } public String doPostMethod(String url,String outStr){ HttpClient httpClient=HttpClients.custom().build(); String result = ""; HttpPost httpPost = null; HttpResponse httpResponse = null; HttpEntity entity = null; httpPost = new HttpPost(url); try { httpPost.setEntity(new StringEntity(outStr, "UTF-8")); httpResponse = httpClient.execute(httpPost); entity = httpResponse.getEntity(); if( entity != null ){ result = EntityUtils.toString(entity,"UTF-8"); } }catch (Exception e){ e.printStackTrace(); } return result; } public static void main(String args[]) { HttpUtil httpUtil=new HttpUtil(); JSONObject jsonObject= httpUtil.doGetStr("http://gc.ditu.aliyun.com/geocoding?a=苏州市"); System.out.println(jsonObject.toJSONString()); }}
结果如下
{"address":"","cityName":"","alevel":4,"level":2,"lon":120.58531,"lat":31.29888}
阅读全文
0 0
- 使用Apache HttpClient发送get、post请求
- 使用HttpClient发送GET和POST请求
- 使用httpclient发送get/post请求
- 使用httpclient发送get或post请求
- Java使用HttpClient发送Get请求和Post请求
- Android使用Apache HttpClient发送GET、POST请求
- Android笔记---使用HttpClient发送POST和GET请求
- android_30_使用HttpClient发送Get和Post请求
- Java使用HttpClient发送Get和Post请求
- java使用HttpClient模拟发送http的get、post请求
- 使用HttpClient发送POST请求
- 使用httpclient发送post请求
- java httpclient发送post get 请求
- HttpClient发送get和post请求
- 利用HttpClient发送Get和Post请求
- 利用HttpClient发送Get和Post请求
- 利用HttpClient发送Get和Post请求
- HttpClient发送Get、Post请求简单实践
- python引包以及pyCharm运行scrapy方法
- 关于json
- ◆◆◆Angular2学习笔记-问题解决方法
- μCOS 系列专题—分配优先级(Assigning Task Priorities)
- zookeeper-crud
- 使用httpclient发送get/post请求
- 绝对路径和相对路径
- 树莓派之蓝牙编程
- 字符设备驱动-LED驱动
- activiti全局的监听器和链接监听器
- C++项目中的extern "C" {}
- 算法学习之旅,中级篇(4)-–快速排序
- c++ primer plus 第十章答案
- Spring注解@Component、@Repository、@Service、@Controller区别