When testing Socket programming again today, I cannot connect to the external network. The company uses Http agent. I didn't understand the search online, so I spent a lot of time studying. Only after seeing the good relationship between HTTP and TCP protocols can you understand. Now that you can use HTTP proxy through Socket, the result is very simple, but the process is very difficult.
1. Let’s briefly talk about HTTP and TCP (you can Google the specific content yourself, and the information is very complete). Here are the key points:
HTTP: It is an application layer protocol, based on the transport layer protocol.
TCP: It is a transport layer protocol, based on the network layer protocol.
IP: It is a network layer protocol.
A TCP connection requires three handshakes (just like transferring a household registration, not detailed). HTTP is just an application protocol, which is equivalent to a custom protocol, that is, it does not interfere with the underlying transmission method, but only the data content format. Defined. For example, if someone says "SB" (your name), you answer "Yes", it is just the content format, and it does not change the transmission method of sound (transmitted through sound waves <network hardware medium>, through languages that both parties can understand. <TCP/IP>). Similarly, FTP and Telnet are also an application-layer protocol. For example, when others say "SB", you answer "hey", but the format and content are different.
2. After realizing the above, let’s talk about HTTP proxy. From the above, it can be understood that the HTTP proxy server is such a machine: you send all HTTP requests (whether you want to request Baidu or Google) to this HTTP proxy server. Then this HTTP proxy server requests the final address you want to access and passes the response back to you. It should also be noted here that it proxys the HTTP protocol, and HTTP is based on TCP, which means that this server proxys the TCP connection with the specified HTTP content format. It's boring to continue, look at the following code:
The code copy is as follows:
//The following address is the address of the proxy server
Socket socket = new Socket("10.1.2.188", 80);
//The content written and written is content that follows the HTTP request protocol format, request Baidu
socket.getOutputStream().write(new String("GET http://www.baidu.com/ HTTP/1.1/r/n/r/n").getBytes());
byte[] bs = new byte[1024];
InputStream is = socket.getInputStream();
int i;
while ((i = is.read(bs)) > 0) {
System.out.println(new String(bs, 0, i));
}
is.close();
Of course, in Java, there is a Proxy proxy to surf the Internet. At this time, using URL (HTTP) does not involve Socket (TCP). See the following code
The code copy is as follows:
//Set the proxy
System.setProperty("http.proxySet", "true");
System.setProperty("http.proxyHost", "10.1.2.188");
System.setProperty("http.proxyPort", "80");
//Directly access the destination address
URL url = new URL("http://www.baidu.com");
URLConnection con = url.openConnection();
InputStreamReader isr = new InputStreamReader(con.getInputStream());
char[] cs = new char[1024];
int i = 0;
while ((i = isr.read(cs)) > 0) {
System.out.println(new String(cs, 0, i));
}
isr.close();
Finally, let’s summarize:
In an environment using HTTP proxy,
If you use Socket (TCP) to connect to the external network, you will directly connect to the proxy server and then specify the external network URL to forward in the sent HTTP request.
If you use URL (HTTP) to connect to the external network, you need to set HTTP proxy parameters or use Proxy.
OK, you can use it as you like after you understand. Look at the following code, an example of using NIO Socket to access the external network through HTTP proxy:
The code copy is as follows:
SocketChannel sc = SocketChannel.open(new InetSocketAddress("10.1.2.188", 80));
sc.write(Charset.forName("utf8").encode("GET http://www.baidu.com/ HTTP/1.1/r/n/r/n"));
ByteBuffer buffer = ByteBuffer.allocate(1024);
while (sc.read(buffer) != -1) {
buffer.flip();
System.out.println(Charset.forName("utf8").decode(buffer));
buffer.clear();
}
sc.close();
Example of adding proxy to Java Socket programming
Sometimes our network cannot be directly connected to the external network. We need to use http, https or socket proxy to connect to the external network. Here are some methods for Java to use proxy to connect to the external network: Method 1: Use system properties to complete the proxy Setting up, this method is relatively simple, but it is not possible to set up a proxy for a separate connection:
The code copy is as follows:
public static void main(String[] args) {
Properties prop = System.getProperties();
// Set the address of the proxy server to use when accessing http
prop.setProperty("http.proxyHost", "192.168.0.254");
// Set the port of http access to the proxy server to use
prop.setProperty("http.proxyPort", "8080");
// Set up a host that does not need to be accessed through a proxy server. You can use the * wildcard character, and multiple addresses are separated by |
prop.setProperty("http.nonProxyHosts", "localhost|192.168.0.*");
// Set the proxy server address and port for secure access
// It does not have the https.nonProxyHosts property, it is accessed according to the rules set in http.nonProxyHosts
prop.setProperty("https.proxyHost", "192.168.0.254");
prop.setProperty("https.proxyPort", "443");
// Hosts, ports that use ftp proxy servers and hosts that do not require ftp proxy servers
prop.setProperty("ftp.proxyHost", "192.168.0.254");
prop.setProperty("ftp.proxyPort", "2121");
prop.setProperty("ftp.nonProxyHosts", "localhost|192.168.0.*");
// The address and port of the socks proxy server
prop.setProperty("socksProxyHost", "192.168.0.254");
prop.setProperty("socksProxyPort", "8000");
// Set the username and password to log in to the proxy server
Authenticator.setDefault(new MyAuthenticator("userName", "Password"));
}
static class MyAuthenticator extends Authenticator {
private String user = "";
private String password = "";
public MyAuthenticator(String user, String password) {
this.user = user;
this.password = password;
}
protected PasswordAuthentication getPasswordAuthentication() {
returnnew PasswordAuthentication(user, password.toCharArray());
}
}
Method 2 uses Proxy to implement proxy for each connection. This method can only be used in jdk 1.5 or above (including jdk 1.5). The advantage is that the proxy for each connection can be set separately, and the disadvantage is that it is more troublesome to set:
The code copy is as follows:
public static void main(String[] args) {
try {
URL url = new URL("http://www.baidu.com");
// Create a proxy server
InetSocketAddress addr = new InetSocketAddress("192.168.0.254",
8080);
// Proxy proxy = new Proxy(Proxy.Type.SOCKS, addr); // Socket proxy
Proxy proxy = new Proxy(Proxy.Type.HTTP, addr); // http proxy
// If we know the name of the proxy server, we can use it directly
// Finish
URLConnection conn = url.openConnection(proxy);
InputStream in = conn.getInputStream();
// InputStream in = url.openStream();
String s = IOUtils.toString(in);
System.out.println(s);
} catch (Exception e) {
e.printStackTrace();
}
}