The QNetworkProxy class provides a network layer proxy. More...

QNetworkProxy类提供了一个网络层代理

Header: #include <QNetworkProxy>
qmake: QT += network
Since: Qt 4.1
Note: All functions in this class are reentrant.

注意:这个类中的所有函数都是可重入的。

List of all members, including inherited members

所有成员的列表,包括继承的成员
Public Types

flags Capabilities
enum Capability { TunnelingCapability, ListeningCapability, UdpTunnelingCapability, CachingCapability, HostNameLookupCapability }
enum ProxyType { NoProxy, DefaultProxy, Socks5Proxy, HttpProxy, HttpCachingProxy, FtpCachingProxy }
Public Functions

QNetworkProxy()
QNetworkProxy(ProxyType type, const QString & hostName = QString(), quint16 port = 0, const QString & user = QString(), const QString & password = QString())
QNetworkProxy(const QNetworkProxy & other)
~QNetworkProxy()
Capabilities capabilities() const
bool hasRawHeader(const QByteArray & headerName) const
QVariant header(QNetworkRequest::KnownHeaders header) const
QString hostName() const
bool isCachingProxy() const
bool isTransparentProxy() const
QString password() const
quint16 port() const
QByteArray rawHeader(const QByteArray & headerName) const
QList<QByteArray> rawHeaderList() const
void setCapabilities(Capabilities capabilities)
void setHeader(QNetworkRequest::KnownHeaders header, const QVariant & value)
void setHostName(const QString & hostName)
void setPassword(const QString & password)
void setPort(quint16 port)
void setRawHeader(const QByteArray & headerName, const QByteArray & headerValue)
void setType(QNetworkProxy::ProxyType type)
void setUser(const QString & user)
void swap(QNetworkProxy & other)
QNetworkProxy::ProxyType type() const
QString user() const
bool operator!=(const QNetworkProxy & other) const
QNetworkProxy & operator=(const QNetworkProxy & other)
bool operator==(const QNetworkProxy & other) const
Static Public Members

QNetworkProxy applicationProxy()
void setApplicationProxy(const QNetworkProxy & networkProxy)
Detailed Description详细说明

The QNetworkProxy class provides a network layer proxy.

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 and QNetworkAccessManager. 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提供了配置Qt网络类的网络层代理支持的方法。当前支持的类有QAbstractSocket、QTcpSocket、qdpsocket、QTcpServer和QNetworkAccessManager。代理支持被设计成尽可能透明。这意味着您编写的现有启用网络的应用程序应该使用以下代码自动支持网络代理。

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() and QTcpServer::setProxy(). In this way, it is possible to disable the use of a proxy for specific sockets using the following code:

设置应用程序范围代理的另一种方法是使用QAbstractSocket::setProxy()和QtcServer::setProxy()为单个套接字指定代理。通过这种方式,可以使用以下代码禁用对特定套接字的代理:

serverSocket->setProxy(QNetworkProxy::NoProxy);
Network proxy is not used if the address used in connectToHost(), bind() or listen() is equivalent to QHostAddress::LocalHost or QHostAddress::LocalHostIPv6.

如果connectToHost()、bind()或listen()中使用的地址与QHostAddress::LocalHost或QHostAddress::LocalHostIPv6等效,则不使用网络代理。

Each type of proxy support has certain restrictions associated with it. You should read the ProxyType documentation carefully before selecting a proxy type to use.

每种类型的代理支持都有一定的限制。在选择要使用的代理类型之前,应该仔细阅读ProxyType文档。

Note: Changes made to currently connected sockets do not take effect. If you need to change a connected socket, you should reconnect it.

注意:对当前连接的套接字所做的更改不会生效。如果需要更改已连接的插座,则应重新连接。

SOCKS5

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:

基于RFIS 1929的Qt 1928和RFIS。支持的身份验证方法是无身份验证和用户名/密码身份验证。同时支持IPv4和IPv6。如果启用了QNetworkProxy::HostNameLookupCapability,则通过SOCKS5服务器解析域名,否则将在本地解析域名,并将IP地址发送到服务器。在将SOCKS5与qdpsocket和QTcpServer一起使用时,需要记住以下几点:

