相关文章推荐
老实的毛衣  ·  C语言 用http post方式 ...·  3 周前    · 
紧张的花生  ·  C语言实现http ...·  3 周前    · 
谦虚好学的刺猬  ·  android - ...·  2 年前    · 
不羁的硬盘  ·  reactjs - React Hooks ...·  2 年前    · 
近视的金鱼  ·  html转json ...·  2 年前    · 
if ( ( send_etc_sockfd = socket ( PF_INET , SOCK_STREAM , 0 ) ) < 0 ) perror ( "fail to socket at ETC_Frame.c" ) exit ( - 1 ) printf ( "TCP socket connecting\n" ) ; if ( connect ( send_etc_sockfd , ( struct sockaddr * ) & myaddr , sizeof ( myaddr ) ) < 0 ) { printf ( " connect socket error: %s(errno :%d)\n" , strerror ( errno ) , errno ) ; printf ( "ip=0x%08x\r\n" , myaddr . sin_addr . s_addr ) ; exit ( - 1 ) ;
 //方式   路径  地址
 strcpy(my_http_head,"POST http://");//
 pr = inet_ntoa(ipaddr->sin_addr);//ip
 strcat(my_http_head,pr);
 strcat(my_http_head,":");
 sprintf(strm,"%d",ipaddr->sin_port);//端口
 strcat(my_http_head,strm);
 strcat(my_http_head,serv->Charaddr);//路径
 strcat(my_http_head,serv->CharNum);//编号
 strcat(my_http_head,"json/");
 strcat(my_http_head,serv->filename);
 strcat(my_http_head," HTTP/1.1\r\n");
 if(type>=6)
  strcat(my_http_head,"Server: ");
  strcat(my_http_head,"Host: ");
 pr = inet_ntoa(ipaddr->sin_addr);//ip
 strcat(my_http_head,pr);
 strcat(my_http_head,":");
 sprintf(strm,"%d",ipaddr->sin_port);//端口
 strcat(my_http_head,strm);
 strcat(my_http_head,"\r\n");
 //strcat(my_http_head,"User-Agent: dsd\r\n");
 strcat(my_http_head,"Connection: Keep-Alive\r\n");
 //内容类型
 strcat(my_http_head,"Content-Type: ");
 strcat(my_http_head,"application/json\r\n");
    //内容长度
 strcat(my_http_head,"Content-Length: ");
// strcat(my_http_head,"16\r\n");
// strcat(my_http_head,"\r\n");//空行
// printf("my_http_head:%s\n",my_http_head);
 sprintf(strm, "%d", strlen(my_buff));
 strcat(my_http_head,strm);
 strcat(my_http_head,"\r\n\r\n");//空行
 strcat(my_http_head,my_buff);//空行
 send(fd,my_http_head,strlen(my_http_head),0)

三、创建json结构数据

cJSON *usr ;
usr=cJSON_CreateObject(); //创建根数据对象
 cJSON_AddStringToObject(usr,"brand",ETC_baseinfo.brand);                    //厂家型号
 cJSON_AddStringToObject(usr,"deviceIP",ETC_baseinfo.deviceIP);               //车辆检测器IP地址
 cJSON_AddStringToObject(usr,"deviceId",ETC_baseinfo.deviceId);                        //设备编号
 cJSON_AddNumberToObject(usr,"devicePort",ETC_baseinfo.devicePort);                        //车辆检测器Port端口
 cJSON_AddStringToObject(usr,"gantryId",ETC_baseinfo.gantryId);         //门架编号
 cJSON_AddStringToObject(usr,"serverAddress",ETC_baseinfo.serverAddress); //车辆检测器服务地址
 cJSON_AddStringToObject(usr,"stateTime",ETC_baseinfo.stateTime);     //数据生成时间
  char *out = cJSON_Print(usr); //将json形式打印成正常字符串形式
 printf("out old string:\n%s\n",out);
 free(out);
 char *out2 = cJSON_PrintUnformatted(usr);
 sprintf(my_buff,"%s",out2);
 free(out2);
 cJSON_Delete(usr); // 释放内存
不论pos还是get的实质就是一段数据,这些数据你可以用抓包方式获取,还可以使用源代码的服务器自己测试得到
pos还是get的实质就是一段数据,这些数据你可以用抓包方式获取,还可以使用源代码的服务器自己测试得到
总之以上就是属于http协议的部分,因为http是基于tcp的,所以你用c完全可以写一个socket的tcp,然后用tcp再
封装成http的即可
所有的数据都写完后,在该Socket上进行响应报文的接收。最后解析一下响应报文,检查响应状态就可以了。

What : C语言 如何用 post 方式 上传 json 数据 . Piont: C语言 Socket 实现 HTTP 协议, json 格式 数据 组包。1, C语言 Socket 实现 HTTP POST + json 格式 数据 按照 HTTP 协议发送request。 http POST 报文格式 http 报文是面向文本的。 报文分为:请求报文和响应报文 请求报文由:请求行,请求头部,空行和请求 数据 四个部分组成。 最近在工作中,由于合作商只提供uRL,我这边需要通过 HTTP 请求Get或 Post 方式 请求 Json 数据 ,然后解析 JSON 格式,解析 json 我使用的第三方库 json cpp,代码如下 #pragma once #include <iostream> #include <windows.h> #include <wininet.h> using names... 近期应项目要求,需要在嵌入式设备上 实现 数据 上传 至服务器,查找了许多资料,现在将整个过程所需要的知识整理下来,以备后续查找。 下位机:基于Linux的嵌入式开发硬件 上位机:web服务器(PC端) HTTP 协议简介 HTTP 即Hyper Text Transfer Protocol (超文本传输协议),是一种基于TCP/IP通信协议来传递 数据 (HTML 文件 ,图片 文件 ,查询结果等)。 HTTP 协议的几种请求方法 在 HTTP 1.1中,总共有8种请求方法(也叫动作),用来表明对Request-URL requests 实现 文件 上传 (对应 post man form-data)requests 实现 文件 上传 (对应 post man form-data) post man发送 post 请求,有的情况需要发送form-data格式的 数据 post 请求一般是 json 格式的情况居多,例如:header = {'Content-Type': 'application/ json ', #header写入必要的请求头内容'Acce...      最近在 C语言 项目开发中,需要用到了 http 上传 文件 ,又不想用curl库,于是打算基于socket用 C语言 实现 ,也正好研究一波表单 上传 形式。  1、首先写一个 上传 文件 html 文件 : file.html &lt;!DOCTYPE&gt; &lt;html&gt; &lt;head&gt; &lt;meta charset="utf-8"&gt; &lt;title&... What : C语言 如何用 post 方式 上传 json 数据 .Piont: C语言 Socket 实现 HTTP 协议, json 格式 数据 组包。1, C语言 Socket 实现 HTTP POST + json 格式 数据 按照 HTTP 协议发送request。 http POST 报文格式 http 报文是面向文本的。报文分为:请求报文和响应报文请求报文由:请求行,请求头部,空行和请求 数据 四个部分组成。 POST :当客户端给服务器提供信... 目录一、前言二、什么是 http ?三、浏览器是如何访问地址的?四、 HTTP 请求报文格式五、 HTTP 响应报文格式六、代码1、通过域名获取ip2、创建socket并连接服务器3、合成并发送请求报文,接收响应最后说下GET和 POST 的区别   之前在做嵌入式开发时遇到一个项目,需要用 http 与服务器通信,移植了一个 http 库,编译后发现固体太大,flash都差点不够放了,要 实现 的功能也就简单的信息获取和上报而已。于是我决定用socket简单的 实现 这些功能。 二、什么是 http ? 超文本传输协议(Hy