纯C++的Socket访问Http封装类 .

 
纯C++的Socket访问Http封装类

1.项目中要使用c++++来访问Web服务器,从网上找了个C++的封装类,其中调用了MFC,在VC2005上用能用,但是移植到VC2003就出问题了,干脆修改成了纯C++的,不敢独享,share之。

2.以下是调用方法:

01.#include "stdafx.h"    
02.#include <iostream>    
03.#include <string>    
04.#include "http/request.h"    
05.   
06.using namespace std;   
07.   
08.int _tmain(int argc, _TCHAR* argv[])   
09.{   
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://sungard.com", sHeaderSend,   
17.                                 sHeaderReceive, sMessage);   
18.    if (i)   
19.    {      
20.        cout<<"Http头:"<<endl;   
21.        cout<< sHeaderSend <<endl;   
22.        cout<<"响应头"<<endl;   
23.        cout<< sHeaderReceive <<endl;   
24.        cout<<"网页内容"<<endl;   
25.        cout<< sMessage <<endl;   
26.    }else   
27.    {   
28.        cout<<"网络不可到达"<<endl;   
29.    }   
30.    system("pause");   
31.    return 0;   
32.}   

类代码如下

01.//******************************************     
02.//纯C++的socket访问Http封装类     
03.//http://sungard.com     
04.//2011-08-22     
05.//******************************************    
06.  
07.#if !defined(AFX_REQUEST_H__9F2C9BB6_CBA7_40AF_80A4_09A1CE1CE220__INCLUDED_)    
08.#define AFX_REQUEST_H__9F2C9BB6_CBA7_40AF_80A4_09A1CE1CE220__INCLUDED_    
09.  
10.#if _MSC_VER > 1000    
11.#pragma once    
12.#endif // _MSC_VER > 1000    
13.  
14.  
15.#include <stdio.h>    
16.#include <stdlib.h>    
17.#include <string.h>    
18.#include <winsock2.h>    
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  
73.   
74.//******************************************     
75.//纯C++的Socket访问Http封装类     
76.//  
77.//     
78.//******************************************    
79.  
80.  
81.#include "stdafx.h"    
82.#include "Request.h"    
83.#include <string>    
84.#ifdef _DEBUG    
85.#undef THIS_FILE     
86.static char THIS_FILE[]=__FILE__;    
87.#define new DEBUG_NEW    
88.#endif     
89.   
90.   
91.//////////////////////////////////////////////////////////////////////     
92.// Construction/Destruction     
93.//////////////////////////////////////////////////////////////////////     
94.   
95.Request::Request()    
96.{    
97.   
98.}    
99.   
100.Request::~Request()    
101.{    
102.   
103.}    
104.   
105.   
106.//*******************************************************************************************************     
107.//MemBufferCreate:      
108.//                  Passed a MemBuffer structure, will allocate a memory buffer      
109.//                   of MEM_BUFFER_SIZE.  This buffer can then grow as needed.     
110.//*******************************************************************************************************     
111.void Request::MemBufferCreate(MemBuffer *b)    
112.{    
113.    b->size = MEM_BUFFER_SIZE;    
114.    b->buffer =(unsigned    char *) malloc( b->size );    
115.    b->position = b->buffer;    
116.}    
117.   
118.//*******************************************************************************************************     
119.// MemBufferGrow:       
120.//                  Double the size of the buffer that was passed to this function.      
121.//*******************************************************************************************************     
122.void Request::MemBufferGrow(MemBuffer *b)    
123.{    
124.    size_t sz;    
125.    sz = b->position - b->buffer;    
126.    b->size = b->size *2;    
127.    b->buffer =(unsigned    char *) realloc(b->buffer,b->size);    
128.    b->position = b->buffer + sz;   // readjust current position     
129.}    
130.   
131.//*******************************************************************************************************     
132.// MemBufferAddByte:      
133.//                  Add a single byte to the memory buffer, grow if needed.     
134.//*******************************************************************************************************     
135.void Request::MemBufferAddByte(MemBuffer *b,unsigned char byt)    
136.{    
137.    if( (size_t)(b->position-b->buffer) >= b->size )    
138.        MemBufferGrow(b);    
139.   
140.    *(b->position++) = byt;    
141.}    
142.   
143.//*******************************************************************************************************     
144.// MemBufferAddBuffer:     
145.//                  Add a range of bytes to the memory buffer, grow if needed.     
146.//*******************************************************************************************************     
147.void Request::MemBufferAddBuffer(MemBuffer *b,    
148.                                 unsigned char *buffer, size_t size)    
149.{    
150.    while( ((size_t)(b->position-b->buffer)+size) >= b->size )    
151.        MemBufferGrow(b);    
152.   
153.    memcpy(b->position,buffer,size);    
154.    b->position+=size;    
155.}    
156.   
157.//*******************************************************************************************************     
158.// GetHostAddress:      
159.//                  Resolve using DNS or similar(WINS,etc) the IP      
160.//                   address for a domain name such as www.wdj.com.      
161.//*******************************************************************************************************     
162.DWORD Request::GetHostAddress(LPCSTR host)    
163.{    
164.    struct hostent *phe;    
165.    char *p;    
166.   
167.    phe = gethostbyname( host );    
168.   
169.    if(phe==NULL)    
170.        return 0;    
171.   
172.    p = *phe->h_addr_list;    
173.    return *((DWORD*)p);    
174.}    
175.   
176.//*******************************************************************************************************     
177.// SendString:      
178.//                  Send a string(null terminated) over the specified socket.     
179.//*******************************************************************************************************     
180.void Request::SendString(SOCKET sock,LPCSTR str)    
181.{    
182.    send(sock,str,strlen(str),0);    
183.}    
184.   
185.//*******************************************************************************************************     
186.// ValidHostChar:      
187.//                  Return TRUE if the specified character is valid     
188.//                      for a host name, i.e. A-Z or 0-9 or -.:      
189.//*******************************************************************************************************     
190.BOOL Request::ValidHostChar(char ch)    
191.{    
192.    return( isalpha(ch) || isdigit(ch)    
193.        || ch=='-' || ch=='.' || ch==':' );    
194.}    
195.   
196.   
197.//*******************************************************************************************************     
198.// ParseURL:      
199.//                  Used to break apart a URL such as      
200.//                      http://www.localhost.com:80/TestPost.htm into protocol, port, host and request.     
201.//*******************************************************************************************************     
202.void Request::ParseURL(string url,LPSTR protocol,int lprotocol,LPSTR host,int lhost,LPSTR request,int lrequest,int *port)    
203.{    
204.    char *work,*ptr,*ptr2;    
205.   
206.    *protocol = *host = *request = 0;    
207.    *port=80;    
208.   
209.    work = strdup(url.c_str());    
210.    strupr(work);    
211.   
212.    ptr = strchr(work,':');                         // find protocol if any     
213.    if(ptr!=NULL)    
214.    {    
215.        *(ptr++) = 0;    
216.        lstrcpyn(protocol,work,lprotocol);    
217.    }    
218.    else   
219.    {    
220.        lstrcpyn(protocol,"HTTP",lprotocol);    
221.        ptr = work;    
222.    }    
223.   
224.    if( (*ptr=='/') && (*(ptr+1)=='/') )            // skip past opening /'s      
225.        ptr+=2;    
226.   
227.    ptr2 = ptr;                                     // find host     
228.    while( ValidHostChar(*ptr2) && *ptr2 )    
229.        ptr2++;    
230.   
231.    *ptr2=0;    
232.    lstrcpyn(host,ptr,lhost);    
233.   
234.    lstrcpyn(request,url.c_str() + (ptr2-work),lrequest);   // find the request     
235.   
236.    ptr = strchr(host,':');                         // find the port number, if any     
237.    if(ptr!=NULL)    
238.    {    
239.        *ptr=0;    
240.        *port = atoi(ptr+1);    
241.    }    
242.   
243.    free(work);    
244.}    
245.   
246.//*******************************************************************************************************     
247.// SendHTTP:      
248.//                  Main entry point for this code.       
249.//                    url           - The URL to GET/POST to/from.     
250.//                    headerSend        - Headers to be sent to the server.     
251.//                    post          - Data to be posted to the server, NULL if GET.     
252.//                    postLength    - Length of data to post.     
253.//                    req           - Contains the message and headerSend sent by the server.     
254.//     
255.//                    returns 1 on failure, 0 on success.     
256.//*******************************************************************************************************     
257.int Request::SendHTTP(string url,LPCSTR headerReceive,BYTE *post,    
258.                      DWORD postLength,HTTPRequest *req)    
259.{    
260.    WSADATA         WsaData;    
261.    SOCKADDR_IN     sin;    
262.    SOCKET          sock;    
263.    char            buffer[512];    
264.    char            protocol[20],host[256],request[1024];    
265.    int             l,port,chars,err;    
266.    MemBuffer       headersBuffer,messageBuffer;    
267.    char            headerSend[1024];    
268.    BOOL            done;    
269.   
270.   
271.   
272.   
273.    ParseURL(url,protocol,sizeof(protocol),host,sizeof(host),       // Parse the URL     
274.        request,sizeof(request),&port);    
275.    if(strcmp(protocol,"HTTP"))    
276.        return 1;    
277.   
278.    err = WSAStartup (0x0101, &WsaData);                            // Init Winsock     
279.    if(err!=0)    
280.        return 1;    
281.   
282.    sock = socket (AF_INET, SOCK_STREAM, 0);    
283.    //if (socket == INVALID_SOCKET)     
284.    if (sock == INVALID_SOCKET)    
285.    {    
286.        return 1;    
287.    }    
288.   
289.    sin.sin_family = AF_INET;                                       //Connect to web sever     
290.    sin.sin_port = htons( (unsigned short)port );    
291.    sin.sin_addr.s_addr = GetHostAddress(host);    
292.   
293.    if( connect (sock,(LPSOCKADDR)&sin, sizeof(SOCKADDR_IN) ) )    
294.    {    
295.   
296.        return 1;    
297.    }    
298.   
299.   
300.    if( !*request )    
301.        lstrcpyn(request,"/",sizeof(request));    
302.   
303.    if( post == NULL )    
304.    {    
305.        SendString(sock,"GET ");    
306.        strcpy(headerSend, "GET ");    
307.    }    
308.    else     
309.    {    
310.        SendString(sock,"POST ");    
311.        strcpy(headerSend, "POST ");    
312.    }    
313.    SendString(sock,request);    
314.    strcat(headerSend, request);    
315.   
316.    SendString(sock," HTTP/1.0/r/n");    
317.    strcat(headerSend, " HTTP/1.0/r/n");    
318.   
319.    SendString(sock,"Accept: image/gif, image/x-xbitmap,"   
320.        " image/jpeg, image/pjpeg, application/vnd.ms-excel,"   
321.        " application/msword, application/vnd.ms-powerpoint,"   
322.        " */*/r/n");    
323.    strcat(headerSend, "Accept: image/gif, image/x-xbitmap,"   
324.        " image/jpeg, image/pjpeg, application/vnd.ms-excel,"   
325.        " application/msword, application/vnd.ms-powerpoint,"   
326.        " */*/r/n");    
327.   
328.    SendString(sock,"Accept-Language: en-us/r/n");    
329.    strcat(headerSend, "Accept-Language: en-us/r/n");    
330.   
331.    SendString(sock,"Accept-Encoding: gzip, default/r/n");    
332.    strcat(headerSend, "Accept-Encoding: gzip, default/r/n");    
333.   
334.    SendString(sock,"User-Agent: sungard/4.0/r/n");    
335.    strcat(headerSend, "User-Agent: sungard/4.0/r/n");    
336.   
337.    if(postLength)    
338.    {    
339.        sprintf(buffer,"Content-Length: %ld/r/n",postLength);    
340.        SendString(sock,buffer);    
341.        strcat(headerSend, buffer);    
342.    }    
343.    //SendString(sock,"Cookie: mycookie=blablabla/r/n");     
344.    //  printf("Cookie: mycookie=blablabla/r/n");     
345.    SendString(sock,"Host: ");    
346.    strcat(headerSend, "Host: ");    
347.   
348.    SendString(sock,host);    
349.    strcat(headerSend, host);    
350.   
351.    SendString(sock,"/r/n");    
352.    strcat(headerSend, "/r/n");    
353.   
354.    if( (headerReceive!=NULL) && *headerReceive )    
355.    {    
356.        SendString(sock,headerReceive);    
357.        strcat(headerSend, headerReceive);    
358.    }    
359.   
360.    SendString(sock,"/r/n");                                // Send a blank line to signal end of HTTP headerReceive     
361.    strcat(headerSend, "/r/n");    
362.   
363.    if( (post!=NULL) && postLength )    
364.    {    
365.        send(sock,(const char*)post,postLength,0);    
366.        post[postLength]    = '/0';    
367.   
368.        strcat(headerSend, (const char*)post);    
369.    }    
370.   
371.    //strcpy(req->headerSend, headerSend);     
372.    req->headerSend     = (char*) malloc( sizeof(char*) * strlen(headerSend));    
373.    strcpy(req->headerSend, (char*) headerSend );    
374.   
375.   
376.    MemBufferCreate(&headersBuffer );    
377.    chars = 0;    
378.    done = FALSE;    
379.   
380.    while(!done)    
381.    {    
382.        l = recv(sock,buffer,1,0);    
383.        if(l<0)    
384.            done=TRUE;    
385.   
386.        switch(*buffer)    
387.        {    
388.        case '/r':    
389.            break;    
390.        case '/n':    
391.            if(chars==0)    
392.                done = TRUE;    
393.            chars=0;    
394.            break;    
395.        default:    
396.            chars++;    
397.            break;    
398.        }    
399.   
400.        MemBufferAddByte(&headersBuffer,*buffer);    
401.    }    
402.   
403.    req->headerReceive  = (char*) headersBuffer.buffer;    
404.    *(headersBuffer.position) = 0;    
405.   
406.   
407.   
408.    MemBufferCreate(&messageBuffer);                            // Now read the HTTP body     
409.   
410.    do   
411.    {    
412.        l = recv(sock,buffer,sizeof(buffer)-1,0);    
413.        if(l<0)    
414.            break;    
415.        *(buffer+l)=0;    
416.        MemBufferAddBuffer(&messageBuffer, (unsigned char*)&buffer, l);    
417.    } while(l>0);    
418.    *messageBuffer.position = 0;    
419.    req->message = (char*) messageBuffer.buffer;    
420.    req->messageLength = (messageBuffer.position - messageBuffer.buffer);    
421.   
422.   
423.    closesocket(sock);                                          // Cleanup     
424.   
425.    return 0;    
426.}    
427.   
428.   
429.//*******************************************************************************************************     
430.// SendRequest     
431.//     
432.//*******************************************************************************************************     
433.int Request::SendRequest(bool IsPost, string url, string& psHeaderSend, string& psHeaderReceive, string& psMessage)    
434.{    
435.    HTTPRequest         req;    
436.    int                 i,rtn;    
437.    LPSTR               buffer;    
438.   
439.    req.headerSend                          = NULL;    
440.    req.headerReceive                       = NULL;    
441.    req.message                             = NULL;    
442.   
443.    //Read in arguments     
444.   
445.   
446.    if(IsPost)    
447.    {                                                   /* POST */    
448.        i       = psHeaderSend.length();    
449.        buffer  = (char*) malloc(i+1);    
450.        strcpy(buffer, psHeaderSend.c_str());    
451.   
452.        rtn             = SendHTTP( url,    
453.            "Content-Type: application/x-www-form-urlencoded/r/n",    
454.            (unsigned char*)buffer,    
455.            i,    
456.            &req);    
457.   
458.        free(buffer);    
459.    }    
460.    else/* GET */   
461.    {    
462.        rtn = SendHTTP(url,NULL,NULL,0,&req);    
463.    }    
464.   
465.   
466.   
467.    if(!rtn)                                            //Output message and/or headerSend      
468.    {    
469.        psHeaderSend        = req.headerSend;    
470.        psHeaderReceive     = req.headerReceive;    
471.        psMessage           = req.message;    
472.   
473.   
474.        free(req.headerSend);    
475.        free(req.headerReceive);    
476.        free(req.message);    
477.        return 1;    
478.    }    
479.    else   
480.    {    
481.        return 0;    
482.    }    
483.}   
//******************************************  
//纯C++的socket访问Http封装类, 
////25  
//******************************************  
 
