wxWidgets: wxHTTP Class Reference

| Version: 3.1.5

Loading...
Searching...
No Matches
Public Member Functions | List of all members
wxHTTP Class Reference
Class List by Category » Networking

`#include </cygdrive/c/Users/Hamis/Cygwin-packaging/wxWidgets3.1/wxWidgets3.1-3.1.5-4.x86_64/src/wxWidgets-3.1.5/interface/wx/protocol/http.h>`

Inheritance diagram for wxHTTP:
[legend]

## Detailed Description

wxHTTP can be used to establish a connection to an HTTP server.

wxHTTP can thus be used to create a (basic) HTTP client.

Note
  In practice, for any but the most trivial cases, e.g. if you need HTTPS, HTTP/2 or IPv6, proxy detection, authentication, etc. support please use wxWebRequest instead.

##

Library: wxNet

<>< =''>:</>&;&;< =''>\ </></>

See also
  wxSocketBase, wxURL

## Public Member Functions
| wxHTTP ()
| Default constructor.
virtual | ~wxHTTP ()
| Destructor will close the connection if connected.
virtual bool | Connect (const wxString &host)
| Connect to the HTTP server.
virtual bool | Connect (const wxString &host, unsigned short port)
virtual bool | Connect (const wxSockAddress &addr, bool wait)
| Connects to a server using the specified address.
wxString | GetHeader (const wxString &header) const
| Returns the data attached with a field whose name is specified by header.
virtual wxInputStream * | GetInputStream (const wxString &path)
| Creates a new input stream on the specified path.
int | GetResponse () const
| Returns the HTTP response code returned by the server.
void | SetMethod (const wxString &method)
| Set HTTP method.
void | SetHeader (const wxString &header, const wxString &h_data)
| It sets data of a field to be sent during the next request to the HTTP server.
wxString | GetCookie (const wxString &cookie) const
| Returns the value of a cookie.
bool | HasCookies () const
| Returns true if there were cookies.
bool | SetPostBuffer (const wxString &contentType, const wxMemoryBuffer &data)
| Set the binary data to be posted to the server.
bool | SetPostText (const wxString &contentType, const wxString &data, const wxMBConv &conv=wxConvUTF8)
| Set the text to be posted to the server.
Public Member Functions inherited from wxProtocol
virtual bool | Abort ()=0
| Abort the current stream.
virtual wxString | GetContentType () const
| Returns the type of the content of the last opened stream.
virtual wxProtocolError | GetError () const
| Returns the last occurred error.
bool | Reconnect ()
| Tries to reestablish a previous opened connection (close and renegotiate connection).
virtual void | SetPassword (const wxString &user)
| Sets the authentication password.
virtual void | SetUser (const wxString &user)
| Sets the authentication user.
void | SetDefaultTimeout (wxUint32 Value)
| Sets a new default timeout for the network operations.
void | SetLog (wxProtocolLog *log)
| Set the logger, deleting the old one and taking ownership of this one.
wxProtocolLog * | GetLog () const
| Return the current logger, may be NULL.
wxProtocolLog * | DetachLog ()
| Detach the existing logger without deleting it.
void | LogRequest (const wxString &str)
| Call wxProtocolLog::LogRequest() if we have a valid logger or do nothing otherwise.
void | LogResponse (const wxString &str)
| Call wxProtocolLog::LogResponse() if we have a valid logger or do nothing otherwise.
Public Member Functions inherited from wxSocketClient
| wxSocketClient (wxSocketFlags flags=wxSOCKET_NONE)
| Constructor.
virtual | ~wxSocketClient ()
| Destructor.
bool | Connect (const wxSockAddress &address, const wxSockAddress &local, bool wait=true)
| Connects to a server using the specified address.
bool | WaitOnConnect (long seconds=-1, long milliseconds=0)
| Wait until a connection request completes, or until the specified timeout elapses.
Public Member Functions inherited from wxSocketBase
bool | Error () const
| Returns true if an error occurred in the last IO operation.
virtual bool | GetLocal (wxSockAddress &addr) const
| Return the local address of the socket.
virtual bool | GetPeer (wxSockAddress &addr) const
| Return the peer address field of the socket.
long | GetTimeout () const
| Return the socket timeout in seconds.
bool | IsConnected () const
| Returns true if the socket is connected.
bool | IsData ()
| Check if the socket can be currently read or written.
bool | IsDisconnected () const
| Returns true if the socket is not connected.
bool | IsOk () const
| Returns true if the socket is initialized and ready and false in other cases.
wxUint32 | LastCount () const
| Returns the number of bytes read or written by the last IO call.
wxUint32 | LastReadCount () const
| Returns the number of bytes read by the last Read() or ReadMsg() call (receive direction only).
wxUint32 | LastWriteCount () const
| Returns the number of bytes written by the last Write() or WriteMsg() call (transmit direction only).
wxSocketError | LastError () const
| Returns the last wxSocket error.
void | RestoreState ()
| Restore the previous state of the socket, as saved with SaveState().
void | SaveState ()
| Save the current state of the socket in a stack.
virtual bool | Close ()
| Shut down the socket, disabling further transmission and reception of data and disable events for the socket and frees the associated system resources.
void | ShutdownOutput ()
| Shuts down the writing end of the socket.
wxSocketBase & | Discard ()
| Delete all bytes in the incoming queue.
wxSocketFlags | GetFlags () const
| Returns current IO flags, as set with SetFlags().
void | InterruptWait ()
| Use this function to interrupt any wait operation currently in progress.
wxSocketBase & | Peek (void *buffer, wxUint32 nbytes)
| Peek into the socket by copying the next bytes which would be read by Read() into the provided buffer.
wxSocketBase & | Read (void *buffer, wxUint32 nbytes)
| Read up to the given number of bytes from the socket.
wxSocketBase & | ReadMsg (void *buffer, wxUint32 nbytes)
| Receive a message sent by WriteMsg().
void | SetFlags (wxSocketFlags flags)
| Use SetFlags to customize IO operation for this socket.
virtual bool | SetLocal (const wxIPV4address &local)
| Set the local address and port to use.
void | SetTimeout (long seconds)
| Set the default socket timeout in seconds.
wxSocketBase & | Unread (const void *buffer, wxUint32 nbytes)
| Put the specified data into the input queue.
bool | Wait (long seconds=-1, long millisecond=0)
| Wait for any socket event.
bool | WaitForLost (long seconds=-1, long millisecond=0)
| Wait until the connection is lost.
bool | WaitForRead (long seconds=-1, long millisecond=0)
| Wait until the socket is readable.
bool | WaitForWrite (long seconds=-1, long millisecond=0)
| Wait until the socket becomes writable.
wxSocketBase & | Write (const void *buffer, wxUint32 nbytes)
| Write up to the given number of bytes to the socket.
wxSocketBase & | WriteMsg (const void *buffer, wxUint32 nbytes)
| Sends a buffer which can be read using ReadMsg().
void * | GetClientData () const
| Returns a pointer of the client data for this socket, as set with SetClientData().
void | Notify (bool notify)
| According to the notify value, this function enables or disables socket events.
void | SetClientData (void *data)
| Sets user-supplied client data for this socket.
void | SetEventHandler (wxEvtHandler &handler, int id=-1)
| Sets an event handler to be called when a socket event occurs.
void | SetNotify (wxSocketEventFlags flags)
| Specifies which socket events are to be sent to the event handler.
wxSOCKET_T | GetSocket () const
| Returns the native socket descriptor.
| wxSocketBase ()
| Default constructor.
virtual | ~wxSocketBase ()
| Destructor.
bool | Destroy ()
| Destroys the socket safely.
Public Member Functions inherited from wxObject
| wxObject ()
| Default ctor; initializes to NULL the internal reference data.
| wxObject (const wxObject &other)
| Copy ctor.
virtual | ~wxObject ()
| Destructor.
virtual wxClassInfo * | GetClassInfo () const
| This virtual function is redefined for every class that requires run-time type information, when using the wxDECLARE_CLASS macro (or similar).
wxObjectRefData * | GetRefData () const
| Returns the wxObject::m_refData pointer, i.e. the data referenced by this object.
bool | IsKindOf (const wxClassInfo *info) const
| Determines whether this class is a subclass of (or the same class as) the given class.
bool | IsSameAs (const wxObject &obj) const
| Returns true if this object has the same data pointer as obj.
void | Ref (const wxObject &clone)
| Makes this object refer to the data in clone.
void | SetRefData (wxObjectRefData *data)
| Sets the wxObject::m_refData pointer.
void | UnRef ()
| Decrements the reference count in the associated data, and if it is zero, deletes the data.
void | UnShare ()
| This is the same of AllocExclusive() but this method is public.
void | operator delete (void *buf)
| The delete operator is defined for debugging versions of the library only, when the identifier `__WXDEBUG__` is defined.
void * | operator new (size_t size, const wxString &filename=NULL, int lineNum=0)
| The new operator is defined for debugging versions of the library only, when the identifier `__WXDEBUG__` is defined.

