基于mongoose的httpclient配置SSL与cookie

本文详细介绍了mongoose轻量级HTTP服务器项目的使用方法,包括如何通过两个核心文件搭建httpserver或httpclient,以及如何配置SSL支持https,使用OpenSSL库进行编译链接。此外,还提供了在httpclient请求中配置cookie解决特定API访问问题的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、mongoose简介

mongoose是一个轻量的开源http服务器项目:https://github.com/cesanta/mongoose

只需要使用mongoose.c和mongoose.h两个文件就可以搭建httpserver或httpclient(参考https://blog.csdn.net/u012234115/article/details/79596826

二、SSL配置

若要支持https,则需要配置SSL,这里使用OpenSSL开源库。

1、openssl源码地址为:https://www.openssl.org/source/old/ ,这里选择版本为openssl-1.0.1u.tar.gz

2、解压之后,进入源码目录openssl-1.0.1u,执行如下命令

./config -fPIC no-shared
make

当前目录下会编译出libssl.a和libcrypto.a两个库文件,在开发的时候只需要包含头文件并链接这两个库就可以了

3、将openssl-1.0.1u/include/openssl目录拷贝到自己模块的头文件目录(./inc)下,将libssl.a和libcrypto.a静态库拷贝到自己模块的库文件目录(./lib)下,在makefile中添加头文件目录和lib库:

INCLUDE += ******** -I./inc
LIB += ******* -L./lib -lssl -lcrypto -ldl

注意:编译生成的库libssl.a和libcrypto.a存在依赖关系,要把libssl.a放在libcrypto.a前面(依赖的库放后面)

4、修改mongoose.h   设置使能SSL宏        #define MG_ENABLE_SSL 1

5、编译应用代码,即可使用httpclient访问https的URL

三、配置cookie

在使用酷狗音乐API通过拼接hash值获取歌曲真实接口地址后,服务器返回如下错误

{“status”:0,“err_code”:20010,“data”:[]}

在浏览器直接访问可以获取到所有数据,但是通过mongoose的httpclient获取到的则是如上错误。原因是需要在httpclient请求头中携带cookie值为:kg_mid=2333,这里的kg_mid可以是任何值,非空就行。
修改mongoose.c中的mg_connect_http_opt函数如下所示:

struct mg_connection *mg_connect_http_opt(struct mg_mgr *mgr,
                                          mg_event_handler_t ev_handler,
                                          struct mg_connect_opts opts,
                                          const char *url,
                                          const char *extra_headers,
                                          const char *post_data) {
  char *user = NULL, *pass = NULL, *addr = NULL;
  const char *path = NULL;
  struct mbuf auth;
  struct mg_connection *nc =
      mg_connect_http_base(mgr, ev_handler, opts, "http://", "https://", url,
                           &path, &user, &pass, &addr);

  if (nc == NULL) {
    return NULL;
  }

  mbuf_init(&auth, 0);
  if (user != NULL) {
    mg_basic_auth_header(user, pass, &auth);
  }

  mg_printf(nc, "%s %s HTTP/1.1\r\nHost: %s\r\nContent-Length: %" SIZE_T_FMT
                "\r\nCookie: %s=%d\r\n%.*s%s\r\n%s",
            post_data == NULL ? "GET" : "POST", path, addr,
            post_data == NULL ? 0 : strlen(post_data), "kg_mid", 2333, (int) auth.len,
            (auth.buf == NULL ? "" : auth.buf),
            extra_headers == NULL ? "" : extra_headers,
            post_data == NULL ? "" : post_data);
  
  //mg_printf(nc, "Cookie: %s=%d\r\n", "kg_mid", 2333);  //runoob added:增加酷狗音乐API需要的cookie

  mbuf_free(&auth);
  MG_FREE(user);
  MG_FREE(pass);
  MG_FREE(addr);
  return nc;
}

添加cookie后抓包client的请求报文如下

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值