本帖最后由 523066680 于 2017-8-8 19:02 编辑
示例代码,不用谢- //post
- #include<stdio.h>
- #include<winsock2.h>
- #include<windows.h>
- #pragma comment(lib, "ws2_32.lib")
-
- int main()
- {
- int num;
-
- SOCKET s;
- WSADATA wsa;
- struct sockaddr_in serv;
-
- char sndBuf[1024], rcvBuf[2048];
-
- WSAStartup(MAKEWORD(2, 1), &wsa);
-
-
- if ((s = socket(AF_INET, SOCK_STREAM, 0))<0)
- {
- perror("socket error!");
- exit(1);
- }
-
- memset(&serv,0,sizeof(serv));
- serv.sin_family = AF_INET;
- serv.sin_port = htons(80);
- serv.sin_addr.S_un.S_addr = inet_addr("115.239.210.26");
-
- if ((connect(s, (struct sockaddr *)&serv, sizeof(serv)))<0)
- {
- perror("connet error!");
- exit(1);
- }
-
- memset(sndBuf, 0, 1024);
- memset(rcvBuf, 0, 2048);
-
- //头信息
- strcat(sndBuf, "POST ");
- strcat(sndBuf, "http://www.baidu.com");
- strcat(sndBuf, " HTTP/1.0\r\n");
- strcat(sndBuf, "Host: ");
- strcat(sndBuf, "baidu.com");
- strcat(sndBuf, "\r\n");
- strcat(sndBuf, "Connection: keep-alive\r\n");
-
- strcat(sndBuf, "\r\n");
-
- puts(sndBuf);
-
- if ((num = send(s,sndBuf,1024, 0))<0)
- {
- perror("send error!");
- exit(1);
- }
-
- puts("send success!\n");
-
- do
- {
- if ((num = recv(s, rcvBuf, 2048, 0))<0)
- {
- perror("recv error!");
- system("pause");
- exit(1);
- }
- else if (num>0)
- {
- printf("%s", rcvBuf);
- memset(rcvBuf, 0, 2048);
- }
- }
- while (num>0);
-
- puts("\nread success!\n");
-
- closesocket(s);
-
- system("pause");
- WSACleanup();
-
- return 0;
-
- }
复制代码
|