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<NameValuePair> formParams = new ArrayList<NameValuePair>(); //表單參數 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"));
html
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<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder.<ConnectionSocketFactory>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代理配置git
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<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder.<ConnectionSocketFactory>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);
版權聲明
做者:wycm
出處:https://www.cnblogs.com/w-y-c-m/p/9237335.html
您的支持是對博主最大的鼓勵,感謝您的認真閱讀。
本文版權歸做者全部,歡迎轉載,但未經做者贊成必須保留此段聲明,且在文章頁面明顯位置給出原文鏈接,不然保留追究法律責任的權利。
github