1.項目中要使用c++++來訪問Web服務器,從網上找了個C++的封裝類,其中調用了MFC,在VC2005上用能用,但是移植到VC2003就出問題了,干脆修改成了純C++的,不敢獨享,share之。
2.以下是調用方法:
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; //初始化類
11 string sHeaderSend; //定義http頭
12 string sHeaderReceive; //返回頭
13 string sMessage=""; //返回頁面內容
14 bool IsPost=false; //是否Post提交
15
16 int i =myRequest.SendRequest(IsPost, "http://neeao.com", sHeaderSend,
17 sHeaderReceive, sMessage);
18 if (i)
19 {
20 cout<<"Http頭:"<
21 cout<< sHeaderSend <
22 cout<<"響應頭"<
23 cout<< sHeaderReceive <
24 cout<<"網頁內容"<
25 cout<< sMessage <
26 }else
27 {
28 cout<<"網絡不可到達"<
29 }
30 system("pause");
31 return 0;
32 }
33
直接上代碼了,
Request.h
1 //********
2 //純C++的socket訪問Http封裝類,Neeao修改
3 //neeao.com
4 //2009-08-25
5 //********
6
7 #if !defined(AFX_REQUEST_H__9F2C9BB6_CBA7_40AF_80A4_09A1CE1CE220__INCLUDED_)
8 #define AFX_REQUEST_H__9F2C9BB6_CBA7_40AF_80A4_09A1CE1CE220__INCLUDED_
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 a
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_H__9F2C9BB6_CBA7_40AF_80A4_09A1CE1CE220__INCLUDED_)
72
Request.cpp
1 //********
2 //純C++的Socket訪問Http封裝類,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 as
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