TI E2E 英文论坛海量技术问答的中文版全新上线,可点击相关论坛查看,或在站内搜索 “参考译文” 获取。

This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

CC3235MODS: httpget 如何从云服务器中获取数据?

Part Number: CC3235MODS

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);
}