栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 系统运维 > 运维 > Linux

知识巩固源码落实之4:http get同步请求数据demo

Linux 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

知识巩固源码落实之4:http get同步请求数据demo

1:背景介绍

http是基于C/S模式的,一直对如何实现http请求很疑惑。

http的协议理论了解很多,却一直无从下手不知道怎么实现一个http的请求。

作为一个http的客户端,请求服务器的一个消息,实现demo,做代码备份。

1.1:协议简单了解

这里实现一个http的get请求,获取对端服务器的响应进行打印,实现demo

//http请求实际就是按照http协议,构造特定的数据,使用tcp进行发送 
//请求行 请求头部 空行  请求数据
    // GET %s %srn   resource  HTTP_VERSION   请求行
	// Host: %srn    hostname					请求头
	// %srn          CONNECTION_TYPE			请求头
	// rn
1.2:关注细节 1.2.1:需要关注hostname转ip的相关方法

把hostname转为目的服务器的具体ip,创建tcp client,进行连接,发送数据得请求。

//转换 url和ip  返回ip
char *get_ip_by_host(const char * hostname)
{
	//通过域名获取ip
	struct hostent *host_entry = gethostbyname(hostname);
	if(host_entry == NULL)
	{
		return NULL;
	}

	//通过 struct hostent * 可以获取到域名  ip列表等信息
	printf("host name is [%s] n", host_entry->h_name);
	printf("the other name is :n");
	for(int i=0; host_entry->h_aliases[i]; ++i)
	{
		printf("   [i:%d][%s] n", i, host_entry->h_aliases[i]);
	}

	printf("Address type: %sn", (host_entry->h_addrtype==AF_INET) ? "AF_INET": "AF_INET6");

	printf("address length: %d n", host_entry->h_length);

	printf("ip list is :n");
	for(int i=0; host_entry->h_addr_list[i]; ++i)
	{
		// printf("   [i:%d][%s] n", i, host_entry->h_addr_list[i]);
		printf("   [i:%d][%s] n", i, inet_ntoa(*(struct in_addr*)host_entry->h_addr_list[i]));
	}
	return inet_ntoa(*(struct in_addr*)*host_entry->h_addr_list);
}
1.2.2:需要关注如何按照http协议构造要发送的数据

构造发送的字符串,使用tcp进行发送。

#define HTTP_VERSION    "HTTP/1.1"

//好像不同的浏览器有不同的一些参数  可以在网页用f12请求进行查看 
#define USER_AGENT      "User-Agent: Mozilla/5.0 (Windows NT 5.1; rv:10.0.2) Gecko/20100101 Firefox/10.0.2rn"
#define ENCODE_TYPE     "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*

//练习http的get请求,通过url转换ip,创建tcp,发送构造的http协议,请求服务器的内容
//异步发送和接收  其实是利用了epoll event的指针,指向的函数指针

#include 
#include 
#include 

#include 
#include 
#include 
#include 
#include 
#include        
#include  

#include 
#include 
#include 


//转换 url和ip  返回ip
char *get_ip_by_host(const char * hostname);
//创建socket 根据服务器ip和断开 进行连接 返回fd
int create_http_socket(const char * ip);
//发送http请求 返回发送成功还是失败
int send_http_request(int fd, const char * hostname, const char* resource);
//获取回应消息并打印 监听对端的回应进行打印 返回成功或者失败
int get_recv_http_response(int sockfd);


//作为http的客户端,一请求一结束
int main(int argc, char *argv[])
{
	if(argc < 3)
	{
		printf(" please use ./%s hostname resouse nn", argv[0]);
		return -1;
	}
	//根据入参,解析获取到的url,保存请求的资源
	char * ip = get_ip_by_host(argv[1]);
	printf("get ip is [%s] n", ip);

	//构造tcp连接 作为tcp的客户端,主动连接目标服务器
	int connfd = create_http_socket(ip);

	//构造http报文,发送http请求,并获取到返回结果
	send_http_request(connfd, argv[1], argv[2]);

	//监听获取http的请求的回复 内部做输出查看
	get_recv_http_response(connfd);

	close(connfd); //客户端主动关闭fd
	return 0;
}
// struct hostent {
//    char  *h_name;            
//    char **h_aliases;         
//    int    h_addrtype;        
//    int    h_length;          
//    char **h_addr_list;       
//            }

//转换 url和ip  返回ip
char *get_ip_by_host(const char * hostname)
{
	//通过域名获取ip
	struct hostent *host_entry = gethostbyname(hostname);
	if(host_entry == NULL)
	{
		return NULL;
	}

	//通过 struct hostent * 可以获取到域名  ip列表等信息
	printf("host name is [%s] n", host_entry->h_name);
	printf("the other name is :n");
	for(int i=0; host_entry->h_aliases[i]; ++i)
	{
		printf("   [i:%d][%s] n", i, host_entry->h_aliases[i]);
	}

	printf("Address type: %sn", (host_entry->h_addrtype==AF_INET) ? "AF_INET": "AF_INET6");

	printf("address length: %d n", host_entry->h_length);

	printf("ip list is :n");
	for(int i=0; host_entry->h_addr_list[i]; ++i)
	{
		// printf("   [i:%d][%s] n", i, host_entry->h_addr_list[i]);
		printf("   [i:%d][%s] n", i, inet_ntoa(*(struct in_addr*)host_entry->h_addr_list[i]));
	}
	return inet_ntoa(*(struct in_addr*)*host_entry->h_addr_list);
}

