Introduction to Http encapsulation code for Socket access in pure C ++

Source: Internet
Author: User

1. in the project, c ++ is used to access the Web server, and a C ++ encapsulation class is found from the Internet, in which MFC is called and can be used on VC2005, however, a problem occurs when it is transplanted to VC2003. It is simply changed to a pure C ++, and it is not exclusive and share.
2. The following is the call method:


1 # include "stdafx. h"
2 # include
3 # include
4 # include "httpequest. h"
5
6 using namespace std;
7
8 int _ tmain (int argc, _ TCHAR * argv [])
9 {
10 Request myRequest; // initialization class
11 string sHeaderSend; // defines the http Header
12 string sHeaderReceive; // return Header
13 string sMessage = ""; // return the page content
14 bool IsPost = false; // whether Post is submitted
15
16 int I = myRequest. SendRequest (IsPost, "http://neeao.com", sHeaderSend,
17 sHeaderReceive, sMessage );
18 if (I)
19 {
20 cout <"Http header:" <
21 cout <sHeaderSend <
22 cout <"Response Header" <
23 cout <sHeaderReceive <
24 cout <"webpage content" <
25 cout <sMessage <
26} else
27 {
28 cout <"the network is not reachable" <
29}
30 system ("pause ");
31 return 0;
32}
33


Code directly,

Request. h


1 //********
2 // The socket access Http encapsulation class of pure C ++, which is modified by Neeao
3 // neeao.com
4 // 2009-08-25
5 //********
6
7 # if! Defined (afx_request_hda-9f2c9bb6_cba7_40af_80a4_09a1ce1ce2201_encoded _)
8 # define afx_request_h00009f2c9bb6_cba7_40af_80a4_09a1ce1ce2201_encoded _
9
10 # if _ MSC_VER> 1000
11 # pragma once
12 # endif // _ MSC_VER> 1000
13
14
15 # include
16 # include
17 # include
18 # include
19 # pragma comment (lib, "WS2_32 ")
20
21 using namespace std;
22 # define MEM_BUFFER_SIZE 10
23
24 /*
25 HTTPRequest: Structure that returns the HTTP headers and message
26 from the request
27 */
28 typedef struct
29 {
30 LPSTR headerSend; // Pointer to HTTP header Send
31 LPSTR headerReceive; // Pointer to HTTP headers Receive
32 LPSTR message; // Pointer to the HTTP message
33 long messageLength; // Length of the message
34} HTTPRequest;
35
36 /*
37 MemBuffer: Structure used to implement a memory buffer, which is
38 buffer of memory that will grow to hold variable sized
39 parts of the HTTP message.
40 */
41 typedef struct
42 {
43 unsigned char * buffer;
44 unsigned char * position;
45 size_t size;
46} MemBuffer;
47
48
49 class Request
50 {
51 public:
52 Request ();
53 virtual ~ Request ();
54
55 private:
56 void MemBufferCreate (MemBuffer * B );
57 void MemBufferGrow (MemBuffer * B );
58 void MemBufferAddByte (MemBuffer * B, unsigned char byt );
59 void MemBufferAddBuffer (MemBuffer * B, unsigned char * buffer, size_t size );
60 DWORD GetHostAddress (LPCSTR host );
61 void SendString (SOCKET sock, LPCSTR str );
62 BOOL ValidHostChar (char ch );
63 void ParseURL (string url, LPSTR protocol, int lprotocol, LPSTR host, int lhost, LPSTR request, int lrequest, int * port );
64
65 int SendHTTP (string url, LPCSTR headerReceive, BYTE * post, DWORD postLength, HTTPRequest * req );
66
67 public:
68 int SendRequest (bool IsPost, string url, string & psHeaderSend, string & pszHeaderReceive, string & pszMessage );
69 };
70
71 # endif //! Defined (afx_request_hda-9f2c9bb6_cba7_40af_80a4_09a1ce1ce2201_encoded _)
72


Request. cpp

1 //********
2 // The Socket access Http encapsulation class of pure C ++, which is modified by Neeao
3 // http://neeao.com
4 // 2009-08-25
5 //********
6
7
8 # include "stdafx. h"
9 # include "Request. h"
10 # include
11 # ifdef _ DEBUG
12 # undef THIS_FILE
13 static char THIS_FILE [] =__ FILE __;
14 # define new DEBUG_NEW
15 # endif
16
17
18 /////////////////////////////////////// ///////////////////////////////
19 // Construction/Destruction
20 /////////////////////////////////////// ///////////////////////////////
21
22 Request: Request ()
23 {
24
25}
26
27 Request ::~ Request ()
28 {
29
30}
31
32
33 //*
34 // MemBufferCreate:
35 // Passed a MemBuffer structure, will allocate a memory buffer
36 // of MEM_BUFFER_SIZE. This buffer can then grow as needed.
37 //*
38 void Request: MemBufferCreate (MemBuffer * B)
39 {
40 B-> size = MEM_BUFFER_SIZE;
41 B-> buffer = (unsigned char *) malloc (B-> size );
42 B-> position = B-> buffer;
43}
44
45 //*
46 // MemBufferGrow:
47 // Double the size of the buffer that was passed to this function.
48 //*
49 void Request: MemBufferGrow (MemBuffer * B)
50 {
51 size_t sz;
52 sz = B-> position-B-> buffer;
53 B-> size = B-> size * 2;
54 B-> buffer = (unsigned char *) realloc (B-> buffer, B-> size );
55 B-> position = B-> buffer + sz; // readjust current position
56}
57
58 //*
59 // MemBufferAddByte:
60 // Add a single byte to the memory buffer, grow if needed.
61 //*
62 void Request: MemBufferAddByte (MemBuffer * B, unsigned char byt)
63 {
64 if (size_t) (B-> position-B-> buffer)> = B-> size)
65 MemBufferGrow (B );
66
67 * (B-> position ++) = byt;
68}
69
70 //*
71 // MemBufferAddBuffer:
72 // Add a range of bytes to the memory buffer, grow if needed.
73 //*
74 void Request: MemBufferAddBuffer (MemBuffer * B,
75 unsigned char * buffer, size_t size)
76 {
77 while (size_t) (B-> position-B-> buffer) + size)> = B-> size)
78 MemBufferGrow (B );
79
80 memcpy (B-> position, buffer, size );
81 B-> position + = size;
82}
83
84 //*
85 // GetHostAddress:
86 // Resolve using DNS or similar (WINS, etc) the IP
87 // address for a domain name such as www.wdj.com.
88 //*
89 DWORD Request: GetHostAddress (LPCSTR host)
90 {
91 struct hostent * phe;
92 char * p;
93
94 phe = gethostbyname (host );
95
96 if (phe = NULL)
97 return 0;
98
99 p = * phe-> h_addr_list;
100 return * (DWORD *) p );
101}
102


103 //*
104 // SendString:
105 // Send a string (null terminated) over the specified socket.
106 //*
107 void Request: SendString (SOCKET sock, LPCSTR str)
108 {
109 send (sock, str, strlen (str), 0 );
110}
111
112 //*
113 // ValidHostChar:
114 // Return TRUE if the specified character is valid
115 // for a host name, I. e. A-Z or 0-9 or -.:
116 //*
117 BOOL Request: ValidHostChar (char ch)
118 {
119 return (isalpha (ch) | isdigit (ch)
120 | ch =-| ch =. | ch = :);
121}
122
123
124 //*
125 // ParseURL:
126 // Used to break apart a URL such
127 // http://www.localhost.com: 80/TestPost.htm into protocol, port, host and request.
128 //*
129 void Request: ParseURL (string url, LPSTR protocol, int lpr

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.