## Additional Inherited Members
static bool | Initialize ()
| Perform the initialization needed in order to use the sockets.
static void | Shutdown ()
| Shut down the sockets.
Protected Member Functions inherited from wxObject
void | AllocExclusive ()
| Ensure that this object's data is not shared with any other object.
virtual wxObjectRefData * | CreateRefData () const
| Creates a new instance of the wxObjectRefData-derived class specific to this object and returns it.
virtual wxObjectRefData * | CloneRefData (const wxObjectRefData *data) const
| Creates a new instance of the wxObjectRefData-derived class specific to this object and initializes it copying data.
Protected Attributes inherited from wxObject
wxObjectRefData * | m_refData
| Pointer to an object which is the object's reference-counted data.

## Constructor & Destructor Documentation

## ◆ wxHTTP()

wxHTTP::wxHTTP | ( |  | ) |

Default constructor.

## ◆ ~wxHTTP()

virtual wxHTTP::~wxHTTP | ( |  | ) |
| virtual

Destructor will close the connection if connected.

## Member Function Documentation

## ◆ Connect() [1/3]

virtual bool wxHTTP::Connect | ( | const wxSockAddress & | address,
|  | bool | wait )
| virtual

Connects to a server using the specified address.

If wait is true, Connect() will wait until the connection completes.

