Java Core Learning (35) Network Programming---proxy server

Source: Internet
Author: User

First, the function of the proxy server

1, break through their own IP restrictions, to hide their own IP address, access to restricted sites.

2, improve access speed, Proxy server equivalent to a layer of cache.

Second, directly use proxy to create the connection

A proxy has a constructor: proxy (Proxy.type type,socketaddress sa), which is used to create a proxy object that represents an agent server, where the SA parameter specifies the address of the proxy server, and the type represents the kind of the proxy server. Is an enumeration object, type has three kinds:

Proxy.Type.DIRECT: Direct connection, no proxy

Proxy.Type.HTTP: Indicates support for advanced protocol proxies, such as FTP, HTTP

Proxy.Type.SOCKS: Represents SOCKS (V4 or V5) proxy

You can then pass in the proxy object when you create the URLConnection or the socket.

Iii. using Proxyselector to automatically select a proxy server

When using the proxy class, you need to manually specify a proxy server each time you create a connection, while using the Proxyselector class you can specify a proxy server for all network requests, and Proxyselector represents a proxy selector, which is an abstract class. A program can implement its own proxy selector by inheriting it, and the following two abstract methods need to be implemented:

List<proxy> Select (Uri uri): Returns a list of proxy servers based on business needs.

Connectfailed (URI uri,socketaddress sa,ioexception IoE): This method is invoked when a connection proxy fails.

After implementing its own proxy selector, a static method called Proxyselector in the program SetDefault () registers the proxy selector.

The static method called Proxyselector Getdefault () can get an instance of Sun.net.spi.DefaultProxySelector, which is the default proxy selector, and the usage of it is checked out.

Java Core Learning (35) Network Programming---proxy server

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.