• 使用HttpGet,closeableHttpClient,HttpClients,CloseableHttpResponse实现本地访问web服务器


    一、架包

    httpclient-4.4.1.jar  httpcore-4.4.1.jar   httpmime-4.2.jar

    二、例子

    String url="http://xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
    
    HttpGet httpGet = new HttpGet(url);
            CloseableHttpClient httpclient = HttpClients.createDefault();
            CloseableHttpResponse response = httpclient.execute(httpGet);
            int statusCode = response.getStatusLine().getStatusCode();
            if (statusCode != 200) {
                httpGet.abort();
                throw new RuntimeException("HttpClient,error status code :" + statusCode);
            }
            HttpEntity entity = response.getEntity();
            String result = null;
            if (entity != null) {
                result = EntityUtils.toString(entity, "utf-8");
                EntityUtils.consume(entity);
                response.close();
                System.out.println(result);
            }

    三、使用多线程以及CountDownLatch实现共同访问

    public class Test {
    public static void main(String[] args) {
        CountDownLatch latch=new CountDownLatch(1);
        String url="http://test.meyki.net:9080/apidev/v3/goods/goodsList/eyJwYWdlU2l6ZSI6IjIwIiwib3MiOiJpb3MiLCJzaG9wSWQiOiI1MTU2MjNmNGVlOGU0OGIxYmU3MmY2MGFiOTcxZTExOSIsInVzZXJJZCI6IjIyNTg0MDVmYTU5ODRlOTViNWI2NTI4ZTM3YmExODBjIiwicGFnZU5vIjoiMSIsImlzU2hlbHZlcyI6IjEiLCJ2Y29kZSI6IjE0MCIsInRhZyI6IjEifQ==";
        for(int i=0;i<=15;i++){
            Thread thread=new Thread(new Mythread(latch,url));
            thread.start();
        }
        latch.countDown();
    }
    }
     1 package com.meyki.common;
     2 
     3 import java.io.IOException;
     4 import java.util.concurrent.CountDownLatch;
     5 
     6 import org.apache.http.HttpEntity;
     7 import org.apache.http.ParseException;
     8 import org.apache.http.client.methods.CloseableHttpResponse;
     9 import org.apache.http.client.methods.HttpGet;
    10 import org.apache.http.impl.client.CloseableHttpClient;
    11 import org.apache.http.impl.client.HttpClients;
    12 import org.apache.http.util.EntityUtils;
    13 
    14 import okhttp3.OkHttpClient;
    15 import okhttp3.Request;
    16 import okhttp3.Response;
    17 
    18 public class Mythread  implements Runnable{
    19     private final CountDownLatch startSignal;
    20     private  String url;
    21     public Mythread(CountDownLatch  startSignal,String url){
    22         super();
    23         this.startSignal=startSignal;
    24         this.url=url;
    25     }
    26     @Override
    27     public void run() {
    28         try {
    29             startSignal.await();
    30         } catch (InterruptedException e) {
    31             // TODO Auto-generated catch block
    32             e.printStackTrace();
    33         }
    34         try {
    35             dowork();
    36         } catch (ParseException e) {
    37             // TODO Auto-generated catch block
    38             e.printStackTrace();
    39         } catch (IOException e) {
    40             // TODO Auto-generated catch block
    41             e.printStackTrace();
    42         }
    43     }
    44     private void dowork() throws ParseException, IOException {
    45         
    46         //使用HttpGet,closeableHttpClient,HttpClients,CloseableHttpResponse在本地访问
    47         HttpGet httpGet = new HttpGet(url);
    48         CloseableHttpClient httpclient = HttpClients.createDefault();
    49         CloseableHttpResponse response = httpclient.execute(httpGet);
    50         int statusCode = response.getStatusLine().getStatusCode();
    51         if (statusCode != 200) {
    52             httpGet.abort();
    53             throw new RuntimeException("HttpClient,error status code :" + statusCode);
    54         }
    55         HttpEntity entity = response.getEntity();
    56         String result = null;
    57         if (entity != null) {
    58             result = EntityUtils.toString(entity, "utf-8");
    59             EntityUtils.consume(entity);
    60             response.close();
    61             System.out.println(result);
    62         }
    63         //使用OkhttpClient
    64         /*OkHttpClient   httpClient=new OkHttpClient();
    65         Request request=new Request.Builder()
    66                         .url(url)
    67                         .build();
    68         //得到response对象
    69         Response response=httpClient.newCall(request).execute();
    70         if(response.isSuccessful()){
    71             String result=response.body().string();
    72             System.out.println(response.code());
    73             System.out.println(response.message());
    74             System.out.println(result);
    75         }*/
    76     }
    77         
    78 }
  • 相关阅读:
    Codeforces Round448 D
    Codeforces Round448 C Square Subsets
    hdu6006
    2017计蒜客计算之道初赛第六场 微软大楼设计方案(困难)
    UVA 12105 Bigger is Better
    Codeforce 55D Beautiful numbers
    4月补题
    C++小技巧之CONTAINING_RECORD
    Codeforces #380 div2 E(729E) Subordinates
    Codeforces #380 div2 D(729D) Sea Battle
  • 原文地址:https://www.cnblogs.com/wwwcf1982603555/p/9088695.html
Copyright © 2020-2023  润新知