#if !defined(AFX_REQUEST_H__9F2C9BB6_CBA7_40AF_80A4_09A1CE1CE220__INCLUDED_)  
#define AFX_REQUEST_H__9F2C9BB6_CBA7_40AF_80A4_09A1CE1CE220__INCLUDED_  
 
#if _MSC_VER > 1000  
#pragma once  
#endif // _MSC_VER > 1000  
 
 
#include <stdio.h>  
#include <stdlib.h>  
#include <string.h>  
#include <winsock2.h>  
#pragma comment(lib, "WS2_32")   
 
using namespace std;  
#define MEM_BUFFER_SIZE 10  
 
/*   
    HTTPRequest: Structure that returns the HTTP headers and message  
                    from the request  
*/  
typedef struct 
{   
    LPSTR headerSend;                               // Pointer to HTTP header Send   
    LPSTR headerReceive;                            // Pointer to HTTP headers Receive  
    LPSTR message;                                  // Pointer to the HTTP message   
    long messageLength;                             // Length of the message   
} HTTPRequest;  
 
/*   
    MemBuffer:  Structure used to implement a memory buffer, which is a  
                buffer of memory that will grow to hold variable sized  
                parts of the HTTP message.   
*/ 
typedef struct 
{  
    unsigned    char *buffer;  
    unsigned    char *position;  
    size_t      size;  
} MemBuffer;  
 
 
class Request    
{  
public:  
    Request();  
    virtual ~Request();  
 
private:  
    void        MemBufferCreate(MemBuffer *b);  
    void        MemBufferGrow(MemBuffer *b);  
    void        MemBufferAddByte(MemBuffer *b, unsigned char byt);  
    void        MemBufferAddBuffer(MemBuffer *b, unsigned char *buffer, size_t size);  
    DWORD       GetHostAddress(LPCSTR host);  
    void        SendString(SOCKET sock,LPCSTR str);  
    BOOL        ValidHostChar(char ch);  
    void        ParseURL(string url,LPSTR protocol,int lprotocol, LPSTR host,int lhost,LPSTR request,int lrequest,int *port);  
 
