连接超时(connect timed out)和读取超时(Read timed out)

您所在的位置:网站首页 flask与vue请求超时 连接超时(connect timed out)和读取超时(Read timed out)

连接超时(connect timed out)和读取超时(Read timed out)

2023-11-03 09:10| 来源: 网络整理| 查看: 265

import org.apache.http.HttpEntity;

import org.apache.http.HttpHost;

import org.apache.http.client.config.RequestConfig;

import org.apache.http.clienthods.CloseableHttpResponse;

import org.apache.http.clienthods.HttpGet;

import org.apache.http.impl.client.CloseableHttpClient;

import org.apache.http.impl.client.HttpClients;

import org.apache.http.util.EntityUtils;

 

public class Demo {

    public static void main(String[] args) throws Exception {

        CloseableHttpClient closeableHttpClient=HttpClients.createDefault(); //1、创建实例

         

        /* 访问我在香港机房的网站首页 */

        HttpGet httpGet=new HttpGet("http://www.zhixuyuan.top"); //2、创建实例

         

        HttpHost proxy=new HttpHost("114.219.61.82", 8118);

        RequestConfig config=RequestConfig.custom().setProxy(proxy)

                .setConnectTimeout(3000) // 设置连接超时时间 3秒钟

                .setSocketTimeout(10) // 设置读取超时时间0.01秒钟

                .build();

        httpGet.setConfig(config);

         

        CloseableHttpResponse closeableHttpResponse=closeableHttpClient.execute(httpGet); //3、执行

        HttpEntity httpEntity=closeableHttpResponse.getEntity(); //4、获取实体

         

        //System.out.println(httpEntity.toString());

        System.out.println(EntityUtils.toString(httpEntity, "utf-8")); //获取网页内容

         

        closeableHttpResponse.close();

        closeableHttpClient.close();

    }

}



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3