The reference codes for get and post methods are as follows:
import org.apache.http.HttpEntity; import org.apache.http.HttpHost; import org.apache.http.NameValuePair; import org.apache.http.client.entity.UrlEncodedFormEntity; import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpPost; import org.apache.http.client.protocol.HttpClientContext; import org.apache.http.config.Registry; import org.apache.http.config.RegistryBuilder; import org.apache.http.conn.DnsResolver; import org.apache.http.conn.socket.ConnectionSocketFactory; import org.apache.http.conn.socket.PlainConnectionSocketFactory; import org.apache.http.conn.ssl.SSLConnectionSocketFactory; import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; import org.apache.http.impl.conn.PoolingHttpClientConnectionManager; import org.apache.http.message.BasicNameValuePair; import org.apache.http.protocol.HttpContext; import org.apache.http.ssl.SSLContexts; import org.apache.http.util.EntityUtils; import javax.net.ssl.SSLContext; import java.io.*; import java.net.*; import java.util.ArrayList; import java.util.List; import java.util.Map; public class HttpsSocks5Utils { //The order is proxy address, proxy port number, user, password private static String proxyHost="testHost"; private static int proxyPort=1080; private static String proxyName="test"; private static String proxyPwd="test"; public static InputStream getWithProxy(String url, Map<String, String> headers, String charset) { //Username and password verification Authenticator.setDefault(new Authenticator(){ protected PasswordAuthentication getPasswordAuthentication(){ PasswordAuthentication p=new PasswordAuthentication(proxyName, proxyPwd.toCharArray()); return p; } }); Registry<ConnectionSocketFactory> reg = RegistryBuilder.<ConnectionSocketFactory> create() .register("http", new MyConnectionSocketFactory()) .register("https", new MySSLConnectionSocketFactory(SSLContexts.createSystemDefault())).build(); PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager(reg, new FakeDnsResolver()); CloseableHttpClient httpclient = HttpClients.custom().setConnectionManager(cm).build(); try { InetSocketAddress socksaddr = new InetSocketAddress(proxyHost,proxyPort); HttpClientContext context = HttpClientContext.create(); context.setAttribute("socks.address", socksaddr); HttpGet httpget = new HttpGet(url); if(headers != null) { for(String key:headers.keySet()) { httpget.setHeader(key, headers.get(key)); } } CloseableHttpResponse response = httpclient.execute(httpget,context); try { return response.getEntity().getContent(); // return new String(EntityUtils.toByteArray(response.getEntity()), charset); } finally { response.close(); } } catch (Exception e) { e.printStackTrace(); } finally { try { httpclient.close(); } catch (Exception e2) { e2.printStackTrace(); } } return null; } public static InputStream postWithProxy(String url,Map<String, Object> paramMap,Map<String, String> headerMap) { //Username and password verification Authenticator.setDefault(new Authenticator(){ protected PasswordAuthentication getPasswordAuthentication(){ PasswordAuthentication p=new PasswordAuthentication(proxyName, proxyPwd.toCharArray()); return p; } }); Registry<ConnectionSocketFactory> reg = RegistryBuilder.<ConnectionSocketFactory> create() .register("http", new MyConnectionSocketFactory()) .register("https", new MySSLConnectionSocketFactory(SSLContexts.createSystemDefault())).build(); PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager(reg, new FakeDnsResolver()); CloseableHttpClient httpClient = HttpClients.custom().setConnectionManager(cm).build(); try { InetSocketAddress socksaddr = new InetSocketAddress(proxyHost, proxyPort); HttpClientContext context = HttpClientContext.create(); context.setAttribute("socks.address", socksaddr); //Create Post request HttpPost httpPost = new HttpPost(url); //Encapsulate post request header if (null != headerMap & amp; & amp; headerMap.size() > 0){ for (Map.Entry<String, String> stringObjectEntry : headerMap.entrySet()) { httpPost.addHeader(stringObjectEntry.getKey(), stringObjectEntry.getValue()); } } if (httpPost.getFirstHeader("User-Agent")==null){ httpPost.addHeader("User-Agent","Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Safari/537.36 Edg/116.0.1938.62"); } //Encapsulate post request parameters if (null != paramMap & amp; & amp; paramMap.size() > 0) { List<NameValuePair> nameValuePairs = new ArrayList<>(); // Get the entity through the map integrated entrySet method for (Map.Entry<String, Object> mapEntry : paramMap.entrySet()) { nameValuePairs.add(new BasicNameValuePair(mapEntry.getKey(), mapEntry.getValue().toString())); } //Set encapsulated request parameters for httpPost try { httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs, "UTF-8")); } catch (UnsupportedEncodingException e) { throw new RuntimeException("Data encapsulation exception"); } } //Set ContentType (Note: If you just pass ordinary parameters, ContentType does not have to be application/json) // httpPost.setHeader("Content-Type", "application/json;charset=utf8"); CloseableHttpResponse response = httpClient.execute(httpPost, context); // Get the response entity from the response model HttpEntity responseEntity = response.getEntity(); System.out.println("The response status is:" + response.getStatusLine()); if (responseEntity != null) { System.out.println("The response content length is:" + responseEntity.getContentLength()); System.out.println("The response content is:" + EntityUtils.toString(responseEntity)); System.out.println(); } return responseEntity.getContent(); } catch (Exception e) { throw new RuntimeException(e); } finally { try { httpClient.close(); } catch (Exception e2) { e2.printStackTrace(); } } } static class FakeDnsResolver implements DnsResolver { @Override public InetAddress[] resolve(String host) throws UnknownHostException { // Return some fake DNS record for every request, we won't be using it return new InetAddress[] { InetAddress.getByAddress(new byte[] { 1, 1, 1, 1 }) }; } } static class MyConnectionSocketFactory extends PlainConnectionSocketFactory { @Override public Socket createSocket(final HttpContext context) throws IOException { InetSocketAddress socksaddr = (InetSocketAddress) context.getAttribute("socks.address"); Proxy proxy = new Proxy(Proxy.Type.SOCKS, socksaddr); return new Socket(proxy); } @Override public Socket connectSocket(int connectTimeout, Socket socket, HttpHost host, InetSocketAddress remoteAddress, InetSocketAddress localAddress, HttpContext context) throws IOException { // Convert address to unresolved InetSocketAddress unresolvedRemote = InetSocketAddress .createUnresolved(host.getHostName(), remoteAddress.getPort()); return super.connectSocket(connectTimeout, socket, host, unresolvedRemote, localAddress, context); } } static class MySSLConnectionSocketFactory extends SSLConnectionSocketFactory { public MySSLConnectionSocketFactory(final SSLContext sslContext) { // You may need this verifier if target site's certificate is not secure super(sslContext, ALLOW_ALL_HOSTNAME_VERIFIER); } @Override public Socket createSocket(final HttpContext context) throws IOException { InetSocketAddress socksaddr = (InetSocketAddress) context.getAttribute("socks.address"); Proxy proxy = new Proxy(Proxy.Type.SOCKS, socksaddr); return new Socket(proxy); } @Override public Socket connectSocket(int connectTimeout, Socket socket, HttpHost host, InetSocketAddress remoteAddress, InetSocketAddress localAddress, HttpContext context) throws IOException { // Convert address to unresolved InetSocketAddress unresolvedRemote = InetSocketAddress .createUnresolved(host.getHostName(), remoteAddress.getPort()); return super.connectSocket(connectTimeout, socket, host, unresolvedRemote, localAddress, context); } } }