    int         SendHTTP(string url,LPCSTR headerReceive,BYTE *post, DWORD postLength,HTTPRequest *req);  
 
public:  
    int     SendRequest(bool IsPost, string url, string& psHeaderSend, string& pszHeaderReceive,string& pszMessage);  
};  
 
#endif // !defined(AFX_REQUEST_H__9F2C9BB6_CBA7_40AF_80A4_09A1CE1CE220__INCLUDED_)  
Request.cpp
 
//******************************************  
//纯C++的Socket访问Http封装类 
////-25  
//******************************************  
 
 
#include "stdafx.h"  
#include "Request.h"  
#include <string>  
#ifdef _DEBUG  
#undef THIS_FILE  
static char THIS_FILE[]=__FILE__;  
#define new DEBUG_NEW  
#endif  
 
 
//////////////////////////////////////////////////////////////////////  
// Construction/Destruction  
//////////////////////////////////////////////////////////////////////  
 
Request::Request()  
{  
 
}  
 
Request::~Request()  
{  
 
}  
 
 
//*******************************************************************************************************  
//MemBufferCreate:   
//                  Passed a MemBuffer structure, will allocate a memory buffer   
//                   of MEM_BUFFER_SIZE.  This buffer can then grow as needed.  
//*******************************************************************************************************  
void Request::MemBufferCreate(MemBuffer *b)  
{  
    b->size = MEM_BUFFER_SIZE;  
    b->buffer =(unsigned    char *) malloc( b->size );  
    b->position = b->buffer;  
}  
 
