|
OpenTop 1.3 | |||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | Cross-Platform C++ | ||||
SUMMARY: CONSTRUCTOR | METHOD | DETAIL: CONSTRUCTOR | METHOD |
#include "ot/net/URLConnection.h"
When a URL is created, the protocol (or scheme) is used to request the URLStreamHandlerFactory for an instance of a URLStreamHandler that knows how to (a) parse URLs of for that protocol and (b) create instances of URLConnection that know how to connect to a resource using the protocol.
Many programs do not need to use a URLConnection directly, they can simply create a URL and ask it for an InputStream using getInputStream(). However, the URLConnection class gives the application greater control over how to connect to the resource and even allows the program to write to some types of resource via an OutputStream.
Programs that do need to deal with a URLConnection directly generally follow the following basic sequence:-
#include "ot/io/Console.h" #include "ot/net/URL.h" #include "ot/net/URLConnection.h" using namespace ot; using namespace net; using namespace io; int main(int argc, char* argv[]) { // create a SystemMonitor to ensure clean library termination SystemMonitor monitor; if(argc != 2) { Console::cout() << OT_T("Enter a single URL") << endl; exit(1); } // convert the multi-byte input into a OpenTop (Unicode) string String urlSpec = StringUtils::FromNativeMBCS(argv[1]); try { URL url(urlSpec); RefPtr<URLConnection> rpUrlConn = url.openConnection(); rpUrlConn->connect(); for(int i=0; i<rpUrlConn->getHeaderFieldCount(); i++) { const String& hdr = rpUrlConn->getHeaderField(i); if(rpUrlConn->getHeaderFieldKey(i).empty()) Console::cout() << OT_T("null"); else Console::cout() << rpUrlConn->getHeaderFieldKey(i); Console::cout() << OT_T(": ") << hdr << endl; } } catch(Exception& e) { Console::err() << e.toString() << endl; } return 0; }
Constructor/Destructor Summary | |
URLConnection(const URL& url) Constructs a URLConnection for the specified URL. |
Method Summary | |
virtual void |
connect()=0 |
virtual String |
getContentEncoding() Returns the value of the "content-encoding" header field. |
virtual long |
getContentLength() Returns the length of the content for this URLConnection. |
virtual String |
getContentType() Returns the value of the "content-type" header field. |
virtual DateTime |
getDate() Returns the value of the "date" header field parsed into a DateTime object. |
static bool |
GetDefaultUseCaches() Returns the default value for the UseCaches property that will be used for new instances of URLConnection. |
bool |
getDoInput() const Returns a boolean value indicating whether this URLConnection may be used for input operations. |
bool |
getDoOutput() const Returns a boolean value indicating whether this URLConnection may be used for output operations. |
virtual String |
getHeaderField(const String& name)=0 Returns the value of a header field where the key is equal to name. |
virtual String |
getHeaderField(size_t index)=0 Returns the value of a header field by index. |
virtual size_t |
getHeaderFieldCount()=0 Returns the number of header fields. |
virtual DateTime |
getHeaderFieldDate(const String& name) Parse the specified header field as a Date and return as a DateTime. |
virtual String |
getHeaderFieldKey(size_t index)=0 Returns the value of a header field key by index. |
virtual long |
getHeaderFieldLong(const String& name, long defaultValue) Parse the specified header field as a long integer. |
virtual RefPtr< InputStream > |
getInputStream()=0 Returns an InputStream which reads bytes from the connection. |
virtual DateTime |
getLastModified() Returns the value of the "last-modified" header field parsed into a DateTime object. |
virtual RefPtr< OutputStream > |
getOutputStream()=0 Returns an OutputStream which can be used to write bytes to the connection. |
virtual String |
getRequestProperty(const String& name) const Returns the value of the specified MIME header. |
virtual const URL& |
getURL() const Returns the URL for this URLConnection. |
bool |
getUseCaches() const Returns the value of the UseCaches property for this URLConnection. |
bool |
isConnected() const Returns the value of the connected property. |
protected void |
resetHeaderFields() |
protected void |
setConnected(bool bConnected) Sets the connected property to bConnected. |
static void |
SetDefaultUseCaches(bool bUseCaches) Static method to set the default value for the UseCaches property for new instances of URLConnection. |
void |
setDoInput(bool bEnable) Enables this URLConnection for input processing. |
void |
setDoOutput(bool bEnable) Enables this URLConnection for output processing. |
virtual void |
setRequestProperty(const String& name, const String& value) Sets a request MIME header value. |
protected void |
setURL(const URL& url) Sets the URL for this URLConnection. |
void |
setUseCaches(bool bUseCaches) Sets the value of the UseCaches property for this URLConnection instance. |
Methods inherited from class ot::ManagedObject |
addRef, getRefCount, onFinalRelease, operator=, release |
Constructor/Destructor Detail |
URLConnection(const URL& url)
Method Detail |
virtual void connect()=0
virtual String getContentEncoding()
virtual long getContentLength()
When using HTTP 1.1, the content length is not always available. In this case the resource will be transmitted using the "Chunked" encoding method.
virtual String getContentType()
virtual DateTime getDate()
static bool GetDefaultUseCaches()
bool getDoInput() const
The default value is true, unless this URLConnection has been explicitly enabled for output operations, in which case the default is false.
bool getDoOutput() const
The default value is false..
virtual String getHeaderField(const String& name)=0
name
- virtual String getHeaderField(size_t index)=0
index
- virtual size_t getHeaderFieldCount()=0
virtual DateTime getHeaderFieldDate(const String& name)
virtual String getHeaderFieldKey(size_t index)=0
index
- virtual long getHeaderFieldLong(const String& name, long defaultValue)
name
- defaultValue
- virtual RefPtr< InputStream > getInputStream()=0
IOException
- virtual DateTime getLastModified()
virtual RefPtr< OutputStream > getOutputStream()=0
IOException
- IllegalStateException
- ProtocolException
- virtual String getRequestProperty(const String& name) const
name
- virtual const URL& getURL() const
bool getUseCaches() const
bool isConnected() const
protected void resetHeaderFields()
protected void setConnected(bool bConnected)
static void SetDefaultUseCaches(bool bUseCaches)
void setDoInput(bool bEnable)
IllegalStateException
- void setDoOutput(bool bEnable)
IllegalStateException
- virtual void setRequestProperty(const String& name, const String& value)
name
- value
- ProtocolException
- protected void setURL(const URL& url)
void setUseCaches(bool bUseCaches)
|
OpenTop 1.3 | |||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | Cross-Platform C++ | ||||
SUMMARY: CONSTRUCTOR | METHOD | DETAIL: CONSTRUCTOR | METHOD |