http.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /////////////////////////////////////////////////////////////////////////////
  2. // Name: protocol/http.h
  3. // Purpose: interface of wxHTTP
  4. // Author: wxWidgets team
  5. // Licence: wxWindows licence
  6. /////////////////////////////////////////////////////////////////////////////
  7. /**
  8. @class wxHTTP
  9. wxHTTP can be used to establish a connection to an HTTP server.
  10. wxHTTP can thus be used to create a (basic) HTTP @b client.
  11. @library{wxnet}
  12. @category{net}
  13. @see wxSocketBase, wxURL
  14. */
  15. class wxHTTP : public wxProtocol
  16. {
  17. public:
  18. /**
  19. Default constructor.
  20. */
  21. wxHTTP();
  22. /**
  23. Destructor will close the connection if connected.
  24. */
  25. virtual ~wxHTTP();
  26. //@{
  27. /**
  28. Connect to the HTTP server.
  29. By default, connection is made to the port 80 of the specified @a host.
  30. You may connect to a non-default port by specifying it explicitly using
  31. the second overload.
  32. Currently wxHTTP only supports IPv4.
  33. For the overload taking wxSockAddress, the @a wait argument is ignored.
  34. */
  35. virtual bool Connect(const wxString& host);
  36. virtual bool Connect(const wxString& host, unsigned short port);
  37. virtual bool Connect(const wxSockAddress& addr, bool wait);
  38. //@}
  39. /**
  40. Returns the data attached with a field whose name is specified by @a header.
  41. If the field doesn't exist, it will return an empty string and not a @NULL string.
  42. @note
  43. The header is not case-sensitive, i.e. "CONTENT-TYPE" and "content-type"
  44. represent the same header.
  45. */
  46. wxString GetHeader(const wxString& header) const;
  47. /**
  48. Creates a new input stream on the specified path.
  49. Notice that this stream is unseekable, i.e. SeekI() and TellI() methods
  50. shouldn't be used.
  51. Note that you can still know the size of the file you are getting using
  52. wxStreamBase::GetSize(). However there is a limitation: in HTTP protocol,
  53. the size is not always specified so sometimes @c (size_t)-1 can returned to
  54. indicate that the size is unknown.
  55. In such case, you may want to use wxInputStream::LastRead() method in a loop
  56. to get the total size.
  57. @return Returns the initialized stream. You must delete it yourself
  58. once you don't use it anymore and this must be done before
  59. the wxHTTP object itself is destroyed. The destructor
  60. closes the network connection. The next time you will
  61. try to get a file the network connection will have to
  62. be reestablished, but you don't have to take care of
  63. this since wxHTTP reestablishes it automatically.
  64. @see wxInputStream
  65. */
  66. virtual wxInputStream* GetInputStream(const wxString& path);
  67. /**
  68. Returns the HTTP response code returned by the server.
  69. Please refer to RFC 2616 for the list of responses.
  70. */
  71. int GetResponse() const;
  72. /**
  73. Set HTTP method.
  74. Set <a href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html">common</a>
  75. or expanded HTTP method.
  76. Overrides GET or POST methods that is used by default.
  77. @param method
  78. HTTP method name, e.g. "GET".
  79. @since 3.0
  80. @see SetPostBuffer(), SetPostText()
  81. */
  82. void SetMethod(const wxString& method);
  83. /**
  84. It sets data of a field to be sent during the next request to the HTTP server.
  85. The field name is specified by @a header and the content by @a h_data.
  86. This is a low level function and it assumes that you know what you are doing.
  87. */
  88. void SetHeader(const wxString& header, const wxString& h_data);
  89. /**
  90. Returns the value of a cookie.
  91. */
  92. wxString GetCookie(const wxString& cookie) const;
  93. /**
  94. Returns @true if there were cookies.
  95. */
  96. bool HasCookies() const;
  97. /**
  98. Set the binary data to be posted to the server.
  99. If a non-empty buffer is passed to this method, the next request will
  100. be an HTTP @c POST instead of the default HTTP @c GET and the given @a
  101. data will be posted as the body of this request.
  102. For textual data a more convenient SetPostText() can be used instead.
  103. @param contentType
  104. The value of HTTP "Content-Type" header, e.g. "image/png".
  105. @param data
  106. The data to post.
  107. @return
  108. @true if any data was passed in or @false if the buffer was empty.
  109. @since 2.9.4
  110. */
  111. bool SetPostBuffer(const wxString& contentType, const wxMemoryBuffer& data);
  112. /**
  113. Set the text to be posted to the server.
  114. After a successful call to this method, the request will use HTTP @c
  115. POST instead of the default @c GET when it's executed.
  116. Use SetPostBuffer() if you need to post non-textual data.
  117. @param contentType
  118. The value of HTTP "Content-Type" header, e.g. "text/html;
  119. charset=UTF-8".
  120. @param data
  121. The data to post.
  122. @param conv
  123. The conversion to use to convert @a data contents to a byte stream.
  124. Its value should be consistent with the charset parameter specified
  125. in @a contentType.
  126. @return
  127. @true if string was non-empty and was successfully converted using
  128. the given @a conv or @false otherwise (in this case this request
  129. won't be @c POST'ed correctly).
  130. @since 2.9.4
  131. */
  132. bool SetPostText(const wxString& contentType,
  133. const wxString& data,
  134. const wxMBConv& conv = wxConvUTF8);
  135. };