//*******************************************************************************************************  
// MemBufferGrow:    
//                  Double the size of the buffer that was passed to this function.   
//*******************************************************************************************************  
void Request::MemBufferGrow(MemBuffer *b)  
{  
    size_t sz;  
    sz = b->position - b->buffer;  
    b->size = b->size *2;  
    b->buffer =(unsigned    char *) realloc(b->buffer,b->size);  
    b->position = b->buffer + sz;   // readjust current position  
}  
 
//*******************************************************************************************************  
// MemBufferAddByte:   
//                  Add a single byte to the memory buffer, grow if needed.  
//*******************************************************************************************************  
void Request::MemBufferAddByte(MemBuffer *b,unsigned char byt)  
{  
    if( (size_t)(b->position-b->buffer) >= b->size )  
        MemBufferGrow(b);  
 
    *(b->position++) = byt;  
}  
 
//*******************************************************************************************************  
// MemBufferAddBuffer:  
//                  Add a range of bytes to the memory buffer, grow if needed.  
//*******************************************************************************************************  
void Request::MemBufferAddBuffer(MemBuffer *b,  
                                 unsigned char *buffer, size_t size)  
{  
    while( ((size_t)(b->position-b->buffer)+size) >= b->size )  
        MemBufferGrow(b);  
 
    memcpy(b->position,buffer,size);  
    b->position+=size;  
}  
 
