0

我们使用仅限应用程序的身份验证并取回访问令牌。然后,我们通过执行以下操作向特定用户的时间线发出 GET 请求:

_bpos = snprintf(_buffer, sizeof(_buffer)-1, "GET /1.1/statuses/user_timeline.json?user_id=dog_rates&count=1 HTTP/1.1\n" //This is the HTTP request that gets no reply at all, even though it does send
                                                 "Host: api.twitter.com\n"
                                                 "Authorization: Bearer %s",authToken);
    

但不知何故,这有效:

_bpos = snprintf(_buffer, sizeof(_buffer) - 1, "POST /oauth2/token HTTP/1.1\n"  //I used this to test to make sure the following read/write works, it did for me
                                                      "Host: api.twitter.com\n"
                                                      "Authorization: Basic 12312312312345678901234567890\n"
                                                      "Content-Type: application/x-www-form-urlencoded;charset=UTF-8\n"
                                                      "Content-Length: 29\n\n"
                                                      "grant_type=client_credentials");

所以似乎只有 GET 类型的请求很麻烦。我将不胜感激任何帮助!

代码

_bpos = snprintf(_buffer, sizeof(_buffer)-1, "GET /1.1/statuses/user_timeline.json?user_id=dog_rates&count=1 HTTP/1.1\n" //This is the HTTP request that gets no reply at all, even though it does send
                                                 "Host: api.twitter.com\n"
                                                 "Authorization: Bearer %s",authToken);

    mbedtls_printf("Buffer content:\n%s\n",_buffer);   //prints the content of the buffer to ensure the HTTP request is right

    /* Sending REST API GET request */
    ret = mbedtls_ssl_write(&_ssl, (const unsigned char *) _buffer, _bpos); //writes the data to the socket
    mbedtls_printf("how far u gonna ret: %d\r\n",ret); //prints the return value of the write, positive is number of bytes written (should be equal to _bpos), negative is a failure
    if (ret < 0) //Checks to see if write failed
    {
        if (ret != MBEDTLS_ERR_SSL_WANT_READ && ret != MBEDTLS_ERR_SSL_WANT_WRITE) //If write failed and didnt give back a read/write error, the TLS connection is broken, and the program prints an error
        {
            print_mbedtls_error("mbedtls_ssl_write", ret);
            onError(_tcpsocket, -1 );
        }
        return -1;
    }


     /* Read data out of the socket */
    usedspace = 0;
    bufptr = (unsigned char *) _buffer;//this and the above line are used to concatenate multiple reads into one buffer, however second read is commented out for now.

    ret = mbedtls_ssl_read(&_ssl, bufptr, static_cast<size_t>(RECV_BUFFER_SIZE-usedspace)); //read from socket into buffer
    mbedtls_printf("how far u gonna ret: %d\r\n",ret);//prints return value of read, same deal as write return value
    if(ret < 0)//same check that write does
    {
        if (ret != MBEDTLS_ERR_SSL_WANT_READ && ret != MBEDTLS_ERR_SSL_WANT_WRITE)
        {
            print_mbedtls_error("mbedtls_ssl_read", ret);
            onError(_tcpsocket, -1 );
        }
        delete[] buf;
        return -1;
    }
    mbedtls_printf("read finished\r\n");//placeholder text to see if you get this far

    usedspace = usedspace+ret;

    bufptr = bufptr + ret;
4

1 回答 1

0

您的 HTTP 请求无效。您在 GET 请求中的最后一个标头之后缺少换行符,并且您将标头行与 分开\n,而不是与\r\n(另见此)。

我建议您使用像mbed-http这样的库来为您构建请求。

于 2017-05-19T07:10:16.420 回答