コード例 #1
0
ファイル: HttpClient.cpp プロジェクト: brucetsao/LIB_for_MCU
int HttpClient::startRequest(const IPAddress& aServerAddress, const char* aServerName, uint16_t aServerPort, const char* aURLPath, const char* aHttpMethod, const char* aUserAgent)
{
    tHttpState initialState = iState;
    if ((eIdle != iState) && (eRequestStarted != iState))
    {
        return HTTP_ERROR_API;
    }

    {
        if (!iClient->connect(aServerAddress, aServerPort) > 0)
        {
            return HTTP_ERROR_CONNECTION_FAILED;
        }
    }

    // Now we're connected, send the first part of the request
    int ret = sendInitialHeaders(aServerName, aServerAddress, aServerPort, aURLPath, aHttpMethod, aUserAgent);
    if ((initialState == eIdle) && (HTTP_SUCCESS == ret))
    {
        // This was a simple version of the API, so terminate the headers now
        finishHeaders();
    }
    // else we'll call it in endRequest or in the first call to print, etc.

    return ret;
}
コード例 #2
0
ファイル: HttpClient.cpp プロジェクト: brucetsao/LIB_for_MCU
void HttpClient::endRequest()
{
    if (iState < eRequestSent)
    {
        // We still need to finish off the headers
        finishHeaders();
    }
    // else the end of headers has already been sent, so nothing to do here
}
コード例 #3
0
int HttpClient::startRequest(const char* aServerName, uint16_t aServerPort, const char* aURLPath, const char* aHttpMethod, const char* aUserAgent)
{
    tHttpState initialState = iState;
    if ((eIdle != iState) && (eRequestStarted != iState))
    {
        return HTTP_ERROR_API;
    }

#ifdef PROXY_ENABLED
    if (iProxyPort)
    {
        if (!iClient->connect(iProxyAddress, iProxyPort) > 0)
        {
#ifdef LOGGING
            Serial.println("Proxy connection failed");
#endif
            return HTTP_ERROR_CONNECTION_FAILED;
        }
    }
    else
#endif
    {
        if (!iClient->connect(aServerName, aServerPort) > 0)
        {
#ifdef LOGGING
            Serial.println("Connection failed");
#endif
            return HTTP_ERROR_CONNECTION_FAILED;
        }
    }

    // Now we're connected, send the first part of the request
    int ret = sendInitialHeaders(aServerName, IPAddress(0,0,0,0), aServerPort, aURLPath, aHttpMethod, aUserAgent);
    if ((initialState == eIdle) && (HTTP_SUCCESS == ret))
    {
        // This was a simple version of the API, so terminate the headers now
        finishHeaders();
    }
    // else we'll call it in endRequest or in the first call to print, etc.

    return ret;
}
コード例 #4
0
ファイル: HttpClient.cpp プロジェクト: DawesLab/arduino
int HttpClient::startRequest(const char* aURLPath, const char* aHttpMethod, 
                                const char* aContentType, int aContentLength, const byte aBody[])
{
    if (iState == eReadingBody || iState == eReadingChunkLength || iState == eReadingBodyChunk)
    {
        flushClientRx();

        resetState();
    }

    tHttpState initialState = iState;

    if ((eIdle != iState) && (eRequestStarted != iState))
    {
        return HTTP_ERROR_API;
    }

    if (iConnectionClose || !iClient->connected())
    {
        if (iServerName)
        {
            if (!iClient->connect(iServerName, iServerPort) > 0)
            {
#ifdef LOGGING
                Serial.println("Connection failed");
#endif
                return HTTP_ERROR_CONNECTION_FAILED;
            }
        }
        else
        {
            if (!iClient->connect(iServerAddress, iServerPort) > 0)
            {
#ifdef LOGGING
                Serial.println("Connection failed");
#endif
                return HTTP_ERROR_CONNECTION_FAILED;
            }    
        }
    }
    else
    {
#ifdef LOGGING
        Serial.println("Connection already open");
#endif
    }

    // Now we're connected, send the first part of the request
    int ret = sendInitialHeaders(aURLPath, aHttpMethod);

    if (HTTP_SUCCESS == ret)
    {
        if (aContentType)
        {
            sendHeader(HTTP_HEADER_CONTENT_TYPE, aContentType);
        }

        if (aContentLength > 0)
        {
            sendHeader(HTTP_HEADER_CONTENT_LENGTH, aContentLength);
        }

        bool hasBody = (aBody && aContentLength > 0);

        if (initialState == eIdle || hasBody)
        {
            // This was a simple version of the API, so terminate the headers now
            finishHeaders();
        }
        // else we'll call it in endRequest or in the first call to print, etc.

        if (hasBody)
        {
                write(aBody, aContentLength);
        }
    }

    return ret;
}