With QUdpSocket, a call to 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() to get the actual address and port number in use. Because proxied UDP goes through two UDP connections, it is more likely that packets will be dropped.

对于QUdpSocket,对bind()的调用可能会失败,并出现超时错误。如果将非0的端口号传递给bind(),则不能保证它是将要使用的指定端口。使用localPort()和localAddress()来获取实际使用的地址和端口号。因为代理的UDP通过两个UDP连接,所以数据包更有可能被丢弃。

With QTcpServer a call to 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() to get the 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() being used.

对于QTcpServer,对listen()的调用可能会失败,并出现超时错误。如果将非0的端口号传递给listen(),则不能保证将使用的是指定的端口。使用serverPort()和serverAddress()获取用于侦听连接的实际地址和端口。SOCKS5只支持对listen()的每个调用接受一个连接,并且每次调用都可能导致使用不同的serverPort()。

See also QAbstractSocket and QTcpServer.

另请参见QAbstractSocket和QTcpServer。

Member Type Documentation

enum QNetworkProxy::Capability
flags QNetworkProxy::Capabilities

These flags indicate the capabilities that a given proxy server supports.

这些标志表示给定代理服务器支持的功能。

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().

QNetworkProxy在创建对象时默认设置不同的功能(有关默认值列表,请参见QNetworkProxy::ProxyType)。但是,可以在使用setCapabilities()创建对象后更改Capabilities。

The capabilities that QNetworkProxy supports are:

QNetworkProxy支持的功能有:

Constant Value Description定值说明
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.能够打开到远程主机的透明、隧道式TCP连接。代理服务器将传输逐字地从一端中继到另一端,并且不进行缓存。
QNetworkProxy::ListeningCapability 0x0002 Ability to create a listening socket and wait for an incoming TCP connection from a remote host.能够创建侦听套接字并等待来自远程主机的传入TCP连接。
QNetworkProxy::UdpTunnelingCapability 0x0004 Ability to relay UDP datagrams via the proxy server to and from a remote host.能够通过代理服务器在远程主机之间中继UDP数据报。
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.缓存传输内容的能力。此功能特定于每种协议和代理类型。例如,HTTP代理可以缓存使用“GET”命令传输的web数据的内容。
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.能够进行连接以在远程主机名上执行查找并连接到它,而不是要求应用程序执行名称查找并请求连接到IP地址。
This enum was introduced or modified in Qt 4.5.

The Capabilities type is a typedef for QFlags<Capability>. It stores an OR combination of Capability values.

Capabilities类型是QFlags<Capability>的typedef。它存储能力值的OR组合。

enum QNetworkProxy::ProxyType
This enum describes the types of network proxying provided in Qt.

此枚举描述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.

Qt理解两种类型的代理:透明代理和缓存代理。第一组由代理组成,可以处理任意数据传输,而第二组只能处理特定的请求。缓存代理只对可以使用它们的特定类有意义。

Constant Value Description
QNetworkProxy::NoProxy 2 No proxying is used不使用代理
QNetworkProxy::DefaultProxy 0 Proxy is determined based on the application proxy set using setApplicationProxy()使用setApplicationProxy()根据应用程序代理集确定代理
QNetworkProxy::Socks5Proxy 1 Socks5 proxying is usedSocks5 proxying is used
QNetworkProxy::HttpProxy 3 HTTP transparent proxying is used ,http透传代理
QNetworkProxy::HttpCachingProxy 4 Proxying for HTTP requests only,仅限HTTP请求的代理
QNetworkProxy::FtpCachingProxy 5 Proxying for FTP requests only,仅限FTP请求的代理
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.

下表列出了不同的代理类型及其功能。由于每个代理类型都有不同的功能,因此在选择代理类型之前了解它们是很重要的。

Proxy type Description Default capabilities,代理类型说明默认功能
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
Caching-only HTTP Implemented using normal HTTP commands, it is useful only in the context of HTTP requests (see QNetworkAccessManager) CachingCapability, HostNameLookupCapability
Caching FTP Implemented using an FTP proxy, it is useful only in the context of FTP requests (see QNetworkAccessManager) CachingCapability, HostNameLookupCapability
Also note that you shouldn't set the application default proxy (setApplicationProxy()) to a proxy that doesn't have the TunnelingCapability capability. If you do, QTcpSocket will not know how to open connections.

