永发信息网

C语言怎么实现网页的数据交换?

答案:1  悬赏:80  手机版
解决时间 2021-08-11 16:48
  • 提问者网友:寂寞撕碎了回忆
  • 2021-08-11 11:52
想用C做一个论坛的灌水机。但是不知道怎么实现网页的数据交换?请教???
最佳答案
  • 五星知识达人网友:往事隔山水
  • 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;
  

我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