请问http协议高手,我c语言写一个表单提交的程序,浏览器表单提交和c程序发送的数据一模一样,但结果却不一样!!
我用抓包工具分析测试,发现用浏览器表单提交和c程序发送的数据一模一样,而浏览器能接收到应答,而c程序确一直阻塞于套接字的读过程,根本没有应答,代码如下:
#include "stdio.h"
#include "stdlib.h"
#include "winsock2.h"
#pragma comment(lib,"ws2_32.lib")
int main(int argc, char* argv[])
{
SOCKET hsocket;
SOCKADDR_IN saServer;
WSADATA wsadata;
LPHOSTENT lphostent;
int nRet;
char* host_name="**.**.**.**";
char* reqHead="GET /test/proc8.jsp?cardno=9175880000000051&action=3 HTTP/1.1\r\n"
"Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/msword, application/vnd.ms-excel, application/vnd.ms-powerpoint, */*\r\n"
"Referer: http://**.**.**.**:8088/test/pay.jsp\r\n"
"Accept-Language: zh-cn\r\n"
"Accept-Encoding: gzip, deflate\r\n"
"User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727)\r\n"
"Host: **.**.**.**:8088\r\n"
"Connection: Keep-Alive\r\n"
"Cookie: JSESSIONID=A55DFDF6FE50070D841925B49D425BB2\r\n";
// 初始化套接字
if(WSAStartup(MAKEWORD(2,2),&wsadata))
printf("初始化SOCKET出错!");
lphostent=gethostbyname(host_name);
if(lphostent==NULL)
printf("lphostent为空!");
hsocket = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
saServer.sin_family = AF_INET;
saServer.sin_port = htons(8088);
saServer.sin_addr = *((LPIN_ADDR)*lphostent->h_addr_list);
// 利用SOCKET连接 FineReader
nRet = connect(hsocket,(LPSOCKADDR)&saServer,sizeof(SOCKADDR_IN));
if(nRet == SOCKET_ERROR)
{
printf("建立连接时出错!");
closesocket(hsocket);
return 0;
}
// 利用SOCKET发送
nRet = send(hsocket,reqHead,strlen(reqHead),0);
if(nRet==SOCKET_ERROR)
{
printf("发送数据包时出错!");
closesocket(hsocket);
}
char Dest[3000];
nRet=1;
while(nRet>0)
{
// 接收返回数据包
nRet=recv(hsocket,(LPSTR)Dest,sizeof(Dest),0);
printf("nRet is:%d\n");
if(nRet>0)
Dest[nRet]=0;
else
Dest[0]=0;
// 显示返回数据包的大小、内容
printf("\nReceived bytes:%d\n",nRet);
printf("Result:\n%s",Dest);
}
return 0;
}
[解决办法]
"Connection: Keep-Alive\r\n"
"Cookie: JSESSIONID=A55DFDF6FE50070D841925B49D425BB2\r\n";
改成
"Connection: Close\r\n"
"Cookie: JSESSIONID=A55DFDF6FE50070D841925B49D425BB2\r\n\r\n";