Warning
  This method will block the GUI.

If wait is false, Connect() will try to establish the connection and return immediately, without blocking the GUI. When used this way, even if Connect() returns false, the connection request can be completed later. To detect this, use WaitOnConnect(), or catch wxSOCKET_CONNECTION events (for successful establishment) and wxSOCKET_LOST events (for connection failure).

Parameters

  address | Address of the server.
  wait | If true, waits for the connection to complete.

Returns
  true if the connection is established and no error occurs. If wait was true, and Connect() returns false, an error occurred and the connection failed. If wait was false, and Connect() returns false, you should still be prepared to handle the completion of this connection request, either with WaitOnConnect() or by watching wxSOCKET_CONNECTION and wxSOCKET_LOST events.

See also
  WaitOnConnect(), wxSocketBase::SetNotify(), wxSocketBase::Notify()

Reimplemented from wxSocketClient.

## ◆ Connect() [2/3]

virtual bool wxHTTP::Connect | ( | const wxString & | host | ) |
| virtual

Connect to the HTTP server.

By default, connection is made to the port 80 of the specified host. You may connect to a non-default port by specifying it explicitly using the second overload.

Currently wxHTTP only supports IPv4.

For the overload taking wxSockAddress, the wait argument is ignored.

## ◆ Connect() [3/3]

