CloseableHttpClient httpClient = HttpClients.custom() .build(); CloseableHttpResponse respOnse= httpClient.execute(new HttpGet("https://www.baidu.com")); System.out.println(EntityUtils.toString(response.getEntity()));
CloseableHttpClient httpClient = HttpClients.custom() .build(); HttpPost httpPost = new HttpPost("https://www.explame.com"); List formParams = new ArrayList(); //表单参数 formParams.add(new BasicNameValuePair("name1", "value1")); formParams.add(new BasicNameValuePair("name2", "value2")); UrlEncodedFormEntity entity = new UrlEncodedFormEntity(formParams, "utf-8"); httpPost.setEntity(entity); CloseableHttpResponse respOnse= httpClient.execute(httpPost); System.out.println(EntityUtils.toString(response.getEntity()));
CloseableHttpClient httpClient = HttpClients.custom() .build(); HttpPost httpPost = new HttpPost("https://www.explame.com"); StringEntity entity = new StringEntity("{\"id\": \"1\"}"); httpPost.setEntity(entity); CloseableHttpResponse respOnse= httpClient.execute(httpPost); System.out.println(EntityUtils.toString(response.getEntity()));
CloseableHttpClient httpClient = HttpClients.custom() .build(); HttpPost httpPost = new HttpPost("https://www.example.com"); MultipartEntityBuilder multipartEntityBuilder = MultipartEntityBuilder.create(); //要上传的文件 multipartEntityBuilder.addBinaryBody("file", new File("temp.txt")); httpPost.setEntity(multipartEntityBuilder.build()); CloseableHttpResponse respOnse= httpClient.execute(httpPost); System.out.println(EntityUtils.toString(response.getEntity()));
CloseableHttpClient httpClient = HttpClients.custom() .build(); HttpPost httpPost = new HttpPost("https://www.example.com"); MultipartEntityBuilder multipartEntityBuilder = MultipartEntityBuilder.create(); multipartEntityBuilder.addTextBody("username","wycm"); multipartEntityBuilder.addTextBody("passowrd","123"); //文件 multipartEntityBuilder.addBinaryBody("file", new File("temp.txt")); httpPost.setEntity(multipartEntityBuilder.build()); CloseableHttpResponse respOnse= httpClient.execute(httpPost); System.out.println(EntityUtils.toString(response.getEntity()));
CloseableHttpClient httpClient = HttpClients.custom() .setUserAgent("Mozilla/5.0") .build(); CloseableHttpResponse respOnse= httpClient.execute(new HttpGet("https://www.baidu.com")); System.out.println(EntityUtils.toString(response.getEntity()));
设置重试处理器
当请求超时, 会自动重试,最多3次
HttpRequestRetryHandler retryHandler = (exception, executionCount, context) -> { if (executionCount >= 3) { return false; } if (exception instanceof InterruptedIOException) { return true; } if (exception instanceof UnknownHostException) { return true; } if (exception instanceof ConnectTimeoutException) { return true; } if (exception instanceof SSLException) { return true; } HttpClientContext clientCOntext= HttpClientContext.adapt(context); HttpRequest request = clientContext.getRequest(); boolean idempotent = !(request instanceof HttpEntityEnclosingRequest); if (idempotent) { return true; } return false; }; CloseableHttpClient httpClient = HttpClients.custom() .setRetryHandler(retryHandler) .build(); httpClient.execute(new HttpGet("https://www.baidu.com"));
CloseableHttpClient httpClient = HttpClients.custom() //关闭httpclient重定向 .disableRedirectHandling() .build();
CloseableHttpClient httpClient = HttpClients.custom() //post 302支持重定向 .setRedirectStrategy(new LaxRedirectStrategy()) .build(); CloseableHttpResponse respOnse= httpClient.execute(new HttpPost("https://www.explame.com")); System.out.println(EntityUtils.toString(response.getEntity()));
CloseableHttpClient httpClient = HttpClients.custom() .build(); HttpGet httpGet = new HttpGet("http://www.example.com"); httpGet.addHeader("COOKIE", "name=value"); httpClient.execute(httpGet);
httpGet.addHeader("COOKIE", "name=value");
//此处直接粘贴浏览器COOKIE final String RAW_COOKIES = "name1=value1; name2=value2"; final COOKIEStore COOKIEStore = new BasicCOOKIEStore(); for (String rawCOOKIE : RAW_COOKIES.split("; ")){ String[] s = rawCOOKIE.split("="); BasicClientCOOKIE COOKIE = new BasicClientCOOKIE(s[0], s[1]); COOKIE.setDomain("baidu.com"); COOKIE.setPath("/"); COOKIE.setSecure(false); COOKIE.setAttribute("domain", "baidu.com"); Calendar calendar = Calendar.getInstance(); calendar.add(Calendar.DAY_OF_MONTH, +5); COOKIE.setExpiryDate(calendar.getTime()); COOKIEStore.addCOOKIE(COOKIE); } CloseableHttpClient httpClient = HttpClients.custom() .setDefaultCOOKIEStore(COOKIEStore) .build(); httpClient.execute(new HttpGet("https://www.baidu.com"));
COOKIEStore COOKIEStore = new BasicCOOKIEStore(); CloseableHttpClient httpClient = HttpClients.custom() .setDefaultCOOKIEStore(COOKIEStore) .build(); httpClient.execute(new HttpGet("https://www.baidu.com")); //请求一次后,清理COOKIE再发起一次新的请求 COOKIEStore.clear(); httpClient.execute(new HttpGet("https://www.baidu.com"));
//注:HttpClientContext不是线程安全的,不要多个线程维护一个HttpClientContext HttpClientContext httpCOntext= HttpClientContext.create(); CloseableHttpClient httpClient = HttpClients.custom() .build(); httpClient.execute(new HttpGet("https://www.baidu.com"), httpContext); //请求一次后,清理COOKIE再发起一次新的请求 httpContext.getCOOKIEStore().clear(); httpClient.execute(new HttpGet("https://www.baidu.com"));
CloseableHttpClient httpClient = HttpClients.custom() //设置代理 .setRoutePlanner(new DefaultProxyRoutePlanner(new HttpHost("localhost", 8888))) .build(); CloseableHttpResponse respOnse= httpClient.execute(new HttpGet("http://www.example.com")); System.out.println(EntityUtils.toString(response.getEntity()));
//默认信任 SSLContext sslCOntext= SSLContexts.custom() .loadTrustMaterial(KeyStore.getInstance(KeyStore.getDefaultType()) , (chain, authType) -> true).build(); Registry socketFactoryRegistry = RegistryBuilder.create() .register("http", new SocketProxyPlainConnectionSocketFactory()) .register("https", new SocketProxySSLConnectionSocketFactory(sslContext)) .build(); CloseableHttpClient httpClient = HttpClients.custom() .setConnectionManager(new PoolingHttpClientConnectionManager(socketFactoryRegistry)) .build(); HttpClientContext httpClientCOntext= HttpClientContext.create(); httpClientContext.setAttribute("socks.address", new InetSocketAddress("127.0.0.1", 1086)); CloseableHttpResponse respOnse= httpClient.execute(new HttpGet("https://httpbin.org/ip"), httpClientContext); System.out.println(EntityUtils.toString(response.getEntity()));
socket代理配置
static class SocketProxyPlainConnectionSocketFactory extends PlainConnectionSocketFactory{
@Override
public Socket createSocket(final HttpContext context) {
InetSocketAddress socksAddr = (InetSocketAddress) context.getAttribute("socks.address");
if (socksAddr != null){
Proxy proxy = new Proxy(Proxy.Type.SOCKS, socksAddr);
return new Socket(proxy);
} else {
return new Socket();
}
}
}
static class SocketProxySSLConnectionSocketFactory extends SSLConnectionSocketFactory {
public SocketProxySSLConnectionSocketFactory(final SSLContext sslContext) {
super(sslContext, NoopHostnameVerifier.INSTANCE);
}
@Override
public Socket createSocket(final HttpContext context) {
InetSocketAddress socksAddr = (InetSocketAddress) context.getAttribute("socks.address");
if (socksAddr != null){
Proxy proxy = new Proxy(Proxy.Type.SOCKS, socksAddr);
return new Socket(proxy);
} else {
return new Socket();
}
}
}
/**
* socket代理配置
*/
public static void socketProxy() throws Exception {
//默认信任
SSLContext sslCOntext= SSLContexts.custom()
.loadTrustMaterial(KeyStore.getInstance(KeyStore.getDefaultType())
, (X509Certificate[] chain, String authType) -> true).build();
Registry socketFactoryRegistry =
RegistryBuilder.create()
.register("http", new SocketProxyPlainConnectionSocketFactory())
.register("https", new SocketProxySSLConnectionSocketFactory(sslContext))
.build();
CloseableHttpClient httpClient = HttpClients.custom()
.setConnectionManager(new PoolingHttpClientConnectionManager(socketFactoryRegistry))
.build();
HttpClientContext httpClientCOntext= HttpClientContext.create();
httpClientContext.setAttribute("socks.address", new InetSocketAddress("127.0.0.1", 1086));
CloseableHttpResponse respOnse= httpClient.execute(new HttpGet("https://httpbin.org/ip"), httpClientContext);
System.out.println(EntityUtils.toString(response.getEntity()));
}
下载文件
CloseableHttpClient httpClient = HttpClients.custom().build();
CloseableHttpResponse respOnse= httpClient.execute(new HttpGet("https://www.example.com"));
InputStream is = response.getEntity().getContent();
Files.copy(is, new File("temp.png").toPath(), StandardCopyOption.REPLACE_EXISTING);