纯C++的Socket访问Http封装类实例
来源:优易学  2011-11-25 17:37:48   【优易学:中国教育考试门户网】   资料下载   IT书店

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

  2.以下是调用方法:

  1 #include "stdafx.h"

  2 #include <iostream>

  3 #include <string>

  4 #include "http\request.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头:"<<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 }

  33

  直接上代码了,

  Request.h

  1 //******************************************

  2 //纯C++的socket访问Http封装类,Neeao修改

  3 //http://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 <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

  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 <string>

  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

[1] [2] [3] [4] 下一页

责任编辑:小草

文章搜索:
 相关文章
热点资讯
资讯快报
热门课程培训