我是靠谱客的博主 激动胡萝卜,最近开发中收集的这篇文章主要介绍aiyao Linux下用c语言实现发送http请求 方式可以Get或者Post,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

[1].[代码] Linux下用c语言实现发送http请求 方式可以Get或者Post 跳至[1]

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#include <stdio.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <time.h>
#include <errno.h>
#include <signal.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/wait.h>
#include <sys/time.h>
#include <netinet/in.h>
#include <arpa/inet.h>
 
#define IPSTR "61.147.124.120"
#define PORT 80
#define BUFSIZE 1024
 
int main( int argc, char **argv)
{
         int sockfd, ret, i, h;
         struct sockaddr_in servaddr;
         char str1[4096], str2[4096], buf[BUFSIZE], *str;
         socklen_t len;
         fd_set   t_set1;
         struct timeval  tv;
 
         if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0 ) {
                 printf ( "创建网络连接失败,本线程即将终止---socket error!n" );
                 exit (0);
         };
 
         bzero(&servaddr, sizeof (servaddr));
         servaddr.sin_family = AF_INET;
         servaddr.sin_port = htons(PORT);
         if (inet_pton(AF_INET, IPSTR, &servaddr.sin_addr) <= 0 ){
                 printf ( "创建网络连接失败,本线程即将终止--inet_pton error!n" );
                 exit (0);
         };
 
         if (connect(sockfd, ( struct sockaddr *)&servaddr, sizeof (servaddr)) < 0){
                 printf ( "连接到服务器失败,connect error!n" );
                 exit (0);
         }
         printf ( "与远端建立了连接n" );
 
         //发送数据
         memset (str2, 0, 4096);
         strcat (str2, "qqCode=474497857" );
         str=( char *) malloc (128);
         len = strlen (str2);
         sprintf (str, "%d" , len);
 
         memset (str1, 0, 4096);
         strcat (str1, "POST /webservices/qqOnlineWebService.asmx/qqCheckOnline HTTP/1.1n" );
         strcat (str1, "Host: www.webxml.com.cnn" );
         strcat (str1, "Content-Type: application/x-www-form-urlencodedn" );
         strcat (str1, "Content-Length: " );
         strcat (str1, str);
         strcat (str1, "nn" );
 
         strcat (str1, str2);
         strcat (str1, "rnrn" );
         printf ( "%sn" ,str1);
 
         ret = write(sockfd,str1, strlen (str1));
         if (ret < 0) {
                 printf ( "发送失败!错误代码是%d,错误信息是'%s'n" , errno , strerror ( errno ));
                 exit (0);
         } else {
                 printf ( "消息发送成功,共发送了%d个字节!nn" , ret);
         }
 
         FD_ZERO(&t_set1);
         FD_SET(sockfd, &t_set1);
 
         while (1){
                 sleep(2);
                 tv.tv_sec= 0;
                 tv.tv_usec= 0;
                 h= 0;
                 printf ( "--------------->1" );
                 h= select(sockfd +1, &t_set1, NULL, NULL, &tv);
                 printf ( "--------------->2" );
 
                 //if (h == 0) continue;
                 if (h < 0) {
                         close(sockfd);
                         printf ( "在读取数据报文时SELECT检测到异常,该异常导致线程终止!n" );
                         return -1;
                 };
 
                 if (h > 0){
                         memset (buf, 0, 4096);
                         i= read(sockfd, buf, 4095);
                         if (i==0){
                                 close(sockfd);
                                 printf ( "读取数据报文时发现远端关闭,该线程终止!n" );
                                 return -1;
                         }
 
                         printf ( "%sn" , buf);
                 }
         }
         close(sockfd);
 
 
         return 0;
}

最后

以上就是激动胡萝卜为你收集整理的aiyao Linux下用c语言实现发送http请求 方式可以Get或者Post的全部内容,希望文章能够帮你解决aiyao Linux下用c语言实现发送http请求 方式可以Get或者Post所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(45)

评论列表共有 0 条评论

立即
投稿
返回
顶部