lyx915367058 发表于 2014-10-9 09:38:52

LwIP+ STM32 HTTP和HTTPS请求格式?

HTTP请求:
#include   
#include   
#include   
#include   
// Http请求内容
static const char send_data[] =
"GET /v1.0/device/13855/sensor/22988/datapoints HTTP/1.1\r\n"
"U-ApiKey:\r\n"
"Host: api.yeelink.net\r\n\r\n";
void tcpclient(const char* host_name, int port)
{
    (void)port;
   
    char *recv_data;
    int sock, bytes_received;
   
    struct hostent *yeelink_host;
    struct in_addr yeelink_ipaddr;
    struct sockaddr_in yeelink_sockaddr;
   
    recv_data = rt_malloc(1024);
    if (recv_data == RT_NULL)
    {
      rt_kprintf("No memory\r\n");
      return;
    }
    // 第一步 DNS地址解析
    rt_kprintf("calling gethostbyname with: %s\r\n", host_name);
    yeelink_host = gethostbyname(host_name);
    yeelink_ipaddr.s_addr = *(unsigned long *) yeelink_host->h_addr_list;
    rt_kprintf("Yeelink IP Address:%s\r\n" , inet_ntoa(yeelink_ipaddr));
   
   
    yeelink_sockaddr.sin_family = AF_INET;
    yeelink_sockaddr.sin_port = htons(80);
    yeelink_sockaddr.sin_addr = yeelink_ipaddr;
    rt_memset(&(yeelink_sockaddr.sin_zero), 0, sizeof(yeelink_sockaddr.sin_zero));
   
    while(1)
    {
      // 第二步 创建套接字
      if ((sock = socket(AF_INET, SOCK_STREAM, 0)) == -1)
      {
            rt_kprintf("Socket error\n");
            rt_free(recv_data);
            return;
      }
         
      // 第三步 连接yeelink
      if (connect(sock, (struct sockaddr *)&yeelink_sockaddr, sizeof(struct sockaddr)) == -1)
      {
            rt_kprintf("Connect fail!\n");
            lwip_close(sock);
            rt_free(recv_data);
            return;
      }
         
      // 第4步 发送Http请求
      send(sock,send_data,strlen(send_data), 0);
         
      // 第5步 获得Http响应
      bytes_received = recv(sock, recv_data, 1024 - 1, 0);
      recv_data = '\0';
         
      // 响应内容为 {"timestamp":"2013-11-19T08:50:11","value":1}
      // 截取“value”之后的内容
      char* actuator_info = rt_strstr( recv_data , "\"value\"");
      int offset = rt_strlen("\"value\":");
      actuator_status = *(actuator_info + offset);
      rt_kprintf("actuator status :%c\r\n",actuator_status);
         
      // 获得开关状态,并设置LED指示灯
      char actuator_status;
      (actuator_status == '1')?rt_hw_led_on(0):rt_hw_led_off(0);
         
      rt_memset(recv_data , 0 , sizeof(recv_data));
         
      // 关闭套接字
      closesocket(sock);
         
      // 延时5S之后重新连接
      rt_thread_delay( RT_TICK_PER_SECOND * 5 );
    }
}


上面请求可以返数据
HTTP/1.1 200 OK
Server: nginx/1.0.14
Date: Sun, 24 Feb 2013 12:07:24 GMT
Content-Type: text/html
Connection: keep-alive
X-Powered-By: PHP/5.3.10
P3P: CP="NOI ADM DEV PSAi COM NAV OUR OTRo STP IND DEM"
Vary: Accept-Encoding
Content-Length: 45

{"timestamp":"2013-02-24T20:05:44","value":0}


参照上面的例子做HTTPS请求就无数据返,

通过chrome浏览器的插件postman对HTTPS请求抓取
   http://api.bmob.cn/1/classes/data
   X-Bmob-Application-Id: 3905befafce61b6bdca1acf4a20b4bda
   X-Bmob-REST-API-Key: b2cf3b28ce0a9335b1ed9c4b075f8862
有数据返回,
抓取Http请求内容:
GET /1/classes/data HTTP/1.1
Host: api.bmob.cn
X-Bmob-Application-Id: 3905befafce61b6bdca1acf4a20b4bda
X-Bmob-REST-API-Key: b2cf3b28ce0a9335b1ed9c4b075f8862
Cache-Control: no-cache
服务器IP:
IP 180.150.177.117:443

通过IP可以链接到服务器,发送请求也成功。就是无数据返回!想了两天没想明白贴出来大家帮我分析一下!

lyx915367058 发表于 2014-10-10 09:44:21

RE:LwIP+ STM32 HTTP和HTTPS请求格式?

没有测试过吗!:o

zhangxu56726 发表于 2017-7-26 09:25:40

https 要加密的,还需要握手的过程,拿到秘钥,才可以的,你用http当https,当然没有响应了

枫飞 发表于 2017-8-1 16:30:22

zhangxu56726 发表于 2017-7-26 09:25
https 要加密的,还需要握手的过程,拿到秘钥,才可以的,你用http当https,当然没有响应了 ...

https刚接触,stm32哪个系列是能支持https的呢,因为他不是涉及加密么

zhangxu56726 发表于 2017-8-2 09:24:41

stm32哪个系列都不能支持https,这是网络协议,和主控没关系

void_chen 发表于 2018-1-9 14:55:08

楼主,请教一个问题:ST又出过关于LWIP的HTTPS的方案吗

longfeixue 发表于 2018-1-15 17:37:42

学习一下
页: [1]
查看完整版本: LwIP+ STM32 HTTP和HTTPS请求格式?