java的httpClient请求

 <dependency>
        <groupId>org.apache.httpcomponents</groupId>
        <artifactId>httpclient</artifactId>
        <version>4.5.2</version>
    </dependency>
   <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-log4j12</artifactId>
        <version>1.7.25</version>
       <!-- <scope>test</scope>-->
    </dependency>

log4j.properties文件

log4j.rootLogger=INFO,A3,STDOUT

log4j.appender.STDOUT=org.apache.log4j.ConsoleAppender
log4j.appender.STDOUT.layout=org.apache.log4j.PatternLayout
log4j.appender.STDOUT.layout.ConversionPattern=[%p] [%l] %10.10c - %m%n

log4j.appender.A3=org.apache.log4j.RollingFileAppender
log4j.appender.A3.file=logs/server.log
log4j.appender.A3.MaxFileSize=1024KB
log4j.appender.A3.MaxBackupIndex=10
log4j.appender.A3.layout=org.apache.log4j.PatternLayout
log4j.appender.A3.layout.ConversionPattern=\n\n[%-5p] %d{yyyy-MM-dd HH:mm:ss,SSS} method:%l%n%m%n

1、get无参数
public class HttpGetTest {
public static void main(String[] args) {
CloseableHttpClient aDefault = HttpClients.createDefault();
HttpGet httpGet = new HttpGet(“http://www.itcast.cn”);
CloseableHttpResponse response = null;
try {
response = aDefault.execute(httpGet);
if(response.getStatusLine().getStatusCode()==200){
HttpEntity entity = response.getEntity();
String content = EntityUtils.toString(entity,“utf8”);
System.out.println(content.length());
}
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
response.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
aDefault.close();
} catch (IOException e) {
e.printStackTrace();
}

    }


}

}

2、get有参数
public static void main(String[] args) throws IOException, URISyntaxException {
//1打开浏览器
CloseableHttpClient http = HttpClients.createDefault();

    //2输入网址
    URIBuilder uriBuilder = new URIBuilder("http://resource.boxuegu.com/booklist/find.html");
    uriBuilder.setParameter("search","java");
    uriBuilder.setParameter("enc","utf-8");
    HttpGet httpGet = new HttpGet(uriBuilder.build());

    //设置请求信息
    RequestConfig config = RequestConfig.custom().setConnectTimeout(1000)//创建连接的最长时间,单位是毫秒
            .setConnectionRequestTimeout(500)//设置获取连接的最长时间,单位毫秒
            .setSocketTimeout(10*1000)//设置数据传输最长时间,单位毫秒
            .build();

    httpGet.setConfig(config);

    //HttpGet httpGet = new HttpGet("https://search.jd.com/Search");
    //System.out.println("发起请求"+httpGet);
    //3回车
    CloseableHttpResponse response = http.execute(httpGet);
    //4解析响应数据
    if(response.getStatusLine().getStatusCode()==200){
        HttpEntity entity = response.getEntity();
        String content = EntityUtils.toString(entity, "utf8");
        System.out.println(content);
    }
}

3、post无参
//无参数
public static void main(String[] args) {
CloseableHttpClient aDefault = HttpClients.createDefault();
HttpPost httpPost = new HttpPost(“http://www.itcast.cn”);
CloseableHttpResponse response = null;
try {
response = aDefault.execute(httpPost);
if(response.getStatusLine().getStatusCode()==200){
HttpEntity entity = response.getEntity();
String content = EntityUtils.toString(entity,“utf8”);
System.out.println(content.length());
}
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
response.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
aDefault.close();
} catch (IOException e) {
e.printStackTrace();
}

    }


}

}

4、post有参数
public static void main(String[] args) throws UnsupportedEncodingException {
CloseableHttpClient aDefault = HttpClients.createDefault();
HttpPost httpPost = new HttpPost(“http://yun.itheima.com/search”);
//list集合
List params = new ArrayList();
params.add(new BasicNameValuePair(“keys”,“java”)) ;
UrlEncodedFormEntity entity1 = new UrlEncodedFormEntity(params,“utf8”);

    httpPost.setEntity(entity1);

    CloseableHttpResponse response = null;
    try {
        response = aDefault.execute(httpPost);
        if(response.getStatusLine().getStatusCode()==200){
            HttpEntity entity = response.getEntity();
            String content = EntityUtils.toString(entity,"utf8");
            System.out.println(content.length());
        }
    } catch (IOException e) {
        e.printStackTrace();
    }finally {
        try {
            response.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            aDefault.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }


}

}

5、连接池

public static void main(String[] args) {
PoolingHttpClientConnectionManager mg = new PoolingHttpClientConnectionManager();
mg.setMaxTotal(100);//最大连接数
mg.setDefaultMaxPerRoute(10);//每个主机最大链接数
doGet(mg);
}

private static void doGet(PoolingHttpClientConnectionManager mg)  {
    CloseableHttpClient httpclient = HttpClients.custom().setConnectionManager(mg).build();
    HttpGet httpGet = new HttpGet("http://www.baidu.com");
    CloseableHttpResponse response = null;
    try {
        response = httpclient.execute(httpGet);
        if(response.getStatusLine().getStatusCode()==200){
            HttpEntity entity = response.getEntity();
            String s = EntityUtils.toString(entity,"utf8");
            System.out.println(s.length());
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if(response!=null){
            try {
                response.close();
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
    }

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值