To access a web service using SAME70Xplained

Go To Last Post
6 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi 

 

I am working on a project to access data from a web service using  SAME70XPlained board. I am building my code using THIRDPARTY LWIP RAW BASIC HTTP EXAMPLE to set up static IP ethernet connection. That I have successfully received. For the next step to access a web service,I researched on internet about GET and POST method requests that can retrieve and post the data on a web server.

 

In my code I want to access this currency converter web service(http://currencyconverter.kowabunga.net/converter.asmx/GetCurrencyRate?Currency=CAD&RateDate=2019-02-25). So that I can pass values to its parameters like Currency=CAD and Rate Date=2019-02-05 and can get my output value as following on my web page:-

 

This XML file does not appear to have any style information associated with it. The document tree is shown below.

<decimal xmlns="http://tempuri.org/">1.4924</decimal>

 

Following are the doubts that I have:-

 

1. How can I access to another web service if my Atmel board is acting like a web server?

2. How to use POST method request in my code?

3. Is the httpserver code enough or Is there a different httpclient code that I need to implement?

 

I would really appreciate any help.

 

Last Edited: Fri. Mar 8, 2019 - 08:17 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

You need a webclient! You can do this by using a plain tcp socket. What the httpclient code does is append/remove the http headers from the raw socket data. For the most part the header data is fixed, so you can create a string with the required data. How to do this? Http is well documented, so google. Or you can use tools like wireshark to look at raw data and curl to generate the request.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thanks for your reply.

I followed your suggestions but I am still not clear on how can I post data to a web server and then get a reply back.

 

1. as Http client needs connect function to attach a socket with server's IP address. Now my question is:who is my server? Atmel board's static IP address or currencyconverter.kowabunga.net ?

2. Is the following code correct for the http initialization?

 

void httpd_init(void)
{
  
    struct tcp_pcb *client_pcb;
    struct ip_addr remoteIP;
    IP4_ADDR(&remoteIP, ETHERNET_CONF_REMOTE_IPADDR0, ETHERNET_CONF_REMOTE_IPADDR1,
    ETHERNET_CONF_REMOTE_IPADDR2, ETHERNET_CONF_REMOTE_IPADDR3);

    client_pcb= tcp_new();

    tcp_connect(client_pcb,&remoteIP,80,http_request);

}

 

3. Also please see if the http request function is missing information.

 

static err_t http_request(void *arg, struct tcp_pcb *pcb, struct pbuf *p, err_t err)
{
    char *string="POST /converter.asmx/GetCurrencyRate HTTP/1.1\r\nHost: currencyconverter.kowabunga.net\r\nContent-Type: application/x-www-form-urlencoded\r\nContent-Length: 32\r\n\r\nCurrency=CAD&RateDate=2019-02-25\r\n\r\n";
    
    tcp_write(pcb, string, strlen(string), TCP_WRITE_FLAG_COPY);
     /* now send */
    tcp_output(pcb);
    tcp_recv(pcb, http_recv);

    return ERR_OK;  
}

 

 

 

If you can provide me reference to an example that is posting data to server and displaying results on a web browser that would be great. As I have spent weeks to search a post example code using atmel board and I couldn't find something relevant.

 

Thanks in advance.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

1. the server ip is www.currencyconverter.kowabunga.net

2. I have no idea. 

 

Apart from the tcp socket stuff, the rest is not specific to Atmel. I'd suggest you test your methods using python or node.js. Once you'ce got the finer points of talking to a webserver under control, then you can transfer this knowlege to the Atmel board.

 

Tools such as curl and wireshark are invaluable.

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thanks for the reply.

I am still not able to set up connection with server  www.currencyconverter.kowabunga.net .

As TCP connect function tcp_connect(pcb, &resolvedIP, 80, hc_connected) requires a resolved IP address of web server. 

I want to use DNS server that can resolve IP address from a host name.

 

   dns_gethostbyname("www.currencyconverter.kowabunga.net", &resolvedIP, NULL, NULL);

 

When I use above function to get a IP address from DNS server it returns the Atmel board static IP address. 

 

The DNS server address that I am using is the one that I got from typing ipconfig /all command in command prompt of my pc

#define DNS_SERVER_ADDRESS(ipaddr)        (ip4_addr_set_u32(ipaddr, ipaddr_addr("10.28.16.5"))) /* windows DNS */

 

The default DNS server address that was given in atmel code was the following:

 

#define DNS_SERVER_ADDRESS(ipaddr)        (ip4_addr_set_u32(ipaddr, ipaddr_addr("208.67.222.222"))) /* resolver1.opendns.com */

 

What DNS server address should I use?

Please help if you have information on how to resolve IP address from a host name using DNS.

 

 

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I resolved the IP Address from a host name using DNS server 8.8.8.8 address.

It is successfully returning IP address of any host name entered.

 

Thankssmiley