C语言实现HTTP的解析处理
1、
#include "httpclient.h"
#include "am_openat_httpclient.h"
#define HEAD_ACCEPT_KEY "Accept"
#define HEAD_ACCEPT_VALUE "*/*"
#define HEAD_ACCEPT_L_KEY "Accept-Language"
#define HEAD_ACCEPT_L_VALUE "cn"
#define HEAD_USER_KEY "User-Agent"
#define HEAD_USER_VALUE "*Mozilla/4.0"
#define HEAD_CONNECTION_KEY "Connection"
#define HEAD_CONNECTION_VALUE "Keep-Alive"
int main(int argc, char const *argv[])
{
HTTP_SESSION_HANDLE pHTTP; CHAR readBuff[1460]; UINT32 readSize = 0; UINT32 readTotalLen = 0; CHAR token[32]; UINT32 tokenSize=32; UINT32 nRetCode; CHAR http_url[256] = {0}; pHTTP = HTTPClientOpenRequest(0); HTTPClientSetDebugHook(pHTTP, http_loc_debug); if (HTTPClientSetVerb(pHTTP,VerbGet) != HTTP_CLIENT_SUCCESS) { iot_debug_print("[gsmloc] HTTPClientSetVerb error"); return; } if((nRetCode = HTTPClientAddRequestHeaders(pHTTP, HEAD_ACCEPT_KEY, HEAD_ACCEPT_VALUE, TRUE)) != HTTP_CLIENT_SUCCESS) { return; } if((nRetCode = HTTPClientAddRequestHeaders(pHTTP, HEAD_ACCEPT_L_KEY, HEAD_ACCEPT_L_VALUE, TRUE)) != HTTP_CLIENT_SUCCESS) { return; } if((nRetCode = HTTPClientAddRequestHeaders(pHTTP, HEAD_USER_KEY, HEAD_USER_VALUE, TRUE)) != HTTP_CLIENT_SUCCESS) { return; } if((nRetCode = HTTPClientAddRequestHeaders(pHTTP, HEAD_CONNECTION_KEY, HEAD_CONNECTION_VALUE, TRUE)) != HTTP_CLIENT_SUCCESS) { return; } get_gsmlocinfo(http_url); iot_debug_print("[gsmloc] HTTPClientSendRequest enter"); if (HTTPClientSendRequest(pHTTP,http_url, NULL, 0,TRUE,0,0) != HTTP_CLIENT_SUCCESS ) { iot_debug_print("[gsmloc] HTTPClientSendRequest error"); return; } iot_debug_print("[gsmloc] HTTPClientRecvResponse enter"); if(HTTPClientRecvResponse(pHTTP,20000) != HTTP_CLIENT_SUCCESS) { iot_debug_print("[gsmloc] HTTPClientRecvResponse error"); return; } if((nRetCode = HTTPClientFindFirstHeader(pHTTP, "content-length", token, &tokenSize)) != HTTP_CLIENT_SUCCESS) { iot_debug_print("[gsmloc] HTTPClientFindFirstHeader error"); return; } else { iot_debug_print("[gsmloc] HTTPClientFindFirstHeader %d,%s", tokenSize, token); } HTTPClientFindCloseHeader(pHTTP); while(nRetCode == HTTP_CLIENT_SUCCESS || nRetCode != HTTP_CLIENT_EOS) { // Set the size of our buffer // Get the data nRetCode = HTTPClientReadData(pHTTP,readBuff,sizeof(readBuff),300,&readSize); readTotalLen += readSize; if(nRetCode != HTTP_CLIENT_SUCCESS || nRetCode == HTTP_CLIENT_EOS) { iot_debug_print("[gsmloc] HTTPClientReadData end nRetCode %d", nRetCode); process_gsmloc(readBuff); break; } iot_debug_print("[gsmloc] HTTPClientReadData readTotalLen %d, %d, nRetCode %d", readTotalLen, readSize, nRetCode); } if(HTTPClientCloseRequest(&pHTTP) != HTTP_CLIENT_SUCCESS) { iot_debug_print("[gsmloc] HTTPIntrnConnectionClose error"); return; }
}
http的C源代码实现下载链接: https://download.csdn.net/download/aba13579/88247782