首页 新闻 搜索 专区 学院

[Linux C]接收JSON数据时 怎么去掉HTTP头啊啊?

0
[待解决问题]

接收到的消息为

[HTTP/1.1 200 OK
Date: Fri, 31 Aug 2018 07:52:50 GMT
Server: Apache
X-Powered-By: PHP/5.4.40
Content-Length: 316
Connection: close
Content-Type: text/html

{"resp_code":0,"resp_msg":"get the query result.","resp_detail":{"is_exist":1,"order_state":"0","identifid":"ohM8Fj2uy1HAEOzP7enHYC4L1pMk","print_no":"8388800762","total_fee":"1","pay_channel":"1","pay_time":"2018-08-31 15:52:27","time_stamp":1535701970,"nonce":1535701970,"sign":"9D15580140229685DAF06CD4FEF1E37B"}}]

怎么去掉HTTP头信息啊?

Dellienware的主页 Dellienware | 初学一级 | 园豆:39
提问于:2018-08-31 16:03
< >
分享
所有回答(3)
0

按连续2个换行符切断.
http报文协议的规则就是.2个连续的换行符后面的是正文.

吴瑞祥 | 园豆:28851 (高人七级) | 2018-08-31 16:19

怎么切,只得到后面的josn,可以写下详细代码吗...谢谢了

支持(0) 反对(0) Dellienware | 园豆:39 (初学一级) | 2018-08-31 16:20

@Dellienware: 这有什么好怎么切的..百度下你用的语言,怎么截取字符串啊..

支持(0) 反对(0) 吴瑞祥 | 园豆:28851 (高人七级) | 2018-08-31 16:48

@吴瑞祥:
我用的这个方法
char* p=strtok(tmpbuff,"\r\n");
while(p!=NULL){
p=strtok(NULL,"\r\n");
}

最后输出的
p= [HTTP/1.1 200 OK] ,File=[/app/bgacc/caoy/online/server/source/transaction/src/TXN_7105.c],Line=[914]

p= [Date: Fri, 31 Aug 2018 08:47:41 GMT] ,File=[/app/bgacc/caoy/online/server/source/transaction/src/TXN_7105.c],Line=[914]

p= [Server: Apache] ,File=[/app/bgacc/caoy/online/server/source/transaction/src/TXN_7105.c],Line=[914]

p= [X-Powered-By: PHP/5.4.40] ,File=[/app/bgacc/caoy/online/server/source/transaction/src/TXN_7105.c],Line=[914]

p= [Content-Length: 316] ,File=[/app/bgacc/caoy/online/server/source/transaction/src/TXN_7105.c],Line=[914]

p= [Connection: close] ,File=[/app/bgacc/caoy/online/server/source/transaction/src/TXN_7105.c],Line=[914]

p= [Content-Type: text/html] ,File=[/app/bgacc/caoy/online/server/source/transaction/src/TXN_7105.c],Line=[914]

p= [{"resp_code":0,"resp_msg":"get the query result.","resp_detail":{"is_exist":1,"order_state":"0","identifid":"ohM8Fj2uy1HAEOzP7enHYC4L1pMk","print_no":"8388800762","total_fee":"1","pay_channel":"1","pay_time":"2018-08-31 15:52:27","time_stamp":1535705261,"nonce":1535705261,"sign":"3335C56044FC118AED974B0D7AED57F0"}}]

怎样取到最后这个p呢?

char tmpbuff[1024];
memset(tmpbuff, 0x00, sizeof(tmpbuff));
memcpy(tmpbuff, p, strlen(p)); 这样写会报错

支持(0) 反对(0) Dellienware | 园豆:39 (初学一级) | 2018-08-31 16:51

@Dellienware: 是连续的2个换行.你试试

支持(0) 反对(0) 吴瑞祥 | 园豆:28851 (高人七级) | 2018-08-31 18:34

@吴瑞祥: {"resp_code":0,"resp_msg":"get the query result.",

"resp_detail":

{"is_exist":1,"order_state":"0","identifid":"ohM8Fj2uy1HAEOzP7enHYC4L1pMk",

"print_no":"8388800762","total_fee":"1","pay_channel":"1","pay_time":"2018-08-31 15:52:27",

"time_stamp":1535941271,"nonce":1535941271,"sign":"880C6075A153E1E3945AF55ACC74C5C8"}

}

这个order_state是嵌套在JSON数组里 请问cJson怎么解析啊

支持(0) 反对(0) Dellienware | 园豆:39 (初学一级) | 2018-09-03 11:32

@吴瑞祥: strstr("\r\n\r\n",tmpbuff) + 4这个不是得到了json的首字符串的地址吗?
那怎么再获取整个json字符串呢?
memcpy(tmpbuff, strstr("\r\n\r\n",tmpbuff) + 4, ?); 用这个的话 最后的字符串长度不怎么写啊?

支持(0) 反对(0) Dellienware | 园豆:39 (初学一级) | 2018-09-03 16:10

@Dellienware: 尾地址-首地址啊, 在这里就是 tmpbuff + strlen(tmpbuff) - 1 - (strstr("\r\n\r\n",tmpbuff) + 4)

支持(0) 反对(0) boveyyun | 园豆:217 (菜鸟二级) | 2018-09-06 18:20
0

可以参考 curl 的实现代码 https://github.com/curl/curl

dudu | 园豆:38831 (高人七级) | 2018-08-31 16:25
0

strstr(buf, "\r\n\r\n")+4
两个换行是http头结束

窗户 | 园豆:874 (小虾三级) | 2018-09-02 13:25

{"resp_code":0,"resp_msg":"get the query result.",

"resp_detail":

{"is_exist":1,"order_state":"0","identifid":"ohM8Fj2uy1HAEOzP7enHYC4L1pMk",

"print_no":"8388800762","total_fee":"1","pay_channel":"1","pay_time":"2018-08-31 15:52:27",

"time_stamp":1535941271,"nonce":1535941271,"sign":"880C6075A153E1E3945AF55ACC74C5C8"}

}

这个order_state是嵌套在JSON数组里 请问cJson怎么解析啊

支持(0) 反对(0) Dellienware | 园豆:39 (初学一级) | 2018-09-03 11:32

@Dellienware:
我想,这么应该是个很简单的问题。设计一个简单的状态机就搞定了的问题。

支持(0) 反对(0) 窗户 | 园豆:874 (小虾三级) | 2018-09-03 14:22

strstr("\r\n\r\n",tmpbuff) + 4这个不是得到了json的首字符串的地址吗?
那怎么再获取整个json字符串呢?
memcpy(tmpbuff, strstr("\r\n\r\n",tmpbuff) + 4, ?); 用这个的话 最后的字符串长度不怎么写啊?

支持(0) 反对(0) Dellienware | 园豆:39 (初学一级) | 2018-09-03 16:10
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册