|  | Home · All Classes · Main Classes · Grouped Classes · Modules · Functions |

# QNetworkProxy Class Reference
[QtNetwork module]

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

    #include <QNetworkProxy>

Note: All the functions in this class are reentrant.

This class was introduced in Qt 4.1.

- List of all members, including inherited members

### Public Types

- enum ProxyType { DefaultProxy, Socks5Proxy, NoProxy }

### 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 ()
- QString hostName () const
- QString password () const
- quint16 port () const
- void setHostName ( const QString & hostName )
- void setPassword ( const QString & password )
- void setPort ( quint16 port )
- void setType ( QNetworkProxy::ProxyType type )
- void setUser ( const QString & user )
- QNetworkProxy::ProxyType type () const
- QString user () const
- QNetworkProxy & operator= ( const QNetworkProxy & other )

### Static Public Members

- QNetworkProxy applicationProxy ()
- void setApplicationProxy ( const QNetworkProxy & networkProxy )

## Detailed Description

The QNetworkProxy class provides a network layer proxy.

QNetworkProxy provides the method for configuring network layer proxy support to the Qt network classes. The currently supported classes are QAbstractSocket, QTcpSocket, QUdpSocket, QTcpServer, QHttp 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() and 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);

Network proxy is used if the address used in connectToHost(), bind() or listen() is equivilent to QHostAddress::LocalHost or QHostAddress::LocalHostIPv6.

When enabling proxy support there are certain restrictions depending on the proxy type. You should read the documentation of the proxy type carefully before using 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, but resolving domain names through the SOCKS server is not; i.e. all domain names are resolved locally. There are several things to remmber 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(), then 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 listened on. SOCKS5 only supports one accepted connection per call to listen(), and each call is likely to result in a different serverPort().

See also QAbstractSocket and QTcpServer.

## Member Type Documentation

### enum QNetworkProxy::ProxyType

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

Constant | Value | Description
--- | --- | ---
`QNetworkProxy::DefaultProxy` | `0` | Proxy is determined based on the application proxy set using setApplicationProxy()
`QNetworkProxy::Socks5Proxy` | `1` | Socks5 proxying is used
`QNetworkProxy::NoProxy` | `2` | No proxying is used

See also setType() and type().

## Member Function Documentation

### QNetworkProxy::QNetworkProxy ()

Constructs a QNetworkProxy with QNetworkProxy::DefaultProxy type.

### 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::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.

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

### QString QNetworkProxy::hostName () const

Returns the host name of the proxy host.

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

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

### void QNetworkProxy::setApplicationProxy ( const QNetworkProxy & networkProxy ) `[static]`

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.

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

### 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::setType ( QNetworkProxy::ProxyType type )

Sets the proxy type for this instance to be type.

See also type().

### void QNetworkProxy::setUser ( const QString & user )

Sets the user name for proxy authentication to be user.

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

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

### QNetworkProxy & QNetworkProxy::operator= ( const QNetworkProxy & other )

Assigns the value of the network proxy other to this network proxy.