This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

HTTP Client

I wonder if there is a way to consume a webservice with RL-TCPNet and LPC2478.
The RL-TCPNet examples only http server, not the client.
Can anyone help?

  • You have to create a HTTP request.
    Connect to the web server.
    Send the request.
    Retrieve an answer and check the HTTP response.
    Figure out if you need to receive a fixed number of bytes or receive until disconnect.
    Read your "body" data.
    Close connection.

    There should be a number of examples to be found with Google how to send an HTTP request and how to parse the response. It isn't related to what TCP/IP stack that is used.

  • Right.
    Implementing the client, tcp_connect() returns true. But tcp_get_state() always returns TCP_STATE_SYN_SENT.
    After a few seconds is generated TCP_EVT_ABORT event in tcp_callback().

    My code:

    U8 httpc_soc;
    
    U16 httpc_tcp_callback(U8 soc, U8 event, U8 *ptr, U16 par) {
      switch (event) {
        case TCP_EVT_CONREQ:
          return 1;
        case TCP_EVT_ABORT:
          log_print("httpc: abort %03d.%03d.%03d.%03d:%d\n",
                    ptr[0], ptr[1], ptr[2], ptr[3], par);
          tcp_close(soc);
          return 1;
        case TCP_EVT_CONNECT:
          log_print("httpc: connected %03d.%03d.%03d.%03d:%d\n",
                    ptr[0], ptr[1], ptr[2], ptr[3], par);
          return 1;
        case TCP_EVT_CLOSE:
          log_print("httpc: close %03d.%03d.%03d.%03d:%d\n",
                    ptr[0], ptr[1], ptr[2], ptr[3], par);
          return 1;
        case TCP_EVT_ACK:
          return 1;
        case TCP_EVT_DATA:
          log_print("httpc: rcv message (%d bytes)\n", par);
          log_print("httpc: %s\n", ptr);
          return par;
      }
      return 0;
    }
    
    int httpc_send(U8 *server_addr, const char *message) {
      int len = strlen(message);
      U8 *buf = tcp_get_buf((len+4) | 0x8000);
      U8 state, timeout = 0;
      if (!httpc_soc) {
        httpc_soc = tcp_get_socket(TCP_TYPE_CLIENT, 0, 30, httpc_tcp_callback);
        if (!httpc_soc)
          return 0;
      }
      state = tcp_get_state(httpc_soc);
      if (state == TCP_STATE_FREE || state == TCP_STATE_CLOSED) {
        if (tcp_connect(httpc_soc, server_addr, 80, 1000) == __TRUE)
          log_print("httpc: =connect\n");
      }
      state = tcp_get_state(httpc_soc);
      if (state == TCP_STATE_CONNECT) {
        while (tcp_check_send(httpc_soc) != __TRUE && timeout < 10) {
          timeout++;
          os_dly_wait(50);
        }
        if (tcp_check_send(httpc_soc) == __TRUE) {
          if (buf != NULL) {
            mem_copy(buf, (void*)message, len);
            mem_copy(&buf[len-4], "\r\n\r\n", 4);
            if (tcp_send(httpc_soc, buf, len) == __TRUE)
              return 1;
          }
          log_print("httpc: =null\n");
        }
        log_print("httpc: !send\n");
      }
      log_print("httpc: state %d\n", state);
      return 0;
    }
    
    __task void httpc_task(void) {
      U8 webserver[4] = {192,168,1,103};
    
      init_eth(); // eth task && init_TcpNet
    
      while (1) {
        if (httpc_send(webserver, "GET /myserver/index.php?method=GET&rs=datalog HTTP1.1"))
          log_print("httpc: sent message\n");
        os_dly_wait(10000);
      }
    }
    

    Output log:

    httpc: =connect
    httpc: state 4
    httpc: state 4
    httpc: state 4
    httpc: abort 192.168.1.103:80
    httpc: =connect
    httpc: state 4
    httpc: state 4
    httpc: state 4
    httpc: abort 192.168.1.103:80