components/webclient/src/webclient.c
int webclient_request(const char *URI, const char *header, const char *post_data, unsigned char **response)
{
    struct webclient_session *session;
    int rc = WEBCLIENT_OK;
    int totle_length;

    if (!URI)
    {
        return WEBCLIENT_ERROR;
    }

    if (post_data == NULL && response == NULL)
    {
        // "request get failed, get response data cannot be empty."
        return -WEBCLIENT_ERROR;
    }

    if (post_data == NULL)
    {
        session = webclient_session_create(WEBCLIENT_HEADER_BUFSZ);
        if (session == NULL)
        {
            rc = -WEBCLIENT_NOMEM;
            goto __exit;
        }

        if (header != NULL)
        {
            strncpy(session->header->buffer, header, strlen(header));
        }

        if (webclient_get(session, URI) != 200)
        {
            rc = -WEBCLIENT_ERROR;
            goto __exit;
        }

        totle_length = webclient_response(session, response);
        if (totle_length <= 0)
        {
            rc = -WEBCLIENT_ERROR;
            goto __exit;
        }
    }
    else
    {
        session = webclient_session_create(WEBCLIENT_HEADER_BUFSZ);
        if (session == NULL)
        {
            rc = -WEBCLIENT_NOMEM;
            goto __exit;
        }

        if (header != NULL)
        {
            strncpy(session->header->buffer, header, strlen(header));
        }
        else
        {
            /* build header for upload */
            webclient_header_fields_add(session, "Content-Length: %d\r\n", strlen(post_data));
            webclient_header_fields_add(session, "Content-Type: application/octet-stream\r\n");
        }

        if (webclient_post(session, URI, post_data) != 200)
        {
            rc = -WEBCLIENT_ERROR;
            goto __exit;
        }

        totle_length = webclient_response(session, response);
        if (totle_length <= 0)
        {
            rc = -WEBCLIENT_ERROR;
            goto __exit;
        }
    }

    if (header != NULL)
    {
        strncpy(session->header->buffer, header, strlen(header));
    }

__exit:
    if (session)
    {
        webclient_close(session);
        session = NULL;
    }

    if (rc < 0)
    {
        return rc;
    }

    return totle_length;
}
/**
 *  send request(GET/POST) to server and get response data.
 *
 * @param URI input server address
 * @param header send header data
 *             = NULL: use default header data, must be GET request
 *            != NULL: user custom header data, GET or POST request
 * @param post_data data sent to the server
 *             = NULL: it is GET request
 *            != NULL: it is POST request
 * @param response response buffer address
 *
 * @return <0: request failed
 *        >=0: response buffer size
 */
send request(GET/POST) to server and get response data.
@param URI input server address
@param header send header data
= NULL: use default header data, must be GET request
!= NULL: user custom header data, GET or POST request
@param post_data data sent to the server
= NULL: it is GET request
!= NULL: it is POST request
@param response response buffer address

@return <0: request failed
>=0: response buffer size
send request ( GET / POST ) to server and get response data . @param URI input server address @param header send header data = NULL : use default header data must be GET request ! = NULL : user custom header data GET or POST request @param post_data data sent to the server = NULL : it is GET request ! = NULL : it is POST request @param response response buffer address @return <0 : request failed > = 0 : response buffer size
int webclient_request(const char *URI, const char *header, const char *post_data, unsigned char **response)
{
    struct webclient_session *session;
    int rc = WEBCLIENT_OK;
    int totle_length;
    if (!URI)
    {
        return WEBCLIENT_ERROR;
    }
    if (post_data == NULL && response == NULL)
    {
        return -WEBCLIENT_ERROR;
    }
    if (post_data == NULL)
    {
        session = webclient_session_create(WEBCLIENT_HEADER_BUFSZ);
        if (session == NULL)
        {
            rc = -WEBCLIENT_NOMEM;
            goto __exit;
        }
        if (header != NULL)
        {
            strncpy(session->header->buffer, header, strlen(header));
        }
        if (webclient_get(session, URI) != 200)
        {
            rc = -WEBCLIENT_ERROR;
            goto __exit;
        }
        totle_length = webclient_response(session, response);
        if (totle_length <= 0)
        {
            rc = -WEBCLIENT_ERROR;
            goto __exit;
        }
    }
    else
    {
        session = webclient_session_create(WEBCLIENT_HEADER_BUFSZ);
        if (session == NULL)
        {
            rc = -WEBCLIENT_NOMEM;
            goto __exit;
        }
        if (header != NULL)
        {
            strncpy(session->header->buffer, header, strlen(header));
        }
        else
        {
            webclient_header_fields_add(session, "Content-Length: %d\r\n", strlen(post_data));
            webclient_header_fields_add(session, "Content-Type: application/octet-stream\r\n");
        }
        if (webclient_post(session, URI, post_data) != 200)
        {
            rc = -WEBCLIENT_ERROR;
            goto __exit;
        }
        totle_length = webclient_response(session, response);
        if (totle_length <= 0)
        {
            rc = -WEBCLIENT_ERROR;
            goto __exit;
        }
    }
    if (header != NULL)
    {
        strncpy(session->header->buffer, header, strlen(header));
    }
__exit:
    if (session)
    {
        webclient_close(session);
        session = NULL;
    }
    if (rc < 0)
    {
        return rc;
    }
    return totle_length;
}
int webclient_request ( const char * URI , const char * header , const char * post_data , unsigned char * * response ) { struct webclient_session * session ; int rc = WEBCLIENT_OK ; int totle_length ; if ( ! URI ) { return WEBCLIENT_ERROR ; } if ( post_data == NULL && response == NULL ) { return - WEBCLIENT_ERROR ; } if ( post_data == NULL ) { session = webclient_session_create ( WEBCLIENT_HEADER_BUFSZ ) ; if ( session == NULL ) { rc = - WEBCLIENT_NOMEM ; goto __exit ; } if ( header != NULL ) { strncpy ( session -> header -> buffer , header , strlen ( header ) ) ; } if ( webclient_get ( session , URI ) != 200 ) { rc = - WEBCLIENT_ERROR ; goto __exit ; } totle_length = webclient_response ( session , response ) ; if ( totle_length <= 0 ) { rc = - WEBCLIENT_ERROR ; goto __exit ; } } else { session = webclient_session_create ( WEBCLIENT_HEADER_BUFSZ ) ; if ( session == NULL ) { rc = - WEBCLIENT_NOMEM ; goto __exit ; } if ( header != NULL ) { strncpy ( session -> header -> buffer , header , strlen ( header ) ) ; } else { webclient_header_fields_add ( session , " \r \n " , strlen ( post_data ) ) ; webclient_header_fields_add ( session , " \r \n " ) ; } if ( webclient_post ( session , URI , post_data ) != 200 ) { rc = - WEBCLIENT_ERROR ; goto __exit ; } totle_length = webclient_response ( session , response ) ; if ( totle_length <= 0 ) { rc = - WEBCLIENT_ERROR ; goto __exit ; } } if ( header != NULL ) { strncpy ( session -> header -> buffer , header , strlen ( header ) ) ; } __exit : if ( session ) { webclient_close ( session ) ; session = NULL ; } if ( rc < 0 ) { return rc ; } return totle_length ; }
send request(GET/POST) to server and get response data.
send request ( GET / POST ) to server and get response data .
// "request get failed, get response data cannot be empty." /* build header for upload */