SOCKS 5用于任何连接的通用代理。支持TCP、UDP、绑定到端口(传入连接)和身份验证。TunnelingCapability、ListeningCapability、UdpTunnelingCapability、HostNameLookupCapability

使用“CONNECT”命令实现的HTTP,仅支持传出的TCP连接;支持身份验证。TunnelingCapability、CachingCapability、HostNameLookupCapability

仅缓存使用普通HTTP命令实现的HTTP,它仅在HTTP请求上下文中有用(请参阅QNetworkAccessManager)CachingCapability、HostNameLookupCapability

缓存使用FTP代理实现的FTP,它只在FTP请求的上下文中有用(请参阅QNetworkAccessManager)CachingCapability、HostNameLookupCapability

还请注意,不应将应用程序默认代理(setApplicationProxy())设置为不具有隧道功能的代理。如果这样做,qtcsocket将不知道如何打开连接。

See also setType(), type(), capabilities(), and setCapabilities().

Member Function Documentation

QNetworkProxy::QNetworkProxy()
Constructs a QNetworkProxy with DefaultProxy type; the proxy type is determined by applicationProxy(), which defaults to NoProxy.

构造具有DefaultProxy类型的QNetworkProxy;代理类型由applicationProxy()确定,它默认为NoProxy。

See also setType() and setApplicationProxy().

QNetworkProxy::QNetworkProxy(ProxyType type, const QString & hostName = QString(), quint16 port = 0, const QString & user = QString(), const QString & password = QString())
Constructs a QNetworkProxy with type, hostName, port, user and password.

构造一个具有类型、主机名、端口、用户和密码的QNetworkProxy。

The default capabilities for proxy type type are set automatically.

代理类型类型的默认功能是自动设置的。

See also capabilities().

QNetworkProxy::QNetworkProxy(const QNetworkProxy & other)
Constructs a copy of other.

QNetworkProxy::~QNetworkProxy()
Destroys the QNetworkProxy object.

QNetworkProxy QNetworkProxy::applicationProxy() [static]
Returns the application level network proxying.

返回应用程序级网络代理。

If a QAbstractSocket or QTcpSocket has the QNetworkProxy::DefaultProxy type, then the QNetworkProxy returned by this function is used.

如果QAbstractSocket或QTcpSocket具有QNetworkProxy::DefaultProxy类型,则使用此函数返回的QNetworkProxy。

See also QNetworkProxyFactory, setApplicationProxy(), QAbstractSocket::proxy(), and QTcpServer::proxy().

另请参见QNetworkProxyFactory、setApplicationProxy()、QAbstractSocket::proxy()和QTcpServer::proxy()。

Capabilities QNetworkProxy::capabilities() const
Returns the capabilities of this proxy server.

返回此代理服务器的功能

This function was introduced in Qt 4.5.

See also setCapabilities() and type().

bool QNetworkProxy::hasRawHeader(const QByteArray & headerName) const
Returns true if the raw header headerName is in use for this proxy. Returns false if the proxy is not of type HttpProxy or HttpCachingProxy.

如果原始标头headerName正在用于此代理,则返回true。如果代理不是HttpProxy或HttpCachingProxy类型,则返回false。

This function was introduced in Qt 5.0.

See also rawHeader() and setRawHeader().

QVariant QNetworkProxy::header(QNetworkRequest::KnownHeaders header) const
Returns the value of the known network header header if it is in use for this proxy. If it is not present, returns QVariant() (i.e., an invalid variant).

返回已知网络标头标头的值(如果该标头正用于此代理)。如果不存在,则返回QVariant()(即无效变量)。

This function was introduced in Qt 5.0.

See also QNetworkRequest::KnownHeaders, rawHeader(), and setHeader().

QString QNetworkProxy::hostName() const
Returns the host name of the proxy host.

返回代理主机的主机名。

See also setHostName(), setPort(), and port().

