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

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

33 //*******************************************************************************************************

  34 //MemBufferCreate:

  35 //                  Passed a MemBuffer structure, will allocate a memory buffer

  36 //                   of MEM_BUFFER_SIZE.  This buffer can then grow as needed.

  37 //*******************************************************************************************************

  38 void Request::MemBufferCreate(MemBuffer *b)

  39 {

  40     b->size = MEM_BUFFER_SIZE;

  41     b->buffer =(unsigned    char *) malloc( b->size );

  42     b->position = b->buffer;

  43 }

  44

  45 //*******************************************************************************************************

  46 // MemBufferGrow:

  47 //                  Double the size of the buffer that was passed to this function.

  48 //*******************************************************************************************************

  49 void Request::MemBufferGrow(MemBuffer *b)

  50 {

  51     size_t sz;

  52     sz = b->position - b->buffer;

  53     b->size = b->size *2;

  54     b->buffer =(unsigned    char *) realloc(b->buffer,b->size);

  55     b->position = b->buffer + sz;   // readjust current position

  56 }

  57

  58 //*******************************************************************************************************

  59 // MemBufferAddByte:

  60 //                  Add a single byte to the memory buffer, grow if needed.

  61 //*******************************************************************************************************

  62 void Request::MemBufferAddByte(MemBuffer *b,unsigned char byt)

  63 {

  64     if( (size_t)(b->position-b->buffer) >= b->size )

  65         MemBufferGrow(b);

  66

  67     *(b->position++) = byt;

  68 }

  69

  70 //*******************************************************************************************************

  71 // MemBufferAddBuffer:

  72 //                  Add a range of bytes to the memory buffer, grow if needed.

  73 //*******************************************************************************************************

  74 void Request::MemBufferAddBuffer(MemBuffer *b,

  75                                  unsigned char *buffer, size_t size)

  76 {

  77     while( ((size_t)(b->position-b->buffer)+size) >= b->size )

  78         MemBufferGrow(b);

  79

  80     memcpy(b->position,buffer,size);

  81     b->position+=size;

  82 }

  83

  84 //*******************************************************************************************************

  85 // GetHostAddress:

  86 //                  Resolve using DNS or similar(WINS,etc) the IP

  87 //                   address for a domain name such as www.wdj.com.

  88 //*******************************************************************************************************

  89 DWORD Request::GetHostAddress(LPCSTR host)

  90 {

  91     struct hostent *phe;

  92     char *p;

  93

  94     phe = gethostbyname( host );

  95

  96     if(phe==NULL)

  97         return 0;

  98

  99     p = *phe->h_addr_list;

  100     return *((DWORD*)p);

  101 }

  102

  103 //*******************************************************************************************************

  104 // SendString:

  105 //                  Send a string(null terminated) over the specified socket.

  106 //*******************************************************************************************************

  107 void Request::SendString(SOCKET sock,LPCSTR str)

  108 {

  109     send(sock,str,strlen(str),0);

  110 }

  111

  112 //*******************************************************************************************************

  113 // ValidHostChar:

  114 //                  Return TRUE if the specified character is valid

  115 //                      for a host name, i.e. A-Z or 0-9 or -.:

  116 //*******************************************************************************************************

  117 BOOL Request::ValidHostChar(char ch)

  118 {

  119     return( isalpha(ch) || isdigit(ch)

  120         || ch=='-' || ch=='.' || ch==':' );

  121 }

  122

  123

  124 //*******************************************************************************************************

  125 // ParseURL:

  126 //                  Used to break apart a URL such as

  127 //                      http://www.localhost.com:80/TestPost.htm into protocol, port, host and request.

  128 //*******************************************************************************************************

  129 void Request::ParseURL(string url,LPSTR protocol,int lprotocol,LPSTR host,int lhost,LPSTR request,int lrequest,int *port)

  130 {

  131     char *work,*ptr,*ptr2;

  132

  133     *protocol = *host = *request = 0;

  134     *port=80;

  135

  136     work = strdup(url.c_str());

  137     strupr(work);

  138

  139     ptr = strchr(work,':');                         // find protocol if any

  140     if(ptr!=NULL)

  141     {

  142         *(ptr++) = 0;

  143         lstrcpyn(protocol,work,lprotocol);

  144     }

  145     else

  146     {

  147         lstrcpyn(protocol,"HTTP",lprotocol);

  148         ptr = work;

  149     }

  150

  151     if( (*ptr=='/') && (*(ptr+1)=='/') )            // skip past opening /'s

  152         ptr+=2;

  153

  154     ptr2 = ptr;                                     // find host

  155     while( ValidHostChar(*ptr2) && *ptr2 )

  156         ptr2++;

  157

  158     *ptr2=0;

  159     lstrcpyn(host,ptr,lhost);

  160

  161     lstrcpyn(request,url.c_str() + (ptr2-work),lrequest);   // find the request

  162

  163     ptr = strchr(host,':');                         // find the port number, if any

  164     if(ptr!=NULL)

  165     {

  166         *ptr=0;

  167         *port = atoi(ptr+1);

  168     }

  169



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

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