无法向Twitter API发送GET请求,但可以发送POST请求吗?

无法向Twitter API发送GET请求,但可以发送POST请求吗?,twitter,get,httprequest,mbed,mbedtls,Twitter,Get,Httprequest,Mbed,Mbedtls,我们只使用应用程序身份验证并获取访问令牌。然后,我们通过执行以下操作向特定用户的时间线发出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 thou

我们只使用应用程序身份验证并获取访问令牌。然后,我们通过执行以下操作向特定用户的时间线发出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类型的请求比较麻烦。我非常感谢您的帮助

代码
\u bpos=snprintf(\u buffer,sizeof(\u buffer)-1,“GET/1.1/statuses/user\u timeline.json?user\u id=dog\u rates&count=1 HTTP/1.1\n”//这是一个HTTP请求,即使发送了,也不会得到任何回复
“主机:api.twitter.com\n”
“授权:承载%s”,authToken);
mbedtls_printf(“缓冲区内容:\n%s\n”,_Buffer)//打印缓冲区的内容以确保HTTP请求正确
/*发送RESTAPI GET请求*/
ret=mbedtls_ssl_write(&_ssl,(const unsigned char*)_buffer,_bpos)//将数据写入套接字
mbedtls_printf(“你将返回的距离:%d\r\n”,返回)//打印写入的返回值,正值为写入的字节数(应等于_bpos),负值为失败
if(ret<0)//检查写入是否失败
{
if(ret!=MBEDTLS\u ERR\u SSL\u WANT\u READ&&ret!=MBEDTLS\u ERR\u SSL\u WANT\u WRITE)//如果写入失败且未返回读/写错误,则TLS连接断开,程序打印错误
{
打印mbedtls错误(“mbedtls ssl写入”,ret);
误差(_tcpsocket,-1);
}
返回-1;
}
/*从套接字中读取数据*/
usedspace=0;
bufptr=(无符号字符*)\u缓冲区//这一行和上面的一行用于将多个读取连接到一个缓冲区中,但是第二个读取暂时被注释掉。
ret=mbedtls_ssl_read(&_ssl,bufptr,静态_cast(RECV_BUFFER_SIZE-usedspace))//从套接字读入缓冲区
mbedtls_printf(“你将返回的距离:%d\r\n”,返回)//打印读取的返回值,处理方式与写入返回值相同
if(ret<0)//与write执行的检查相同
{
if(ret!=MBEDTLS\u ERR\u SSL\u WANT\u READ&&ret!=MBEDTLS\u ERR\u SSL\u WANT\u WRITE)
{
打印mbedtls错误(“mbedtls\U ssl\U读取”,ret);
误差(_tcpsocket,-1);
}
删除[]buf;
返回-1;
}
mbedtls_printf(“读取完成\r\n”)//占位符文本,以查看您是否达到了这一步
usedspace=usedspace+ret;
bufptr=bufptr+ret;

您的HTTP请求无效。GET请求中最后一个标头之后缺少换行符,并且您正在使用
\n
分隔标头行,而不是使用
\r\n
(同样)

我建议你使用一个库来构建你的请求

_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;