在C++中请求HTTP接口:
char* out = cJSON_PrintUnformatted(root);
setlocale(LC_ALL, "C");
time_t rawtime;
time(&rawtime);
char szbuff[128];
strftime(szbuff, 128, "%a, %d %b %Y %H:%M:%S GMT", gmtime(&rawtime)); //Exp: Wed, 21 Sep 2016 10:27:55 GMT
sprintf(head, "Content-Type:application/json;charset=UTF-8\n\
x-gw-api:kk.system.auth.trustauthorize\nx-gw-api-ver:1.0\nDate:%s\nAuthorization:bearer %s",
szbuff, token);
setlocale(LC_ALL, NULL);
sprintf((char*)send_buf, "%s HTTP/1.1\r\nHost:%s\r\n%s\r\nContent-Length:%d\n\n%s",
HG_AUTH_LOGIN_CMD_EX, HG_AUTH_LOGIN_IP, head, strlen(out), out);
cJSON_Delete(root);
char recv_info[2048] = { 0 };
if(request_data(addr, port, send_buf, recv_info))
这样写就有可能导致HTTP那边接收到的数据是
注意看, UTF-8 后面并没有换行,直接跟着x-gw-api:kk.system…….
所以这就导致接收端出现问题。
解决办法:
C++代码使用字符串时,不用“\” ,把字符串代码写在同一行