加入收藏 收藏网站 设为首页 设为首页
招生考试网
学历类| 阳光高考 研 究 生 自学考试 成人高考 专 升 本 中考会考 外语类| 四 六 级 职称英语 商务英语 公共英语 日语能力
资格类| 公 务 员 报 关 员 银行从业 司法考试 导 游 证 教师资格 财会类| 会 计 证 经 济 师 会计职称 注册会计 税 务 师
工程类| 一级建造 二级建造 造 价 师 造 价 员 咨 询 师 监 理 师 医学类| 卫生资格 执业医师 执业药师 执业护士 国际护士
计算机| 等级考试 软件水平 应用能力 其它类| 书画等级 美国高考 驾 驶 员 书法等级 少儿英语 报 检 员 单 证 员 出国留学
 招生考试网 - 计算机等级考试 - 考试辅导 - 正文

 
纯C++的Socket访问Http封装类
来源:fjzsksw.com 2009-11-25 编辑:yangmeiling 【字体:小 大】

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



 
网站版权与免责声明
①由于各方面情况的不断调整与变化,本网所提供的相关信息请以权威部门公布的正式信息为准.
②本网转载的文/图等稿件出于非商业性目的,如转载稿涉及版权等问题,请在两周内来电联系.
最新文章
热门文章

报名考试
全国 | 黑龙江 | 吉林 | 辽宁 | 内蒙古
青海 | 宁夏 | 甘肃 | 新疆 | 陕西
西藏 | 北京 | 天津 | 河北 | 山东
江苏 | 安徽 | 河南 | 上海 | 浙江
福建 | 广东 | 山西 | 湖南 | 湖北
江西 | 广西 | 海南 | 云南 | 贵州
四川 | 重庆
分省高校计算机考试
黑龙江 | 吉林 | 辽宁 | 内蒙古 | 河北
北京 | 天津 | 新疆 | 甘肃 | 宁夏
青海 | 陕西 | 山西 | 河南 | 山东
江苏 | 安徽 | 浙江 | 福建 | 广东
海南 | 广西 | 江西 | 湖北 | 湖南
四川 | 上海 | 重庆 | 贵州 | 云南
西藏
成绩查询
报考指南
试题答案
模拟试题
考试辅导
计算机一级 | 计算机二级 | 计算机三级 | 计算机四级
经验交流
高校计算机