欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Httpclient 设置域名指向ip的两种方式

程序员文章站 2024-02-01 12:41:04
...

httpRequest是由params和url构成

    private static String send2(HttpUriRequest httpRequest, CookieStore cookieStore) throws IOException {
        HttpClientBuilder httpClientBuilder = HttpClients.custom();

//        if (cookieStore != null) {
            byte ip[] = new byte[] { (byte) 123, (byte) 13, 12, 31 };
            InetAddress inetAddress= InetAddress.getByAddress("mppc.siemin.com",ip);
            HttpHost proxy = new HttpHost(inetAddress,9083,"http");
            RequestConfig globalConfig = RequestConfig.custom()
                    .setCookieSpec(CookieSpecs.DEFAULT)
                    .setProxy(proxy)
                    .build();

            httpClientBuilder.setDefaultRequestConfig(globalConfig);
            httpClientBuilder.setDefaultCookieStore(cookieStore);
//        }

        CloseableHttpClient client = httpClientBuilder.build();
        CloseableHttpResponse response =     client.execute(httpRequest);
        try {
            HttpEntity entity = response.getEntity();
            if (entity != null) {
                return  EntityUtils.toString(entity, "UTF-8");
            }
        } finally {
            response.close();
        }
        return  null;
    }

    private static String send(HttpUriRequest httpRequest, CookieStore cookieStore) throws IOException {
        HttpClientBuilder httpClientBuilder = HttpClients.custom();

        HttpHost proxy = new HttpHost("123.21.213.213",9083,"http");
        RequestConfig globalConfig = RequestConfig.custom()
                .setCookieSpec(CookieSpecs.DEFAULT)
                .setProxy(proxy)
                .build();

        httpClientBuilder.setDefaultRequestConfig(globalConfig);
        httpClientBuilder.setDefaultCookieStore(cookieStore);
        CloseableHttpClient client = httpClientBuilder.build();
        CloseableHttpResponse response =     client.execute(httpRequest);
        try {
            HttpEntity entity = response.getEntity();
            if (entity != null) {
                return  EntityUtils.toString(entity, "UTF-8");
            }
        } finally {
            response.close();
        }
        return  null;
    }
相关标签: http