//*******************************************************************************************************  
// GetHostAddress:   
//                  Resolve using DNS or similar(WINS,etc) the IP   
//                   address for a domain name such as www.wdj.com.   
//*******************************************************************************************************  
DWORD Request::GetHostAddress(LPCSTR host)  
{  
    struct hostent *phe;  
    char *p;  
 
    phe = gethostbyname( host );  
 
    if(phe==NULL)  
        return 0;  
 
    p = *phe->h_addr_list;  
    return *((DWORD*)p);  
}  
 
//*******************************************************************************************************  
// SendString:   
//                  Send a string(null terminated) over the specified socket.  
//*******************************************************************************************************  
void Request::SendString(SOCKET sock,LPCSTR str)  
{  
    send(sock,str,strlen(str),0);  
}  
 
//*******************************************************************************************************  
// ValidHostChar:   
//                  Return TRUE if the specified character is valid  
//                      for a host name, i.e. A-Z or 0-9 or -.:   
//*******************************************************************************************************  
BOOL Request::ValidHostChar(char ch)  
{  
    return( isalpha(ch) || isdigit(ch)  
        || ch=='-' || ch=='.' || ch==':' );  
}  
 
 
//*******************************************************************************************************  
// ParseURL:   
//                  Used to break apart a URL such as   
//                      http://www.localhost.com:80/TestPost.htm into protocol, port, host and request.  
//*******************************************************************************************************  
void Request::ParseURL(string url,LPSTR protocol,int lprotocol,LPSTR host,int lhost,LPSTR request,int lrequest,int *port)  
{  
    char *work,*ptr,*ptr2;  
 
    *protocol = *host = *request = 0;  
    *port=80;  
 
    work = strdup(url.c_str());  
    strupr(work);  
 
    ptr = strchr(work,':');                         // find protocol if any  
    if(ptr!=NULL)  
    {  
        *(ptr++) = 0;  
        lstrcpyn(protocol,work,lprotocol);  
    }  
    else 
    {  
        lstrcpyn(protocol,"HTTP",lprotocol);  
        ptr = work;  
    }  
 
    if( (*ptr=='/') && (*(ptr+1)=='/') )            // skip past opening /'s   
        ptr+=2;  
 
    ptr2 = ptr;                                     // find host  
    while( ValidHostChar(*ptr2) && *ptr2 )  
        ptr2++;  
 
    *ptr2=0;  
    lstrcpyn(host,ptr,lhost);  
 
    lstrcpyn(request,url.c_str() + (ptr2-work),lrequest);   // find the request  
 
    ptr = strchr(host,':');                         // find the port number, if any  
    if(ptr!=NULL)  
    {  
        *ptr=0;  
        *port = atoi(ptr+1);  
    }  
 
    free(work);  
}  
 
