HTTP访问地址Get方式

大城市里の小女人 提交于 2020-01-10 15:15:38
 1 private static RequestConfig requestConfig = null;
 2 
 3 //get方式
 4     public static JSONObject httpGet(String url){
 5         JSONObject jsonResult = null;
 6         CloseableHttpClient client = HttpClients.createDefault();
 7         HttpGet request = new HttpGet(url);
 8         request.setConfig(requestConfig);
 9         try {
10             CloseableHttpResponse response = client.execute(request);
11             if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK){
12                 HttpEntity entity = response.getEntity();
13                 String strResult = EntityUtils.toString(entity, "utf-8");
14                 jsonResult = JSONObject.parseObject(strResult);
15             }else{
16 
17             }
18         }catch (IOException e){
19 
20         }finally {
21             request.releaseConnection();
22         }
23         return jsonResult;
24     }

通过传入url,可以解析路径返回的json参数

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!