QNetworkProxy Class▲
-
Header: QNetworkProxy
-
CMake:
find_package(Qt6 REQUIRED COMPONENTS Network)
target_link_libraries(mytarget PRIVATE Qt6::Network)
-
qmake: QT += network
-
Group: QNetworkProxy is part of Network Programming API, shared
Detailed Description▲
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 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:
serverSocket-&
gt;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.
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.
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 since 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, 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.
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.
See Also▲
See also 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 capabilities after the object has been created with setCapabilities().
The capabilities that QNetworkProxy supports are:
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. |
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. |
QNetworkProxy::SctpTunnelingCapability |
0x00020 |
Ability to open transparent, tunneled SCTP connections to a remote host. |
QNetworkProxy::SctpListeningCapability |
0x00040 |
Ability to create a listening socket and wait for an incoming SCTP connection from a remote host. |
The Capabilities type is a typedef for QFlags<Capability>. It stores an OR combination of Capability values.
enum QNetworkProxy::ProxyType▲
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.
Constant |
Value |
Description |
---|---|---|
QNetworkProxy::NoProxy |
2 |
No proxying is used |
QNetworkProxy::DefaultProxy |
0 |
Proxy is determined based on the application proxy set using setApplicationProxy() |
QNetworkProxy::Socks5Proxy |
1 |
Socks5 proxying is used |
QNetworkProxy::HttpProxy |
3 |
HTTP transparent proxying is used |
QNetworkProxy::HttpCachingProxy |
4 |
Proxying for HTTP requests only |
QNetworkProxy::FtpCachingProxy |
5 |
Proxying for FTP requests only |
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) |
|
Caching FTP |
Implemented using an FTP proxy, it is useful only in the context of FTP requests (see QNetworkAccessManager) |
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.
See Also▲
See also setType(), type(), capabilities(), setCapabilities()
Member Function Documentation▲
QNetworkProxy::QNetworkProxy()▲
Constructs a QNetworkProxy with DefaultProxy type.
The proxy type is determined by applicationProxy(), which defaults to NoProxy or a system-wide proxy if one is configured.
See Also▲
See also setType(), setApplicationProxy()
QNetworkProxy::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.
The default capabilities for proxy type type are set automatically.
See Also▲
See also capabilities()
QNetworkProxy::QNetworkProxy(const QNetworkProxy &other)▲
Constructs a copy of other.
QNetworkProxy::~QNetworkProxy()▲
Destroys the QNetworkProxy object.
[static] QNetworkProxy QNetworkProxy::applicationProxy()▲
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.
See Also▲
See also QNetworkProxyFactory, setApplicationProxy(), QAbstractSocket::proxy(), QTcpServer::proxy()
QNetworkProxy::Capabilities QNetworkProxy::capabilities() const▲
[since 5.0] 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.
This function was introduced in Qt 5.0.
See Also▲
See also rawHeader(), setRawHeader()
[since 5.0] 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).
This function was introduced in Qt 5.0.
See Also▲
See also QNetworkRequest::KnownHeaders, rawHeader(), setHeader()
QString QNetworkProxy::hostName() const▲
bool QNetworkProxy::isCachingProxy() const▲
Returns true if this proxy supports the QNetworkProxy::CachingCapability capability.
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().
See Also▲
See also capabilities(), type(), isTransparentProxy()
bool QNetworkProxy::isTransparentProxy() const▲
Returns true if this proxy supports transparent tunneling of TCP connections. This matches the QNetworkProxy::TunnelingCapability capability.
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().
See Also▲
See also capabilities(), type(), isCachingProxy()
QString QNetworkProxy::password() const▲
quint16 QNetworkProxy::port() const▲
[since 5.0] 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).
Raw headers can be set with setRawHeader() or with setHeader().
This function was introduced in Qt 5.0.
See Also▲
See also header(), setRawHeader()
[since 5.0] 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.
This function was introduced in Qt 5.0.
See Also▲
See also hasRawHeader(), rawHeader()
[static] void QNetworkProxy::setApplicationProxy(const QNetworkProxy &networkProxy)▲
Sets the application level network proxying to be 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.
Setting a default proxy value with this function will override the application proxy factory set with QNetworkProxyFactory::setApplicationProxyFactory, and disable the use of a system proxy.
See Also▲
See also QNetworkProxyFactory, applicationProxy(), QAbstractSocket::setProxy(), QTcpServer::setProxy()
void QNetworkProxy::setCapabilities(QNetworkProxy::Capabilities capabilities)▲
[since 5.0] 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.
If the proxy is not of type HttpProxy or HttpCachingProxy this has no effect.
This function was introduced in Qt 5.0.
See Also▲
See also QNetworkRequest::KnownHeaders, setRawHeader(), header()
void QNetworkProxy::setHostName(const QString &hostName)▲
Sets the host name of the proxy host to be hostName.
See Also▲
void QNetworkProxy::setPassword(const QString &password)▲
Sets the password for proxy authentication to be password.
See Also▲
void QNetworkProxy::setPort(quint16 port)▲
[since 5.0] 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.
For example:
request.setRawHeader(QByteArray("Last-Modified"
), QByteArray("Sun, 06 Nov 1994 08:49:37 GMT"
));
will also set the known header LastModifiedHeader to be the QDateTime object of the parsed date.
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.
If the proxy is not of type HttpProxy or HttpCachingProxy this has no effect.
This function was introduced in Qt 5.0.
See Also▲
See also QNetworkRequest::KnownHeaders, setHeader(), hasRawHeader(), 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().
See Also▲
See also type(), setCapabilities()
void QNetworkProxy::setUser(const QString &user)▲
Sets the user name for proxy authentication to be user.
See Also▲
See also user(), setPassword(), password()
[since 5.0] 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▲
QString QNetworkProxy::user() const▲
Returns the user name used for authentication.
See Also▲
See also setUser(), setPassword(), password()
bool QNetworkProxy::operator!=(const QNetworkProxy &other) const▲
Compares the value of this network proxy to other and returns true if they differ.
QNetworkProxy &QNetworkProxy::operator=(const QNetworkProxy &other)▲
Assigns the value of the network proxy other to this network proxy.
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)