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

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

170     free(work);

  171 }

  172

  173 //*******************************************************************************************************

  174 // SendHTTP:

  175 //                  Main entry point for this code.

  176 //                    url           - The URL to GET/POST to/from.

  177 //                    headerSend        - Headers to be sent to the server.

  178 //                    post          - Data to be posted to the server, NULL if GET.

  179 //                    postLength    - Length of data to post.

  180 //                    req           - Contains the message and headerSend sent by the server.

  181 //

  182 //                    returns 1 on failure, 0 on success.

  183 //*******************************************************************************************************

  184 int Request::SendHTTP(string url,LPCSTR headerReceive,BYTE *post,

  185                       DWORD postLength,HTTPRequest *req)

  186 {

  187     WSADATA         WsaData;

  188     SOCKADDR_IN     sin;

  189     SOCKET          sock;

  190     char            buffer[512];

  191     char            protocol[20],host[256],request[1024];

  192     int             l,port,chars,err;

  193     MemBuffer       headersBuffer,messageBuffer;

  194     char            headerSend[1024];

  195     BOOL            done;

  196

  197

  198

  199

  200     ParseURL(url,protocol,sizeof(protocol),host,sizeof(host),       // Parse the URL

  201         request,sizeof(request),&port);

  202     if(strcmp(protocol,"HTTP"))

  203         return 1;

  204

  205     err = WSAStartup (0x0101, &WsaData);                            // Init Winsock

  206     if(err!=0)

  207         return 1;

  208

  209     sock = socket (AF_INET, SOCK_STREAM, 0);

  210     //if (socket == INVALID_SOCKET)

  211     if (sock == INVALID_SOCKET)

  212     {

  213         return 1;

  214     }

  215

  216     sin.sin_family = AF_INET;                                       //Connect to web sever

  217     sin.sin_port = htons( (unsigned short)port );

  218     sin.sin_addr.s_addr = GetHostAddress(host);

  219

  220     if( connect (sock,(LPSOCKADDR)&sin, sizeof(SOCKADDR_IN) ) )

  221     {

  222

  223         return 1;

  224     }

  225

  226

  227     if( !*request )

  228         lstrcpyn(request,"/",sizeof(request));

  229

  230     if( post == NULL )

  231     {

  232         SendString(sock,"GET ");

  233         strcpy(headerSend, "GET ");

  234     }

  235     else

  236     {

  237         SendString(sock,"POST ");

  238         strcpy(headerSend, "POST ");

  239     }

  240     SendString(sock,request);

  241     strcat(headerSend, request);

  242

  243     SendString(sock," HTTP/1.0\r\n");

  244     strcat(headerSend, " HTTP/1.0\r\n");

  245

  246     SendString(sock,"Accept: image/gif, image/x-xbitmap,"

  247         " image/jpeg, image/pjpeg, application/vnd.ms-excel,"

  248         " application/msword, application/vnd.ms-powerpoint,"

  249         " */*\r\n");

  250     strcat(headerSend, "Accept: image/gif, image/x-xbitmap,"

  251         " image/jpeg, image/pjpeg, application/vnd.ms-excel,"

  252         " application/msword, application/vnd.ms-powerpoint,"

  253         " */*\r\n");

  254

  255     SendString(sock,"Accept-Language: en-us\r\n");

  256     strcat(headerSend, "Accept-Language: en-us\r\n");

  257

  258     SendString(sock,"Accept-Encoding: gzip, default\r\n");

  259     strcat(headerSend, "Accept-Encoding: gzip, default\r\n");

  260

  261     SendString(sock,"User-Agent: Neeao/4.0\r\n");

  262     strcat(headerSend, "User-Agent: Neeao/4.0\r\n");

  263

  264     if(postLength)

  265     {

  266         sprintf(buffer,"Content-Length: %ld\r\n",postLength);

  267         SendString(sock,buffer);

  268         strcat(headerSend, buffer);

  269     }

  270     //SendString(sock,"Cookie: mycookie=blablabla\r\n");

  271     //  printf("Cookie: mycookie=blablabla\r\n");

  272     SendString(sock,"Host: ");

  273     strcat(headerSend, "Host: ");

  274

  275     SendString(sock,host);

  276     strcat(headerSend, host);

  277

  278     SendString(sock,"\r\n");

  279     strcat(headerSend, "\r\n");

  280

  281     if( (headerReceive!=NULL) && *headerReceive )

  282     {

  283         SendString(sock,headerReceive);

  284         strcat(headerSend, headerReceive);

  285     }

  286

  287     SendString(sock,"\r\n");                                // Send a blank line to signal end of HTTP headerReceive

  288     strcat(headerSend, "\r\n");

  289

  290     if( (post!=NULL) && postLength )

  291     {

  292         send(sock,(const char*)post,postLength,0);

  293         post[postLength]    = '\0';

  294

  295         strcat(headerSend, (const char*)post);

  296     }

  297

  298     //strcpy(req->headerSend, headerSend);

  299     req->headerSend     = (char*) malloc( sizeof(char*) * strlen(headerSend));

  300     strcpy(req->headerSend, (char*) headerSend );

  301

  302

  303     MemBufferCreate(&headersBuffer );

  304     chars = 0;

  305     done = FALSE;

  306



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

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