一、
http请求格式
URL包含:/index/index2?a=1&b=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: ");
else
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上进行响应报文的接收。最后解析一下响应报文,检查响应状态就可以了。