纯C++的Socket访问Http封装类代码介绍 - 商帝国咨询 - 五略商书

纯C++的Socket访问Http封装类代码介绍

商帝国咨询头像

阅读 24994 | 分类:文章

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

  1 #include "stdafx.h"
  2 #include 
  3 #include 
  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头:"< 
  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 //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 
  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 S

留言区(共0条)

暂无留言,快来抢占沙发~
作者声明:非原创
平台声明:文章及评论仅代表作者本人观点,五略商书系信息发布平台,仅提供信息存储空间服务,如有侵权,请联系13521509118。转载请注明作者和出处:转自五略商书m.shangdiguo.com,作者商帝国咨询!