QNetworkProxy 类提供网络层代理。 更多...
QNetworkProxy 类提供网络层代理。
QNetworkProxy provides the method for configuring network layer proxy support to the Qt network classes. The currently supported classes are QAbstractSocket , QTcpSocket , QUdpSocket , QTcpServer , QNetworkAccessManager and QFtp . The proxy support is designed to be as transparent as possible. This means that existing network-enabled applications that you have written should automatically support network proxy using the following code.
QNetworkProxy proxy; proxy.setType(QNetworkProxy.Socks5Proxy); proxy.setHostName("proxy.example.com"); proxy.setPort(1080); proxy.setUser("username"); proxy.setPassword("password"); QNetworkProxy.setApplicationProxy(proxy);
An alternative to setting an application wide proxy is to specify the proxy for individual sockets using QAbstractSocket.setProxy () 和 QTcpServer.setProxy (). In this way, it is possible to disable the use of a proxy for specific sockets using the following code:
serverSocket->setProxy(QNetworkProxy.NoProxy);
网络代理不会被使用,若地址用在 connectToHost() , bind() or listen() 相当于 QHostAddress.LocalHost or QHostAddress.LocalHostIPv6 .
Each type of proxy support has certain restrictions associated with it. You should read the ProxyType 文档编制 carefully before selecting a proxy type to use.
注意: Changes made to currently connected sockets do not take effect. If you need to change a connected socket, you should reconnect it.
The SOCKS5 support in Qt 4 is based on RFC 1928 and RFC 1929 . The supported authentication methods are no authentication and username/password authentication. Both IPv4 and IPv6 are supported. Domain names are resolved through the SOCKS5 server if the QNetworkProxy.HostNameLookupCapability is enabled, otherwise they are resolved locally and the IP address is sent to the server. There are several things to remember when using SOCKS5 with QUdpSocket and QTcpServer :
With QUdpSocket 调用 bind() may fail with a timeout error. If a port number other than 0 is passed to bind() , it is not guaranteed that it is the specified port that will be used. Use localPort() and localAddress() 以获取 actual address and port number in use. Because proxied UDP goes through two UDP connections, it is more likely that packets will be dropped.
With QTcpServer 调用 listen() may fail with a timeout error. If a port number other than 0 is passed to listen() , then it is not guaranteed that it is the specified port that will be used. Use serverPort() and serverAddress() 以获取 actual address and port used to listen for connections. SOCKS5 only supports one accepted connection per call to listen() , and each call is likely to result in a different serverPort() 被使用。
These flags indicate the capabilities that a given proxy server 支持。
QNetworkProxy sets different capabilities by default when the object is created (see QNetworkProxy.ProxyType for a list of the defaults). However, it is possible to change the capabitilies after the object has been created with setCapabilities ().
The capabilities that QNetworkProxy supports are:
| 常量 | 值 | 描述 |
|---|---|---|
| QNetworkProxy.TunnelingCapability | 0x0001 | Ability to open transparent, tunneled TCP connections to a remote host. The proxy server relays the transmission verbatim from one side to the other and does no caching. |
| QNetworkProxy.ListeningCapability | 0x0002 | Ability to create a listening socket and wait for an incoming TCP connection from a remote host. |
| QNetworkProxy.UdpTunnelingCapability | 0x0004 | Ability to relay UDP datagrams via the proxy server to and from a remote host. |
| QNetworkProxy.CachingCapability | 0x0008 | Ability to cache the contents of the transfer. This capability is specific to each protocol and proxy type. For example, HTTP proxies can cache the contents of web data transferred with "GET" commands. |
| QNetworkProxy.HostNameLookupCapability | 0x0010 | Ability to connect to perform the lookup on a remote host name and connect to it, as opposed to requiring the application to perform the name lookup and request connection to IP addresses only. |
该枚举在 Qt 4.5 引入或被修改。
Capabilities 类型是 typedef 对于 QFlags <Capability>. It stores an OR combination of Capability values.
This enum describes the types of network proxying provided in Qt.
There are two types of proxies that Qt understands: transparent proxies and caching proxies. The first group consists of proxies that can handle any arbitrary data transfer, while the second can only handle specific requests. The caching proxies only make sense for the specific classes where they can be used.
| 常量 | 值 | 描述 |
|---|---|---|
| QNetworkProxy.NoProxy | 2 | 不使用代理 |
| QNetworkProxy.DefaultProxy | 0 | Proxy is determined based on the application proxy set using setApplicationProxy () |
| QNetworkProxy.Socks5Proxy | 1 | Socks5 代理被使用 |
| QNetworkProxy.HttpProxy | 3 | HTTP transparent proxying is used (This value was introduced in 4.3.) |
| QNetworkProxy.HttpCachingProxy | 4 | Proxying for HTTP requests only (This value was introduced in 4.4.) |
| QNetworkProxy.FtpCachingProxy | 5 | Proxying for FTP requests only (This value was introduced in 4.4.) |
The table below lists different proxy types and their capabilities. Since each proxy type has different capabilities, it is important to understand them before choosing a proxy type.
| 代理类型 | 描述 | 默认能力 |
|---|---|---|
| SOCKS 5 | Generic proxy for any kind of connection. Supports TCP, UDP, binding to a port (incoming connections) and authentication. | TunnelingCapability , ListeningCapability , UdpTunnelingCapability , HostNameLookupCapability |
| HTTP | Implemented using the "CONNECT" command, supports only outgoing TCP connections; supports authentication. | TunnelingCapability , CachingCapability , HostNameLookupCapability |
| 仅缓存 HTTP | Implemented using normal HTTP commands, it is useful only in the context of HTTP requests (see QNetworkAccessManager ) | CachingCapability , HostNameLookupCapability |
| 缓存 FTP | Implemented using an FTP proxy, it is useful only in the context of FTP requests (see QFtp , QNetworkAccessManager ) | CachingCapability , HostNameLookupCapability |
Also note that you shouldn't set the application default proxy ( setApplicationProxy ()) to a proxy that doesn't have the TunnelingCapability 能力。若做了, QTcpSocket 将不知道如何打开连接。
另请参阅 setType (), type (), capabilities (),和 setCapabilities ().
构造 QNetworkProxy with DefaultProxy type; the proxy type is determined by applicationProxy (), which 默认为 NoProxy .
另请参阅 setType () 和 setApplicationProxy ().
构造 QNetworkProxy with type , hostName , port , user and password .
默认能力的代理类型 type are set automatically.
另请参阅 capabilities ().
构造副本为 other .
返回应用程序级别的网络代理。
若 QAbstractSocket or QTcpSocket 拥有 QNetworkProxy.DefaultProxy 类型,那么 QNetworkProxy 被返回通过使用此函数。
另请参阅 QNetworkProxyFactory , setApplicationProxy (), QAbstractSocket.proxy (), and QTcpServer.proxy ().
返回此代理服务器的能力。
该函数在 Qt 4.5 引入。
另请参阅 setCapabilities () 和 type ().
返回代理主机的主机名。
另请参阅 setHostName (), setPort (),和 port ().
Returns true if this proxy supports the QNetworkProxy.CachingCapability 能力。
In Qt 4.4, the capability was tied to the proxy type, but since Qt 4.5 it is possible to remove the capability of caching from a proxy by calling setCapabilities ().
该函数在 Qt 4.4 引入。
另请参阅 capabilities (), type (),和 isTransparentProxy ().
Returns true if this proxy supports transparent tunneling of TCP connections. This matches the QNetworkProxy.TunnelingCapability 能力。
In Qt 4.4, the capability was tied to the proxy type, but since Qt 4.5 it is possible to remove the capability of caching from a proxy by calling setCapabilities ().
该函数在 Qt 4.4 引入。
另请参阅 capabilities (), type (),和 isCachingProxy ().
返回用于身份验证的口令。
另请参阅 user (), setPassword (),和 setUser ().
返回代理主机的端口。
另请参阅 setHostName (), setPort (),和 hostName ().
把应用程序级别的网络代理设为 networkProxy .
若 QAbstractSocket or QTcpSocket 拥有 QNetworkProxy.DefaultProxy 类型,那么 QNetworkProxy set with this function is used. If you want more flexibility in determining which the proxy, use the QNetworkProxyFactory 类。
Setting a default proxy value with this function will override the application proxy factory set with QNetworkProxyFactory.setApplicationProxyFactory.
另请参阅 QNetworkProxyFactory , applicationProxy (), QAbstractSocket.setProxy (), and QTcpServer.setProxy ().
将此代理的能力设为 capabilities .
该函数在 Qt 4.5 引入。
另请参阅 setType () 和 capabilities ().
把代理主机的主机名设为 hostName .
另请参阅 hostName (), setPort (),和 port ().
把代理身份验证的口令设为 password .
另请参阅 user (), setUser (),和 password ().
把代理主机的端口设为 port .
另请参阅 hostName (), setHostName (),和 port ().
把此实例的代理类型设为 type .
Note that changing the type of a proxy does not change the set of capabilities this QNetworkProxy 对象保持的,若有设置任何能力采用 setCapabilities ().
另请参阅 type () 和 setCapabilities ().
把代理身份验证的用户名设为 user .
另请参阅 user (), setPassword (),和 password ().
返回此实例的代理类型。
另请参阅 setType ().
返回用于身份验证的用户名。
另请参阅 setUser (), setPassword (),和 password ().