//创建socket 根据服务器ip和断开 进行连接 返回fd
int create_http_socket(const char * ip)
{
	//创建socket 根据Ip:port连接对端
	int sockfd = socket(AF_INET, SOCK_STREAM, 0);
	if(sockfd < 0)
	{
		return -1;
	}

	struct sockaddr_in sin = {0};
    sin.sin_addr.s_addr = inet_addr(ip);
    sin.sin_port = htons(80);
    sin.sin_family = AF_INET;

    //连接服务器 
    if (-1 == connect(sockfd, (struct sockaddr*)&sin, sizeof(struct sockaddr_in))) {
    	printf("connect http server error n");
        return -1;
    }
    //设置fd非阻塞
	fcntl(sockfd, F_SETFL, O_NONBLOCK);    
	return sockfd;
}

#define HTTP_VERSION    "HTTP/1.1"
//好像不同的浏览器有不同的一些参数  可以在网页用f12请求进行查看 
#define USER_AGENT      "User-Agent: Mozilla/5.0 (Windows NT 5.1; rv:10.0.2) Gecko/20100101 Firefox/10.0.2rn"
#define ENCODE_TYPE     "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8rn"

#define CONNECTION_TYPE "Connection: closern"

#define BUFFER_SIZE 4096
//发送http请求 返回发送成功还是失败

int send_http_request(int fd, const char * hostname, const char* resource)
{
	char send_buff[BUFFER_SIZE] = {0};
	//构造发送的请求报文  请求行 请求头部 空行  请求数据

	// GET /teacher_6.jpg HTTP/1.1
	// Host: www.0voice.com
	// User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.113 Safari/537.36
	// Content-Type: application/x-www-form-urlencoded
	// Content-Length: 9
	// lingsheng

//只关注了必须的参数  测试一下
	// GET %s %srn   resource  HTTP_VERSION   请求行
	// Host: %srn    hostname					请求头
	// %srn          CONNECTION_TYPE			请求头
	// rn
	int len = sprintf(send_buff, "GET %s %srnHost: %srn%srnrn",
		resource, HTTP_VERSION, hostname, CONNECTION_TYPE);

	printf("send request buff is [%lu][%s] n", strlen(send_buff), send_buff);
	int buff_len = strlen(send_buff);
	int sendlen = send(fd, send_buff, strlen(send_buff), 0);
	if(buff_len != sendlen)
	{
		printf("send buffer error n");
		return -1;
	}

	return 0;
}

//获取回应消息并打印 监听对端的回应进行打印 返回成功或者失败
int get_recv_http_response(int sockfd)
{
	//使用io多路复用select对接收 进行输出

	struct timeval tv;
    tv.tv_sec = 5;
    tv.tv_usec = 0;


	fd_set fdread;
    FD_ZERO(&fdread);
    FD_SET(sockfd, &fdread);

    //存储最终结果
    char *result = malloc(sizeof(int));
    result[0] = '';

    //存储每次recv的数据
    int len = 0;
    char buffer[BUFFER_SIZE] = {0};
    while (1) {
 
        int selection = select(sockfd+1, &fdread, NULL, NULL, &tv);
        if (!selection || !(FD_ISSET(sockfd, &fdread))) {
            break;
        } else {
        	//这里是客户端  只针对这个连接的场景
            len = recv(sockfd, buffer, BUFFER_SIZE, 0);
            if (len == 0) break;
 			//重新申请大小并对接收到的数据进行追加 
            result = realloc(result, (strlen(result) + len + 1) * sizeof(char));
            strncat(result, buffer, len);
        }
    }

    if(result == NULL)
    {
    	return -1;
    }
    //打印并释放接收到的数据
    puts(result);
    free(result);
	return 0;
}
3:测试结果:

如,请求baidu的主页,有的url是https的,这里暂时只是http的练习,https的相关需要安全认证。

./XXX www.baidu.com /
./XXX api.seniverse.com /v3/weather/now.json?key=0pyd8z7jouficcil&location=beijing&language=zh-Hans&unit=c

hlp@ubuntu:~/220107$ ./get www.baidu.com /
... #这里打印过多 省略
hlp@ubuntu:~/220107$ ./get api.seniverse.com /v3/weather/now.json?key=0pyd8z7jouficcil&location=hefei&language=zh-Hans&unit=c
[1] 9103
[2] 9104
[3] 9105
hlp@ubuntu:~/220107$ host name is [api.seniverse.com] 
the other name is :
Address type: AF_INET
address length: 4 
ip list is :
   [i:0][116.62.81.138] 
get ip is [116.62.81.138] 
send request buff is [104][GET /v3/weather/now.json?key=0pyd8z7jouficcil HTTP/1.1
Host: api.seniverse.com
Connection: close


] 
HTTP/1.1 400 Bad Request
Date: Sun, 13 Feb 2022 14:26:26 GMT
Content-Type: application/json
Content-Length: 55
Connection: close
access-control-allow-origin: *
x-request-id: FtNerTOXZFLlW0IvMUaB

{"status":"Wrong parameters.","status_code":"AP010001"}

我开始试着积累一些常用代码:自己代码库中备用

我的知识储备更多来自这里,推荐你了解:Linux,Nginx,ZeroMQ,MySQL,Redis,fastdfs,MongoDB,ZK,流媒体,CDN,P2P,K8S,Docker,TCP/IP,协程,DPDK等技术内容,立即学习

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/733748.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号