Using WIN32 to Web server to send request,how to remove the HTTP response header Information!
-
I hope to use a Socket to a Web server to send request, and then obtain Web server respond to my data. But in the response data inside, there is always a response head, how should I get rid of the response head. I tried by the response data length to intercept head off response head data, but I failed, because of different response, data different lengths. How should I do?
#include <stdio.h>
#include <winsock2.h>
#pragma comment(lib,"WS2_32")//package the http head message ,
void PacketSocket(const char *host,const char *url,const char *data,char *reBuf)
{
//write the string to reBuf
wsprintf(reBuf,"POST %s HTTP/1.1\r\nAccept:text/html\r\nAccept-Language:zh-cn,en\r\nAccept-Encoding:gzip,deflate\r\nContent-type:application/x-www-form-urlencoded\r\nAccept-Charset:GBK\r\nHost:%s\r\nContent-Length:%d\r\nContent-Encoding:utf-8\r\nUser-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0; (R1 1.3))\r\nConnection:Keep-Alive\r\nSet-Cookie:JSESSIONID=193F85DD5F193A5714CBC5246218ECO3\r\n\r\n%s",url,host,strlen(data),data);
}int main(){
WSADATA wsa; WORD version=MAKEWORD(1,1); //start up the WSASocket if(WSAStartup(version,&wsa)==SOCKET\_ERROR){ printf("start error!"); return -1; } SOCKET s; //server address sockaddr\_in addr; s=socket(AF\_INET,SOCK\_STREAM,IPPROTO\_TCP); if(s==INVALID\_SOCKET) { printf("has a error,the code:%d",GetLastError()); return -1; } //I have established a J2EE WEB server, and then start it, //use the socket to send request to get index.jsp page addr.sin\_addr.S\_un.S\_addr=inet\_addr("127.0.0.1"); addr.sin\_port=htons(8080);//the port is 8080 --j2ee web server addr.sin\_family=AF\_INET; //connection the server if(connect(s,(sockaddr\*)&addr,sizeof(addr))==SOCKET\_ERROR){ printf("connect had an error!"); return -1; } char szData\[1024\];//request data //Encapsulation request message,I've built a socket j2ee project PacketSocket("localhost","/socket/index.jsp","start=0&limit=15",szData); if(send(s,szData,strlen(szData),0)==SOCKET\_ERROR){ //To the server sends request printf("send request had an error!"); return -1; } char szRecv\[1024\];//Receiving the return value int nCount=recv(s,szRecv,1024,0); if(nCount>0) { szRecv\[nCount\]='\\0'; printf("%s",szRecv); } WSACleanup(); return 0;
}
Response data as follows HTTP/1.1 200 OK Server: Apache-Coyote/1.1 Set-Cookie: JSESSIONID=0910F038DEBB2C62C41C
-
I hope to use a Socket to a Web server to send request, and then obtain Web server respond to my data. But in the response data inside, there is always a response head, how should I get rid of the response head. I tried by the response data length to intercept head off response head data, but I failed, because of different response, data different lengths. How should I do?
#include <stdio.h>
#include <winsock2.h>
#pragma comment(lib,"WS2_32")//package the http head message ,
void PacketSocket(const char *host,const char *url,const char *data,char *reBuf)
{
//write the string to reBuf
wsprintf(reBuf,"POST %s HTTP/1.1\r\nAccept:text/html\r\nAccept-Language:zh-cn,en\r\nAccept-Encoding:gzip,deflate\r\nContent-type:application/x-www-form-urlencoded\r\nAccept-Charset:GBK\r\nHost:%s\r\nContent-Length:%d\r\nContent-Encoding:utf-8\r\nUser-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0; (R1 1.3))\r\nConnection:Keep-Alive\r\nSet-Cookie:JSESSIONID=193F85DD5F193A5714CBC5246218ECO3\r\n\r\n%s",url,host,strlen(data),data);
}int main(){
WSADATA wsa; WORD version=MAKEWORD(1,1); //start up the WSASocket if(WSAStartup(version,&wsa)==SOCKET\_ERROR){ printf("start error!"); return -1; } SOCKET s; //server address sockaddr\_in addr; s=socket(AF\_INET,SOCK\_STREAM,IPPROTO\_TCP); if(s==INVALID\_SOCKET) { printf("has a error,the code:%d",GetLastError()); return -1; } //I have established a J2EE WEB server, and then start it, //use the socket to send request to get index.jsp page addr.sin\_addr.S\_un.S\_addr=inet\_addr("127.0.0.1"); addr.sin\_port=htons(8080);//the port is 8080 --j2ee web server addr.sin\_family=AF\_INET; //connection the server if(connect(s,(sockaddr\*)&addr,sizeof(addr))==SOCKET\_ERROR){ printf("connect had an error!"); return -1; } char szData\[1024\];//request data //Encapsulation request message,I've built a socket j2ee project PacketSocket("localhost","/socket/index.jsp","start=0&limit=15",szData); if(send(s,szData,strlen(szData),0)==SOCKET\_ERROR){ //To the server sends request printf("send request had an error!"); return -1; } char szRecv\[1024\];//Receiving the return value int nCount=recv(s,szRecv,1024,0); if(nCount>0) { szRecv\[nCount\]='\\0'; printf("%s",szRecv); } WSACleanup(); return 0;
}
Response data as follows HTTP/1.1 200 OK Server: Apache-Coyote/1.1 Set-Cookie: JSESSIONID=0910F038DEBB2C62C41C
Skip over the double newline (as defined in RFC 1945, RFC 2616) -or- use a networking library with a HTTP client.