Fork HTTPClient and Modfiy code for mbed 6.0
Dependents: mbed-demo-http-get-json
Diff: HTTPClient.cpp
- Revision:
- 50:1c1409029b05
- Parent:
- 49:c5abb7ae070b
- Child:
- 51:a9ed5a6eee90
--- a/HTTPClient.cpp Sun Jul 14 01:42:33 2019 +0000 +++ b/HTTPClient.cpp Sun Nov 08 02:49:01 2020 +0000 @@ -24,9 +24,9 @@ #endif //Debug is disabled by default -//#define DEBUG "HTCL" +#define DEBUG "HTCL" #include <cstdio> -#if (defined(DEBUG) && !defined(TARGET_LPC11U24)) +#if (defined(DEBUG)) #define DBG(x, ...) std::printf("[DBG %s %3d] " x "\r\n", DEBUG, __LINE__, ##__VA_ARGS__); #define WARN(x, ...) std::printf("[WRN %s %3d] " x "\r\n", DEBUG, __LINE__, ##__VA_ARGS__); #define ERR(x, ...) std::printf("[ERR %s %3d] " x "\r\n", DEBUG, __LINE__, ##__VA_ARGS__); @@ -52,12 +52,13 @@ #include "HTTPClient.h" -HTTPClient::HTTPClient() : +//HTTPClient::HTTPClient() : +HTTPClient::HTTPClient(NetworkInterface *interface) : //modify for mbed 6.0 m_sock(), m_basicAuthUser(NULL), m_basicAuthPassword(NULL), m_nCustomHeaders(0), m_httpResponseCode(0), m_maxredirections(1), m_location(NULL) { - + net = interface; } HTTPClient::~HTTPClient() @@ -221,13 +222,21 @@ //Connect DBG("Connecting socket to server"); - ret = m_sock.connect(host, port); + //ret = m_sock.connect(host, port); // modify for mbed 6.0 + m_sock.open(net); + SocketAddress a; + net->gethostbyname(host, &a); + a.set_port(port); + ret = m_sock.connect(a); + // ---------- if (ret < 0) { m_sock.close(); ERR("Could not connect"); return HTTP_CONN; } - + else { + printf("Connect server %s:%d\n",host,port); + } // Send request DBG("Sending request"); const char* meth = (method==HTTP_GET)?"GET":(method==HTTP_POST)?"POST":(method==HTTP_PUT)?"PUT":(method==HTTP_DELETE)?"DELETE":""; @@ -264,7 +273,8 @@ ret = send(buf); if (ret) { ERR("closing"); - wait_ms(50); + //wait_ms(50); + ThisThread::sleep_for(50);// modify for mbed 6.0 m_sock.close(); ERR("Could not write request"); return HTTP_CONN; @@ -276,7 +286,7 @@ DBG("Sending headers"); if( pDataOut != NULL ) { if( pDataOut->getIsChunked() ) { - ret = send("Transfer-Encoding: chunked\r\n"); + ret = send((char *)"Transfer-Encoding: chunked\r\n"); CHECK_CONN_ERR(ret); } else { snprintf(buf, sizeof(buf), "Content-Length: %d\r\n", pDataOut->getDataLen()); @@ -293,7 +303,7 @@ //Close headers DBG("Headers sent"); - ret = send("\r\n"); + ret = send((char *)"\r\n"); CHECK_CONN_ERR(ret); //Send data (if available) @@ -318,7 +328,7 @@ } if( pDataOut->getIsChunked() ) { - ret = send("\r\n"); //Chunk-terminating CRLF + ret = send((char *)"\r\n"); //Chunk-terminating CRLF CHECK_CONN_ERR(ret); } else { writtenLen += trfLen; @@ -546,7 +556,8 @@ DBG("Trying to read between %d and %d bytes", minLen, maxLen); size_t readLen = 0; - if (!m_sock.is_connected()) { + //if (!m_sock.is_connected()) { + if (net->get_connection_status() != 1) { // modify for mbed 6.0 WARN("Connection was closed by server"); return HTTP_CLOSED; //Connection was closed by server } @@ -556,13 +567,15 @@ if (readLen < minLen) { DBG("Trying to read at most %4d bytes [not Blocking, %d] %d,%d", minLen - readLen, m_timeout, minLen, readLen); - m_sock.set_blocking(false, m_timeout); - ret = m_sock.receive_all(buf + readLen, minLen - readLen); + //m_sock.set_blocking(false, m_timeout); + //ret = m_sock.receive_all(buf + readLen, minLen - readLen); + ret = m_sock.recv(buf + readLen, minLen - readLen);// modify for mbed 6.0 } else { DBG("Trying to read at most %4d bytes [Not blocking, %d] %d,%d", maxLen - readLen, 0, maxLen, readLen); - m_sock.set_blocking(false, 0); - ret = m_sock.receive(buf + readLen, maxLen - readLen); + //m_sock.set_blocking(false, 0); + //ret = m_sock.receive(buf + readLen, maxLen - readLen); + ret = m_sock.recv(buf + readLen, maxLen - readLen);// modify for mbed 6.0 } if (ret > 0) { @@ -570,7 +583,8 @@ } else if ( ret == 0 ) { break; } else { - if (!m_sock.is_connected()) { + //if (!m_sock.is_connected()) { + if (net->get_connection_status() != 1) { // modify for mbed 6.0 ERR("Connection error (recv returned %d)", ret); *pReadLen = readLen; return HTTP_CONN; @@ -578,7 +592,8 @@ break; } } - if (!m_sock.is_connected()) { + //if (!m_sock.is_connected()) { + if (net->get_connection_status() != 1) { // modify for mbed 6.0 break; } } @@ -596,14 +611,16 @@ DBG("send(\r\n%s,%d)", buf, len); size_t writtenLen = 0; - if(!m_sock.is_connected()) { + //if(!m_sock.is_connected()) { + if (net->get_connection_status() != 1) { // modify for mbed 6.0 WARN("Connection was closed by server"); return HTTP_CLOSED; //Connection was closed by server } - DBG("a"); - m_sock.set_blocking(false, m_timeout); + //DBG("a"); + //m_sock.set_blocking(false, m_timeout); DBG("b"); - int ret = m_sock.send_all(buf, len); + //int ret = m_sock.send_all(buf, len); + int ret = m_sock.send(buf, len);// modify for mbed 6.0 if(ret > 0) { writtenLen += ret; } else if( ret == 0 ) {