C语言怎么实现网页的数据交换?
- 提问者网友:寂寞撕碎了回忆
- 2021-08-11 11:52
- 五星知识达人网友:往事隔山水
- 2021-08-11 12:05
#include
#include
#include
main(int argc,char *argv[]){
char buff[4096];
SOCKET sock;
SOCKADDR_IN addr;
WSADATA wsa;
WORD wVersionRequested;
struct hostent *hs;
int count, len;
char *url="www.sample.com";
if (argc!=4) {printf("Error!
"); return -1; }
len=(strlen(argv[1])+strlen(argv[2])+strlen(argv[3])+49);
wVersionRequested = MAKEWORd( 2, 0 );
if (WSAStartup(wVersionRequested , &wsa)!=0){
printf("Winsock Initialization failed.
"); return -1;
}
if ((sock=socket(AF_INET,SOCK_STREAM,0))==INVALID_SOCKET){
printf("Can not create socket.
"); return -1;
}
addr.sin_family = AF_INET;
addr.sin_port = htons(80);
_snprintf(buff, 4096, "POST /cgi-bin/forum.cgi HTTP/1.1
"
"Host: %s
"
"Content-Type:
application/x-www-form-urlencoded
"
"Content-Length: %d
"
"
"
"action=add&Title=%s&URL=
http://%s&yuzi=1&Description=%s
",
url,
len,
argv[1],
argv[2],
argv[3]);
if ((addr.sin_addr.s_addr=inet_addr(url))==-1){
if ((hs=gethostbyname(url))==NULL){
printf("Can not resolve specified host.
"); return -1;
}
addr.sin_family = hs->h_addrtype;
memcpy((void *)&addr.sin_addr.s_addr,hs->h_addr,hs->h_length);
}
if (connect(sock,(LPSOCKADDR)&addr,sizeof(addr))==SOCKET_ERROR){
printf("Can not connect to specified host.
"); return -1;
}
send(sock,buff,strlen(buff),0);
recv(sock,buff,sizeof(buff),0);
closesocket(sock);
}
return 0;