virtual bool wxHTTP::Connect | ( | const wxString & | host,
|  | unsigned short | port )
| virtual

## ◆ GetCookie()

wxString wxHTTP::GetCookie | ( | const wxString & | cookie | ) | const

Returns the value of a cookie.

## ◆ GetHeader()

wxString wxHTTP::GetHeader | ( | const wxString & | header | ) | const

Returns the data attached with a field whose name is specified by header.

If the field doesn't exist, it will return an empty string and not a NULL string.

Note
  The header is not case-sensitive, i.e. "CONTENT-TYPE" and "content-type" represent the same header.

## ◆ GetInputStream()

virtual wxInputStream * wxHTTP::GetInputStream | ( | const wxString & | path | ) |
| virtual

Creates a new input stream on the specified path.

Notice that this stream is unseekable, i.e. SeekI() and TellI() methods shouldn't be used.

Note that you can still know the size of the file you are getting using wxStreamBase::GetSize(). However there is a limitation: in HTTP protocol, the size is not always specified so sometimes ``(size_t)-1 can returned to indicate that the size is unknown. In such case, you may want to use wxInputStream::LastRead() method in a loop to get the total size.

Returns
  Returns the initialized stream. You must delete it yourself once you don't use it anymore and this must be done before the wxHTTP object itself is destroyed. The destructor closes the network connection. The next time you will try to get a file the network connection will have to be reestablished, but you don't have to take care of this since wxHTTP reestablishes it automatically.

See also
  wxInputStream

Implements wxProtocol.

## ◆ GetResponse()

int wxHTTP::GetResponse | ( |  | ) | const

Returns the HTTP response code returned by the server.

Please refer to RFC 2616 for the list of responses.

## ◆ HasCookies()

bool wxHTTP::HasCookies | ( |  | ) | const

Returns true if there were cookies.

## ◆ SetHeader()

void wxHTTP::SetHeader | ( | const wxString & | header,
|  | const wxString & | h_data )

It sets data of a field to be sent during the next request to the HTTP server.

The field name is specified by header and the content by h_data. This is a low level function and it assumes that you know what you are doing.

## ◆ SetMethod()

void wxHTTP::SetMethod | ( | const wxString & | method | ) |

Set HTTP method.

Set common or expanded HTTP method.

Overrides GET or POST methods that is used by default.

Parameters

  method | HTTP method name, e.g. "GET".

Since
  3.0

See also
  SetPostBuffer(), SetPostText()

## ◆ SetPostBuffer()

bool wxHTTP::SetPostBuffer | ( | const wxString & | contentType,
|  | const wxMemoryBuffer & | data )

Set the binary data to be posted to the server.

If a non-empty buffer is passed to this method, the next request will be an HTTP `POST` instead of the default HTTP `GET` and the given data will be posted as the body of this request.

For textual data a more convenient SetPostText() can be used instead.

Parameters

  contentType | The value of HTTP "Content-Type" header, e.g. "image/png".
  data | The data to post.

Returns
  true if any data was passed in or false if the buffer was empty.

Since
  2.9.4

## ◆ SetPostText()

bool wxHTTP::SetPostText | ( | const wxString & | contentType,
|  | const wxString & | data,
|  | const wxMBConv & | conv = wxConvUTF8 )

Set the text to be posted to the server.

After a successful call to this method, the request will use HTTP `POST` instead of the default `GET` when it's executed.

Use SetPostBuffer() if you need to post non-textual data.

Parameters

  contentType | The value of HTTP "Content-Type" header, e.g. "text/html; charset=UTF-8".
  data | The data to post.
  conv | The conversion to use to convert data contents to a byte stream. Its value should be consistent with the charset parameter specified in contentType.

Returns
  true if string was non-empty and was successfully converted using the given conv or false otherwise (in this case this request won't be `POST'ed` correctly).

Since
  2.9.4