bool QNetworkProxy::isCachingProxy() const
Returns true if this proxy supports the QNetworkProxy::CachingCapability capability.

如果此代理支持QNetworkProxy::CachingCapability功能,则返回true。

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().

在qt4.4中,该功能与代理类型相关联,但是自从qt4.5以来,可以通过调用setCapabilities()从代理中删除缓存功能。

This function was introduced in Qt 4.4.

See also capabilities(), type(), and isTransparentProxy().

bool QNetworkProxy::isTransparentProxy() const
Returns true if this proxy supports transparent tunneling of TCP connections. This matches the QNetworkProxy::TunnelingCapability capability.

如果此代理支持TCP连接的透明隧道,则返回true。这与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().

在qt4.4中,该功能与代理类型相关联,但是自从qt4.5以来,可以通过调用setCapabilities()从代理中删除缓存功能。

This function was introduced in Qt 4.4.

See also capabilities(), type(), and isCachingProxy().

QString QNetworkProxy::password() const
Returns the password used for authentication.

返回用于身份验证的密码

See also user(), setPassword(), and setUser().

quint16 QNetworkProxy::port() const
Returns the port of the proxy host.

返回代理主机的端口。

See also setHostName(), setPort(), and hostName().

QByteArray QNetworkProxy::rawHeader(const QByteArray & headerName) const
Returns the raw form of header headerName. If no such header is present or the proxy is not of type HttpProxy or HttpCachingProxy, an empty QByteArray is returned, which may be indistinguishable from a header that is present but has no content (use hasRawHeader() to find out if the header exists or not).

返回头headerName的原始形式。如果不存在这样的头或者代理不是HttpProxy或HttpCachingProxy类型,则返回一个空的QByteArray,这可能与存在但没有内容的头无法区分(使用hasRawHeader()来确定头是否存在)。

Raw headers can be set with setRawHeader() or with setHeader().可以使用setRawHeader()或setHeader()设置原始标头。

This function was introduced in Qt 5.0.

See also header() and setRawHeader().

QList<QByteArray> QNetworkProxy::rawHeaderList() const
Returns a list of all raw headers that are set in this network proxy. The list is in the order that the headers were set.

返回在此网络代理中设置的所有原始标头的列表。列表按标题设置的顺序排列。

If the proxy is not of type HttpProxy or HttpCachingProxy an empty QList is returned.

如果代理不是HttpProxy或HttpCachingProxy类型,则返回空的QList。

This function was introduced in Qt 5.0.这个函数是在qt5.0中引入的。

See also hasRawHeader() and rawHeader().

void QNetworkProxy::setApplicationProxy(const QNetworkProxy & networkProxy) [static]
Sets the application level network proxying to be networkProxy.

将应用程序级网络代理设置为networkProxy。

If a QAbstractSocket or QTcpSocket has the QNetworkProxy::DefaultProxy type, then the QNetworkProxy set with this function is used. If you want more flexibility in determining which proxy is used, use the QNetworkProxyFactory class.

如果QAbstractSocket或QTcpSocket具有QNetworkProxy::DefaultProxy类型,则使用带有此函数的QNetworkProxy集。如果您希望在确定使用哪个代理时有更大的灵活性,请使用QNetworkProxyFactory类。

Setting a default proxy value with this function will override the application proxy factory set with QNetworkProxyFactory::setApplicationProxyFactory.

使用此函数设置默认代理值将覆盖QNetworkProxyFactory::setApplicationProxyFactory设置的应用程序代理工厂。

 

See also QNetworkProxyFactory, applicationProxy(), QAbstractSocket::setProxy(), and QTcpServer::setProxy().

另请参见QNetworkProxyFactory、applicationProxy()、QAbstractSocket::setProxy()和QTcpServer::setProxy()。

void QNetworkProxy::setCapabilities(Capabilities capabilities)
Sets the capabilities of this proxy to capabilities.

将此代理的功能设置为功能。

This function was introduced in Qt 4.5.

See also setType() and capabilities().

void QNetworkProxy::setHeader(QNetworkRequest::KnownHeaders header, const QVariant & value)
Sets the value of the known header header to be value, overriding any previously set headers. This operation also sets the equivalent raw HTTP header.

