Jade Dungeon

http

apache-httpclient

httpClient.getParams().setParameter(ClientPNames.ALLOW_CIRCULAR_REDIRECTS, true);
         httpClient.getParams().setParameter(ClientPNames.MAX_REDIRECTS, 5); 

可以通过这2个控制下。放开循环跳转,限制跳转次数

proxy

socket 5 proxy

The answer above works pretty well, unless your country poisons DNS records as well. It is very difficult to say Java "do not use my DNS servers while connecting through proxy" as addressed in these two questions:

java runtime 6 with socks v5 proxy - Possible? http://stackoverflow.com/questions/1432038/java-runtime-6-with-socks-v5-proxy-possible

How to get URL connection using proxy in java? http://stackoverflow.com/questions/8148024/how-to-get-url-connection-using-proxy-in-java

It is also difficult for Apache HttpClient, since it also tries to resolve host names locally. By some modification to the code above, this can be dealt with:

<dependency>
	<groupId>org.apache.httpcomponents</groupId>
	<artifactId>httpclient</artifactId>
	<version>4.3.6</version>
</dependency>
package jadeutils.misc;

import static org.junit.Assert.*;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;


import java.io.IOException;
import java.io.InputStream;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.net.Socket;
import java.net.UnknownHostException;

import javax.net.ssl.SSLContext;

import org.apache.http.HttpHost;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
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.conn.ssl.SSLContexts;
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.protocol.HttpContext;
import org.apache.http.util.EntityUtils;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

public class TTsdr {

	@Before
	public void setUp() throws Exception {
	}

	@After
	public void tearDown() throws Exception {
	}

	@Test
	public void test() throws ClientProtocolException, IOException {
		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("127.0.0.1",
					7070);
			HttpClientContext context = HttpClientContext.create();
			context.setAttribute("socks.address", socksaddr);

			HttpGet request = new HttpGet("http://www.facebook.com");

			System.out.println("Executing request " + request
					+ " via SOCKS proxy " + socksaddr);
			CloseableHttpResponse response = httpclient.execute(request,
					context);
			try {
				System.out.println("----------------------------------------");
				System.out.println(response.getStatusLine());
				int i = -1;
				InputStream stream = response.getEntity().getContent();
				while ((i = stream.read()) != -1) {
					System.out.print((char) i);
				}
				EntityUtils.consume(response.getEntity());
			} finally {
				response.close();
			}
		} finally {
			httpclient.close();
		}
	}

	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);
		}
	}

}

https

import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;

import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;

import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.ssl.SSLSocketFactory;

import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.conn.PoolingClientConnectionManager;

/**
 * Set https,using SSL
 */
private static  final void setUsingSSL() {
	try {
		X509TrustManager xtm = new X509TrustManager() {
			public void checkClientTrusted(
					X509Certificate[] chain,
					String authType) throws CertificateException 
			{
			}

			public void checkServerTrusted(
					X509Certificate[] chain,
					String authType) throws CertificateException 
			{
			}

			public X509Certificate[] getAcceptedIssuers() { return null; }
		};
		
		SSLContext ctx = SSLContext.getInstance("TLS");
		ctx.init(null, new TrustManager[] { xtm }, null);

		SSLSocketFactory socketFactory = 
			new SSLSocketFactory(ctx, SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);

		httpClient.getConnectionManager().getSchemeRegistry().register(
				new Scheme("https", 443, socketFactory));
	} catch (NoSuchAlgorithmException e) {
		e.printStackTrace();
	} catch (KeyManagementException e) {
		e.printStackTrace();
	}
}

private static DefaultHttpClient httpClient;

static {
	PoolingClientConnectionManager connectPool = new PoolingClientConnectionManager();
	connectPool.setMaxTotal(20);
	httpClient = new DefaultHttpClient(connectPool);
	httpClient.getParams().setParameter("http.socket.timeout", 25000);
	httpClient.getParams().setParameter("http.connection.timeout", 25000);
	setUsingSSL();
	httpClient.getCredentialsProvider().setCredentials(
			new AuthScope(Envconsts.Host, AuthScope.ANY_PORT),
			new UsernamePasswordCredentials(Envconsts.Username,
				Envconsts.Password));
}

http2

Java8 ALPN

-Xbootclasspath/p:C:\programe\server\alpn-boot\alpn-boot-8.1.13.v20181017.jar

https://www.eclipse.org/jetty/documentation/current/alpn-chapter.html#alpn

http://unrestful.io/2015/10/10/http2-java-client-examples.html

study\okhttp\http2-examples