Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/148.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ C++;具有HTTP请求的跨平台套接字_C++_Sockets_Http_Request_Cross Platform - Fatal编程技术网

C++ C++;具有HTTP请求的跨平台套接字

C++ C++;具有HTTP请求的跨平台套接字,c++,sockets,http,request,cross-platform,C++,Sockets,Http,Request,Cross Platform,目前,我正在使用一个非常简单的代码示例来帮助我处理跨平台套接字 Github: 当我试图发出http GET请求时,问题出现了 当我尝试简单请求时,它会返回正确的值,但当我尝试此复杂请求时: “GET/1.0/stock/aapl/batch?type=quote、news、chart&range=1m&last=10 HTTP/1.0\r\nHost:api.iextrading.com\r\n连接:close\r\n\r\n” 它一无所获 //Main.cpp #include <st

目前,我正在使用一个非常简单的代码示例来帮助我处理跨平台套接字

Github:

当我试图发出http GET请求时,问题出现了 当我尝试简单请求时,它会返回正确的值,但当我尝试此复杂请求时:

“GET/1.0/stock/aapl/batch?type=quote、news、chart&range=1m&last=10 HTTP/1.0\r\nHost:api.iextrading.com\r\n连接:close\r\n\r\n”

它一无所获

//Main.cpp

#include <string>
#include <ActiveSocket.h>

#define SEND(a,b,c,d)          send(a, (const char *)b, (int)c, d)
typedef unsigned char  uint8;

int main(int argc, char **argv)
{
    CActiveSocket socket;       // Instantiate active socket object (defaults to TCP).
    char          time[50];
    memset(&time, 0, 50);
    char host[] = "api.iextrading.com";
    char message[] = "GET /1.0/stock/aapl/batch?types=quote,news,chart&range=1m&last=10 HTTP/1.0\r\nHost: api.iextrading.com\r\nConnection: close\r\n\r\n";


    socket.Initialize();

    if (socket.Open(host, 80))
    {

        if (socket.Send((const uint8 *)message, strlen(message)))
        {
            socket.Receive(4096);//size of the buffer

            socket.Close();
        }
    }

    getchar();
    return 1;
}

我的建议是不要再重新发明轮子。有许多(跨平台)库可以处理HTTP连接和请求,它们能够更好地处理您将得到的响应。是的,学习HTTP可能是一个很好的练习,但HTTP并不像看上去那么简单。使用wireshark来捕获流量。看看包是否真的被发送了。查看Wireshark是否将其识别为http。并注意Wireshark指出的任何解析问题
int32 CSimpleSocket::Send(const uint8 *pBuf, size_t bytesToSend)
{
    SetSocketError(SocketSuccess);
    m_nBytesSent = 0;

    switch(m_nSocketType)
    {
    case CSimpleSocket::SocketTypeTcp:
    {
        if (IsSocketValid())
        {
            if ((bytesToSend > 0) && (pBuf != NULL))
            {
                m_timer.Initialize();
                m_timer.SetStartTime();

                //---------------------------------------------------------
                // Check error condition and attempt to resend if call
                // was interrupted by a signal.
                //---------------------------------------------------------
                do
                {
                    m_nBytesSent = SEND(m_socket, pBuf, bytesToSend, 0);
                    TranslateSocketError();
                } while (GetSocketError() == CSimpleSocket::SocketInterrupted);

                m_timer.SetEndTime();
            }
        }
        break;
    }
...
}


int32 CSimpleSocket::Receive(int32 nMaxBytes, uint8 * pBuffer )
{
    m_nBytesReceived = 0;

    if (IsSocketValid() == false)
    {
        return m_nBytesReceived;
    }

    uint8 * pWorkBuffer = pBuffer;
    if ( pBuffer == NULL )
    {
        if ((m_pBuffer != NULL) && (nMaxBytes != m_nBufferSize))
        {
            delete [] m_pBuffer;
            m_pBuffer = NULL;
        }
        if (m_pBuffer == NULL)
        {
            m_nBufferSize = nMaxBytes;
            m_pBuffer = new uint8[nMaxBytes];
        }
        pWorkBuffer = m_pBuffer;
    }

    SetSocketError(SocketSuccess);

    m_timer.Initialize();
    m_timer.SetStartTime();

    switch (m_nSocketType)
    {
    case CSimpleSocket::SocketTypeTcp:
    {
        do
        {
            m_nBytesReceived = RECV(m_socket, (pWorkBuffer +m_nBytesReceived), nMaxBytes, m_nFlags);
            TranslateSocketError();
        } while ((GetSocketError() == CSimpleSocket::SocketInterrupted));

        break;
    }
...
}