将已知标头标头的值设置为value,覆盖以前设置的任何标头。此操作还设置等效的原始HTTP头。

If the proxy is not of type HttpProxy or HttpCachingProxy this has no effect.

如果代理不是HttpProxy或HttpCachingProxy类型,则此操作无效。

This function was introduced in Qt 5.0.

See also QNetworkRequest::KnownHeaders, setRawHeader(), and header().

void QNetworkProxy::setHostName(const QString & hostName)
Sets the host name of the proxy host to be hostName.

将代理主机的主机名设置为主机名。

See also hostName(), setPort(), and port().

void QNetworkProxy::setPassword(const QString & password)
Sets the password for proxy authentication to be password.

将代理身份验证的密码设置为密码。

See also user(), setUser(), and password().

void QNetworkProxy::setPort(quint16 port)
Sets the port of the proxy host to be port.

将代理主机的端口设置为端口。

See also hostName(), setHostName(), and port().

void QNetworkProxy::setRawHeader(const QByteArray & headerName, const QByteArray & headerValue)
Sets the header headerName to be of value headerValue. If headerName corresponds to a known header (see QNetworkRequest::KnownHeaders), the raw format will be parsed and the corresponding "cooked" header will be set as well.

将标头headerName设置为值headerValue。如果headerName对应于一个已知的头(请参见QNetworkRequest::KnownHeaders),则将解析原始格式并设置相应的“熟”头。

For example:

request.setRawHeader("Last-Modified", "Sun, 06 Nov 1994 08:49:37 GMT");
will also set the known header LastModifiedHeader to be the QDateTime object of the parsed date.

请求.setRawHeader(“上次修改”,“1994年11月6日星期日08:49:37 GMT”);

也会将已知的报头LastModifiedHeader设置为解析日期的QDateTime对象。

Note: setting the same header twice overrides the previous setting. To accomplish the behaviour of multiple HTTP headers of the same name, you should concatenate the two values, separating them with a comma (",") and set one single raw header.

注意:设置相同的标题两次会覆盖先前的设置。要完成相同名称的多个HTTP报头的行为,应该将两个值连接起来,用逗号(“,”)分隔它们,并设置一个单独的raw头

If the proxy is not of type HttpProxy or HttpCachingProxy this has no effect.

如果代理不是HttpProxy或HttpCachingProxy类型,则此操作无效。

This function was introduced in Qt 5.0.

See also QNetworkRequest::KnownHeaders, setHeader(), hasRawHeader(), and rawHeader().

void QNetworkProxy::setType(QNetworkProxy::ProxyType type)
Sets the proxy type for this instance to be type.

将此实例的代理类型设置为类型。

Note that changing the type of a proxy does not change the set of capabilities this QNetworkProxy object holds if any capabilities have been set with setCapabilities().

请注意,如果使用setCapabilities()设置了任何功能,则更改代理的类型不会更改QNetworkProxy对象所拥有的功能集。

See also type() and setCapabilities().

void QNetworkProxy::setUser(const QString & user)
Sets the user name for proxy authentication to be user.

See also user(), setPassword(), and password().

void QNetworkProxy::swap(QNetworkProxy & other)
Swaps this network proxy instance with other. This function is very fast and never fails.

This function was introduced in Qt 5.0.

QNetworkProxy::ProxyType QNetworkProxy::type() const
Returns the proxy type for this instance.

See also setType().

QString QNetworkProxy::user() const
Returns the user name used for authentication.

See also setUser(), setPassword(), and password().

bool QNetworkProxy::operator!=(const QNetworkProxy & other) const
Compares the value of this network proxy to other and returns true if they differ.

This function was introduced in Qt 4.4.

QNetworkProxy & QNetworkProxy::operator=(const QNetworkProxy & other)
Assigns the value of the network proxy other to this network proxy.

This function was introduced in Qt 4.2.

bool QNetworkProxy::operator==(const QNetworkProxy & other) const
Compares the value of this network proxy to other and returns true if they are equal (same proxy type, server as well as username and password)

posted on 2020-09-27 17:24  七星落地  阅读(349)  评论(0编辑  收藏  举报