就是一个POST的提交,结构搞晕我了。
搞不清楚为什么GET方式没效果,而POST有效果,却提示“Run-Time Check Failure #2 - Stack around the variable 'dest' was corrupted.”
好晕倒啊!!老衲没办法了,毕竟对com不熟。记下状况以后来研究研究。
#include "Visitor.h"
#include <stdio.h>
#include <iostream>
#include "winsock.h"
#pragma comment(lib,"ws2_32.lib")
#define winsock_version 0x0101
using namespace std;
#include "URL.h";
Visitor::Visitor(void)
{
}
Visitor::~Visitor(void)
{
}
char * Visitor::POST(char * host,char* pathAndQuery, char * parms)
{
char * requestString = new char[4000];
memset(requestString,0,4000);
if(strlen(parms) > 0)
strcat(requestString,"POST ");
else
strcat(requestString,"GET ");
strcat(requestString,pathAndQuery);
strcat(requestString," HTTP/1.1\r\n");
strcat(requestString,"Host: ");
strcat(requestString,host);
strcat(requestString,"\r\nAccept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5\r\n");
strcat(requestString,"Accept-Language: zh-cn,zh;q=0.5\r\n");
strcat(requestString,"User-Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.2; Trident/4.0; .NET CLR 1.1.4322; .NET CLR 2.0.50727; .NET CLR 3.0.04506.648;\r\n");
strcat(requestString,"UA-CPU: x86\r\n");
strcat(requestString,"Accept-Charset: gb2312,utf-8;q=0.7,*;q=0.7\r\n");
//strcat(requestString,"Accept-Encoding: gzip, deflate\r\n");
strcat(requestString,"Connection: Keep-Alive\r\n");
if(strlen(parms) > 0){
strcat(requestString,"Content-Type: application/x-www-form-urlencoded\r\n");
strcat(requestString,"Content-Length: ");
char * p = new char[4];
itoa(strlen(parms),p,10);
strcat(requestString,p);
strcat(requestString,"\r\n\r\n");
strcat(requestString,parms);
}
//cout << requestString;
WSADATA wsadata;
LPHOSTENT lphostent;
SOCKET hsocket;
SOCKADDR_IN saServer;
int nRet;
if(WSAStartup(winsock_version,&wsadata))
{
}
lphostent = gethostbyname(host);
hsocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
saServer.sin_family = AF_INET;
saServer.sin_port = htons(80);
saServer.sin_addr = *((LPIN_ADDR)*lphostent->h_addr_list);
nRet = connect(hsocket, (LPSOCKADDR)&saServer, sizeof(SOCKADDR_IN));
nRet = send(hsocket, requestString, strlen(requestString), 0);
char dest[1000];
nRet=1;
int bytes = 0;
while(nRet>0)
{
nRet = recv(hsocket,(LPSTR)dest,sizeof(dest),0);
if(nRet > 0)
dest[nRet] = 0;
else{
//dest[0] = 0;
break;
}
//printf("\nReceived bytes:%d\n",nRet);
try{
cout << dest;
}catch(exception ex){}
bytes += nRet;
}
closesocket(hsocket);
//delete []dest;
printf("\nReceived bytes:%d\n",bytes);
delete requestString;
return "";
}
posted on 2008-11-05 16:55
BirdsHover 阅读(2864)
评论(1) 编辑 收藏 引用