//*******************************************************************************************************  
// SendHTTP:   
//                  Main entry point for this code.    
//                    url           - The URL to GET/POST to/from.  
//                    headerSend        - Headers to be sent to the server.  
//                    post          - Data to be posted to the server, NULL if GET.  
//                    postLength    - Length of data to post.  
//                    req           - Contains the message and headerSend sent by the server.  
//  
//                    returns 1 on failure, 0 on success.  
//*******************************************************************************************************  
int Request::SendHTTP(string url,LPCSTR headerReceive,BYTE *post,  
                      DWORD postLength,HTTPRequest *req)  
{  
    WSADATA         WsaData;  
    SOCKADDR_IN     sin;  
    SOCKET          sock;  
    char            buffer[512];  
    char            protocol[20],host[256],request[1024];  
    int             l,port,chars,err;  
    MemBuffer       headersBuffer,messageBuffer;  
    char            headerSend[1024];  
    BOOL            done;  
 
 
 
 
    ParseURL(url,protocol,sizeof(protocol),host,sizeof(host),       // Parse the URL  
        request,sizeof(request),&port);  
    if(strcmp(protocol,"HTTP"))  
        return 1;  
 
    err = WSAStartup (0x0101, &WsaData);                            // Init Winsock  
    if(err!=0)  
        return 1;  
 
    sock = socket (AF_INET, SOCK_STREAM, 0);  
    //if (socket == INVALID_SOCKET)  
    if (sock == INVALID_SOCKET)  
    {  
        return 1;  
    }  
 
    sin.sin_family = AF_INET;                                       //Connect to web sever  
    sin.sin_port = htons( (unsigned short)port );  
    sin.sin_addr.s_addr = GetHostAddress(host);  
 
    if( connect (sock,(LPSOCKADDR)&sin, sizeof(SOCKADDR_IN) ) )  
    {  
 
        return 1;  
    }  
 
 
    if( !*request )  
        lstrcpyn(request,"/",sizeof(request));  
 
    if( post == NULL )  
    {  
        SendString(sock,"GET ");  
        strcpy(headerSend, "GET ");  
    }  
    else   
    {  
        SendString(sock,"POST ");  
        strcpy(headerSend, "POST ");  
    }  
    SendString(sock,request);  
    strcat(headerSend, request);  
 
    SendString(sock," HTTP/1.0/r/n");  
    strcat(headerSend, " HTTP/1.0/r/n");  
 
    SendString(sock,"Accept: image/gif, image/x-xbitmap," 
        " image/jpeg, image/pjpeg, application/vnd.ms-excel," 
        " application/msword, application/vnd.ms-powerpoint," 
        " */*/r/n");  
    strcat(headerSend, "Accept: image/gif, image/x-xbitmap," 
        " image/jpeg, image/pjpeg, application/vnd.ms-excel," 
        " application/msword, application/vnd.ms-powerpoint," 
        " */*/r/n");  
 
    SendString(sock,"Accept-Language: en-us/r/n");  
    strcat(headerSend, "Accept-Language: en-us/r/n");  
 
    SendString(sock,"Accept-Encoding: gzip, default/r/n");  
    strcat(headerSend, "Accept-Encoding: gzip, default/r/n");  
 
    SendString(sock,"User-Agent: sungard/4.0/r/n");  
    strcat(headerSend, "User-Agent: sungard/4.0/r/n");  
 
    if(postLength)  
    {  
        sprintf(buffer,"Content-Length: %ld/r/n",postLength);  
        SendString(sock,buffer);  
        strcat(headerSend, buffer);  
    }  
    //SendString(sock,"Cookie: mycookie=blablabla/r/n");  
    //  printf("Cookie: mycookie=blablabla/r/n");  
    SendString(sock,"Host: ");  
    strcat(headerSend, "Host: ");  
 
    SendString(sock,host);  
    strcat(headerSend, host);  
 
    SendString(sock,"/r/n");  
    strcat(headerSend, "/r/n");  
 
    if( (headerReceive!=NULL) && *headerReceive )  
    {  
        SendString(sock,headerReceive);  
        strcat(headerSend, headerReceive);  
    }  
 
    SendString(sock,"/r/n");                                // Send a blank line to signal end of HTTP headerReceive  
    strcat(headerSend, "/r/n");  
 
    if( (post!=NULL) && postLength )  
    {  
        send(sock,(const char*)post,postLength,0);  
        post[postLength]    = '/0';  
 
        strcat(headerSend, (const char*)post);  
    }  
 
    //strcpy(req->headerSend, headerSend);  
    req->headerSend     = (char*) malloc( sizeof(char*) * strlen(headerSend));  
    strcpy(req->headerSend, (char*) headerSend );  
 
 
    MemBufferCreate(&headersBuffer );  
    chars = 0;  
    done = FALSE;  
 
    while(!done)  
    {  
        l = recv(sock,buffer,1,0);  
        if(l<0)  
            done=TRUE;  
 
        switch(*buffer)  
        {  
        case '/r':  
            break;  
        case '/n':  
            if(chars==0)  
                done = TRUE;  
            chars=0;  
            break;  
        default:  
            chars++;  
            break;  
        }  
 
        MemBufferAddByte(&headersBuffer,*buffer);  
    }  
 
    req->headerReceive  = (char*) headersBuffer.buffer;  
    *(headersBuffer.position) = 0;  
 
 
 
    MemBufferCreate(&messageBuffer);                            // Now read the HTTP body  
 
    do 
    {  
        l = recv(sock,buffer,sizeof(buffer)-1,0);  
        if(l<0)  
            break;  
        *(buffer+l)=0;  
        MemBufferAddBuffer(&messageBuffer, (unsigned char*)&buffer, l);  
    } while(l>0);  
    *messageBuffer.position = 0;  
    req->message = (char*) messageBuffer.buffer;  
    req->messageLength = (messageBuffer.position - messageBuffer.buffer);  
 
 
    closesocket(sock);                                          // Cleanup  
 
    return 0;  
}  
 
 
//*******************************************************************************************************  
// SendRequest  
//  
//*******************************************************************************************************  
int Request::SendRequest(bool IsPost, string url, string& psHeaderSend, string& psHeaderReceive, string& psMessage)  
{  
    HTTPRequest         req;  
    int                 i,rtn;  
    LPSTR               buffer;  
 
    req.headerSend                          = NULL;  
    req.headerReceive                       = NULL;  
    req.message                             = NULL;  
 
    //Read in arguments  
 
 
    if(IsPost)  
    {                                                   /* POST */  
        i       = psHeaderSend.length();  
        buffer  = (char*) malloc(i+1);  
        strcpy(buffer, psHeaderSend.c_str());  
 
        rtn             = SendHTTP( url,  
            "Content-Type: application/x-www-form-urlencoded/r/n",  
            (unsigned char*)buffer,  
            i,  
            &req);  
 
        free(buffer);  
    }  
    else/* GET */ 
    {  
        rtn = SendHTTP(url,NULL,NULL,0,&req);  
    }  
 
 
 
    if(!rtn)                                            //Output message and/or headerSend   
    {  
        psHeaderSend        = req.headerSend;  
        psHeaderReceive     = req.headerReceive;  
        psMessage           = req.message;  
 
 
        free(req.headerSend);  
        free(req.headerReceive);  
        free(req.message);  
        return 1;  
    }  
    else 
    {  
        return 0;  
    }  
}  
 



 

你可能感兴趣的:(C++,socket,String,null,buffer,structure)