TI工程师
您好。
我使用httpget例程进行改造。我想从我的云服务器上获取数据。请问按照下面的例程是否可以达到效果?
还有HTTPClient_connect函数得不到任何返回值。是哪里设置的不对吗?
※我对网址进行了简化处理,请见谅。云服务器地址在我自己电脑可以访问的。
#define HOSTNAME "https://private.test-api.ap-3.amazonaws.com/test" #define REQUEST_URI "/0/dev/macaddr" void* httpTask(void* pvParameters) { bool moreDataFlag = false; char data[HTTP_MIN_RECV]; int16_t ret = 0; int16_t len = 0; memset(data,0,512); ret = -1; moreDataFlag = 1; Display_printf(display, 0, 0, "Sending a HTTP GET request to '%s'\n", HOSTNAME); HTTPClient_Handle httpClientHandle; int16_t statusCode; httpClientHandle = HTTPClient_create(&statusCode,0); if(statusCode < 0) { printError("httpTask: creation of http client handle failed", statusCode); } ret = HTTPClient_setHeaderByName(httpClientHandle, HTTPClient_HFIELD_REQ_USER_AGENT, "x-api-key","test",strlen("test")+1, HTTPClient_HFIELD_NOT_PERSISTENT); if(ret < 0) { printError("httpTask: setting request header failed", ret); } ret = HTTPClient_connect(httpClientHandle,HOSTNAME,0,0); if(ret < 0) { printError("httpTask: connect failed", ret); } ret = HTTPClient_sendRequest(httpClientHandle,HTTP_METHOD_GET,REQUEST_URI, NULL,0, 0); if(ret < 0) { printError("httpTask: send failed", ret); } if(ret != HTTP_SC_OK) { printError("httpTask: cannot get status", ret); } Display_printf(display, 0, 0, "HTTP Response Status Code: %d\n", ret); len = 0; do { ret = HTTPClient_readResponseBody(httpClientHandle, data, sizeof(data), &moreDataFlag); if(ret < 0) { printError("httpTask: response body processing failed", ret); } Display_printf(display, 0, 0, "%.*s \r\n",ret,data); len += ret; } while(moreDataFlag); Display_printf(display, 0, 0, "Received %d bytes of payload\n", len); ret = HTTPClient_disconnect(httpClientHandle); if(ret < 0) { printError("httpTask: disconnect failed", ret); } HTTPClient_destroy(httpClientHandle); return(0); }