StanfordCPPLib
Public Member Functions | List of all members
iurlstream Class Reference

An iurlstream is an input stream for reading data from URLs. More...

#include "urlstream.h"

Inheritance diagram for iurlstream:

Public Member Functions

 iurlstream()
 Initializes a new iurlstream that is not attached to any source. More...
 
 iurlstream(string url)
 Initializes a new iurlstream that is attached to the given source URL. More...
 
void close()
 Closes the stream. More...
 
int getErrorCode() const
 Returns the most recent error code received, if any. More...
 
string getErrorMessage() const
 Returns a message about the most recent error, if any. More...
 
string getHeader(string name) const
 Returns the value of the given HTTP header for this URL request. More...
 
int getHttpStatusCode() const
 Returns the most recent HTTP status code, which may be a successful code (e.g. More...
 
string getUrl() const
 Returns the URL sent to the stream's constructor or to the last call to open(...). More...
 
string getUserAgent() const
 Returns the value of the HTTP "User-Agent" header for this URL request, or an empty string if the user agent has not been set. More...
 
void open(string url="")
 Opens the given URL for reading. More...
 
void setHeader(string name, string value)
 
void setUserAgent(string userAgent)
 

Detailed Description

An iurlstream is an input stream for reading data from URLs.

It is implemented as a thin wrapper around a standard C++ stringstream. The data from the given URL is downloaded into a memory buffer, from which you can read it as you would any other input stream.

Constructor & Destructor Documentation

◆ iurlstream() [1/2]

Initializes a new iurlstream that is not attached to any source.

Use this constructor if you want to set various properties of the URL connection (such as user agent or other headers) before downloading.

◆ iurlstream() [2/2]

iurlstream ( string   url)

Initializes a new iurlstream that is attached to the given source URL.

The data from that URL is downloaded immediately.

Member Function Documentation

◆ close()

void close ( )

Closes the stream.

This function actually does nothing and is left in only for legacy compatibility purposes. You do not need to call it.

◆ getErrorCode()

int getErrorCode ( ) const

Returns the most recent error code received, if any.

Returns 0 if no error codes have been issued.

◆ getErrorMessage()

string getErrorMessage ( ) const

Returns a message about the most recent error, if any.

Returns "" if no errors have occurred.

◆ getHeader()

string getHeader ( string   name) const

Returns the value of the given HTTP header for this URL request.

If the given header is not defined, returns an empty string.

◆ getHttpStatusCode()

int getHttpStatusCode ( ) const

Returns the most recent HTTP status code, which may be a successful code (e.g.

200) or an error (e.g 404). If there is no HTTP status code to return, returns 0.

◆ getUrl()

string getUrl ( ) const

Returns the URL sent to the stream's constructor or to the last call to open(...).

◆ getUserAgent()

string getUserAgent ( ) const

Returns the value of the HTTP "User-Agent" header for this URL request, or an empty string if the user agent has not been set.

◆ open()

void open ( string   url = "")

Opens the given URL for reading.

If no URL is passed, uses the URL passed to the constructor.

◆ setHeader()

void setHeader ( string   name,
string   value 
)

◆ setUserAgent()

void setUserAgent ( string   userAgent)