设为首页 加入收藏

TOP

纯C++的Socket访问Http封装类(一)
2014-11-22 12:20:26 】 浏览:360
Tags:Socket 访问 Http 封装

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

首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇理解boost::bind的实参传递方式 下一篇C++socket编程基础(理论篇)

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目