Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1955623
  • 博文数量: 356
  • 博客积分: 8284
  • 博客等级: 中将
  • 技术积分: 4580
  • 用 户 组: 普通用户
  • 注册时间: 2009-05-15 20:25
个人简介

天行健,君子以自强不息

文章分类

全部博文(356)

文章存档

2018年(1)

2016年(4)

2015年(13)

2014年(14)

2013年(2)

2012年(25)

2011年(43)

2010年(65)

2009年(189)

分类: C/C++

2014-12-04 13:50:19

程序代码如下

点击(此处)折叠或打开

  1. static char http_request[500]= {0};
  2. BYTE yeelink_post(const char *device_id,const char *sensors_id,float value)
  3. {
  4.         char remote_server[]="api.yeelink.net";
  5.         char str_tmp[128] = {0};
  6.         // Http内容,表单内容
  7.         char http_content[32] = {0};
  8.         sprintf(str_tmp,"/v1.0/device/%s/sensor/%s/datapoints",device_id,sensors_id);
  9.         // 确定HTTP表单提交内容 {"value":20}
  10.         sprintf( http_content , "{\"value\":%3.1f}" , value);
  11.         // 确定 HTTP请求首部
  12.         // 例如POST /v1.0/device/98d19569e0474e9abf6f075b8b5876b9/1/1/datapoints/add HTTP/1.1\r\n
  13.         sprintf( http_request , "POST %s HTTP/1.1\r\n",str_tmp);
  14.         // 增加属性 例如 Host: api.machtalk.net\r\n
  15.         sprintf( str_tmp , "Host:%s\r\n" , remote_server);
  16.         strcat( http_request , str_tmp);
  17.  
  18.         // 增加密码 例如 APIKey: "9908e3876669432610e9a3ac2fb07490"
  19.         sprintf( str_tmp , "U-ApiKey:%s\r\n" , "9908e3876669432610e9a3ac2fb07490");//需要替换为自己的APIKey
  20.         strcat( http_request , str_tmp);
  21.         //
  22.         strcat( http_request , "Accept: */*\r\n");
  23.         // 增加提交表单内容的长度 例如 Content-Length:12\r\n
  24.         sprintf( str_tmp , "Content-Length:%d\r\n" ,strlen(http_content) );
  25.         strcat( http_request , str_tmp);
  26.         // 增加表单编码格式 Content-Type:application/x-www-form-urlencoded\r\n
  27.         strcat( http_request , "Content-Type: application/x-www-form-urlencoded\r\n");
  28.         strcat( http_request , "Connection: close\r\n");
  29.         // HTTP首部和HTTP内容 分隔部分
  30.         strcat( http_request , "\r\n");
  31.         // HTTP负载内容
  32.         strcat( http_request , http_content);
  33.         return 0;
  34. }
  35. void CVc_tcpclientDlg::OnButtonSend2()
  36. {
  37.     yeelink_post("15983","27668",(float)39.9);
  38.     printf("%s",http_request);
  39. //    send(m_socket,http_request,strlen(http_request),0);    
  40. }

测试通过


阅读(944) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~