make http post

I have got a Ethernet shield and trying to make http post. At the moment I would want to make the call every 10 seconds. Here is the function I have created is it along the right lines? Does a virtual Arduino exist so I could test the code and debug before downloading?

I have used the webclient example, on the arduino website.

void NetworkPost()
{
/*  http://192.168.0.34:81/tenHsServer/tenHsServer.aspx?t=ab&f=DeviceStatus&d=E3 */

    client.println("POST /tenHsServer/tenHsServer.aspx?t=ab&f=DeviceStatus&d=E3");
    client.println("http://192.168.0.34:81");
    client.println("Connection: close");
    client.println(); 
}

POST request wouldn't have any arguments on it. That's how you do a GET.

so it would be

POST /tenHsServer/tenHsServer.aspx HTTP/1.1
Host: 192.168.0.34:81
t=ab&f=DeviceStatus&d=E3

Of course this is assuming that the host isn't a virtual host. If you're trying to contact a virtual host, don't use the ip, use the domain.

I have my function as such and can see it connect and disconnect

void sendPOST() //client function to send/receive GET request data.
{
 
  if (client.connect(myserver, 81)) {  //starts client connection, checks for connection
    Serial.println("connected");
    client.println("POST 192.168.0.34:81/tenHsServer/tenHsServer.aspx?t=ab&f=ToggleDevice&d=E3 HTTP/1.0");
    Serial.println();
    client.println(); //end of get request
  }
  else {
    Serial.println("connection failed"); //error message if no client connect
    Serial.println();
  }
  Serial.println();
  Serial.println("disconnecting.");
  Serial.println("==================");
  Serial.println();
  client.stop(); //stop client

}

I have tried a few permutations with the ip address and the string, but my server doesn’t respond.

If I open a browser and past in the url and paramters the server responds great. Is it possible to do the same thing in this function, ie drop the post and http/1.0. So it behaves as if I hd pasted the URI in a browser?

thanks

Again.

client.println("POST 192.168.0.34:81/tenHsServer/tenHsServer.aspx?t=ab&f=ToggleDevice&d=E3 HTTP/1.0");

That is a GET request, not a POST request.

I have changed the post to a get added removed the http/1.1 or 1.0. Put the whole url in the string ie 192.168.0.34:81/

nothing works, I know that the server side is good I can paste my string into ie or firefox and it works great and I pass it in my custom webpage using Jquery and it works.

the script I am trying to speak to is a third party .net script ans is about 8 years old so no ideas how it internally works.

is there anyway to make the arduino and the ethernet shield behave as if the string is pasted into a url? no posts/Gets etc?

thanks, for any ideas.

Basic client GET test code:

//zoomkat 9-22-12
//simple client test
//for use with IDE 1.0.1
//with DNS, DHCP, and Host
//open serial monitor and send an e to test
//for use with W5100 based ethernet shields
//remove SD card if inserted

#include <SPI.h>
#include <Ethernet.h>

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //physical mac address

char serverName[] = "web.comporium.net"; // zoomkat's test web page server
EthernetClient client;

//////////////////////

void setup(){

  if (Ethernet.begin(mac) == 0) {
    Serial.println("Failed to configure Ethernet using DHCP");
    // no point in carrying on, so do nothing forevermore:
    while(true);
  }

  Serial.begin(9600); 
  Serial.println("Better client test 9/22/12"); // so I can keep track of what is loaded
  Serial.println("Send an e in serial monitor to test"); // what to do to test
}

void loop(){
  // check for serial input
  if (Serial.available() > 0) //if something in serial buffer
  {
    byte inChar; // sets inChar as a byte
    inChar = Serial.read(); //gets byte from buffer
    if(inChar == 'e') // checks to see byte is an e
    {
      sendGET(); // call sendGET function below when byte is an e
    }
  }  
} 

//////////////////////////

void sendGET() //client function to send/receive GET request data.
{
  if (client.connect(serverName, 80)) {  //starts client connection, checks for connection
    Serial.println("connected");
    client.println("GET /~shb/arduino.txt HTTP/1.1"); //download text
    client.println("Host: web.comporium.net");
    client.println("Connection: close");    
    client.println(); //end of get request
  } 
  else {
    Serial.println("connection failed"); //error message if no client connect
    Serial.println();
  }

  while(client.connected() && !client.available()) delay(1); //waits for data
  while (client.connected() || client.available()) { //connected or data available
    char c = client.read(); //gets byte from ethernet buffer
    Serial.print(c); //prints byte to serial monitor 
  }

  Serial.println();
  Serial.println("disconnecting.");
  Serial.println("==================");
  Serial.println();
  client.stop(); //stop client

}

Hi thanks zoomkat, thats the code I have been using. I copied yours it tested great. Tweaked it for my needs. The monitor shows connected then nothing :astonished:

I think the server script my not adhere to correct standards but it does work for my custom webpage and if i copy the url into a browser.

I have both GET and POST method examples in the playground. http://playground.arduino.cc/Code/WebClient

Hi a bit of progress or more confusion :astonished: :astonished:

Used SurferTim code I amended for my purposes and left it to run. At first it was behaving the same as the earlier code examples, ie nothing happening. However after about the the 6th pass my server software popped up a message have copied part of the log, notice it doesn’t have the full entry for data it shows e and not e3.

02/12/2013 18:50:14 Error Web Server Error 404, cannot serve file: C:\Program Files (x86)\HomeSeer HS2\htmlhttp://192.168.0.34:81/tenHsServer/tenHsServer.aspx?t=ab&f=ToggleDevice&d=E3
02/12/2013 18:50:15 Web Server Got data but was not PUT or GET, from: 192.168.0.1 Data: e

I then loaded earlier sketched and pressed the “E” several times eventually I got the same response as above. So it’s nearly working, are any other headers required or not required??

i can copy the string I am passing into a url and the server responds fine.???

Funny to see I am trying to do the same thing and have the same issues.

Has anybody found a solution to address the above issues?

For some reason when I paste the URL http://192.168.1.140/tenHsServer/tenHsServer.aspx?t=ab&f=toggleDevice&d=v90 into a browser it works like a charm but when I try to do the same through the arduino using the following code:

Serial.print ("Connecting to Homeseer on "); // if you get a connection, report back via serial: Serial.println (HSserver); if (client.connect(HSserver, HSport)) { Serial.print ("Succesfully connected to Homeseer on port "); Serial.println (HSport); // send the HTTP request: client.println("GET /tenHsServer/tenHsServer.aspx?t=ab&f=toggleDevice&d=v90 HTTP/1.1"); client.println("Host: 192.168.1.140"); client.println("Connection: close"); client.println();

It does not work.

Appreciate your input and help!

Francois

Fvhemert: Funny to see I am trying to do the same thing and have the same issues.

No you aren't. The original thread was about POST requests and you are doing a GET request.

Please use [code][/code] tags when posting code.

You shouldn't need

Fvhemert: client.println("Host: 192.168.1.140");

since you aren't connecting to a virtual host.

Fvhemert: It does not work.

What does the server's access and error logs say?

James,

thx for the reply. Yes you are right, this is a GET, not a POST

I have checked the headers and actually have recreated the headers like the ones that are generated by Internet Explorer. No luck there. BTW. IE generates the Host: but U have also tried without.

After some more trouble shooting this evening I had some positive results. It looks like the problem is related to an existing connecting to Homeseer,.

If the log shows a message about an existing connection, the GET will fail: 2014-4-8_06:55:21:2179: Web Server ex0 Exception: An existing connection was forcibly closed by the remote host" 2014-4-8_06:55:31:3968: 2014-4-8_06:55:31:3968: ========================================================= 2014-4-8_06:55:31:3968: Read Start: Data Arrival #53 2014-4-8_06:55:31:3968: Read Start: From IP=192.168.1.15 2014-4-8_06:55:31:3968: Read Start: Data Length=1 2014-4-8_06:55:31:3968: Read Start: Data (Up to 50)= 2014-4-8_06:55:31:3968: ========================================================= 2014-4-8_06:55:31:3968: Read: Getting headers. 2014-4-8_06:55:31:3968: Read_Headers: Getting Headers 2014-4-8_06:55:31:3968: ------------------ 2014-4-8_06:55:31:3968: Read: EXIT (1) 2014-4-8_06:55:31:3968: ------------------

A successful attempt will not start with the connection error: 2014-4-8_06:55:31:4124: 2014-4-8_06:55:31:4124: ========================================================= 2014-4-8_06:55:31:4124: Read Start: Data Arrival #53 2014-4-8_06:55:31:4124: Read Start: From IP=192.168.1.15 2014-4-8_06:55:31:4124: Read Start: Data Length=75 2014-4-8_06:55:31:4124: Read Start: Data (Up to 50)= GET /tenHsServer/tenHsServer.aspx?t=ab&f=toggleDe 2014-4-8_06:55:31:4124: ========================================================= 2014-4-8_06:55:31:4124: Read: Getting headers. 2014-4-8_06:55:31:4124: Read_Headers: Getting Headers 2014-4-8_06:55:31:4124: Read_Headers: Processing...||| GET /tenHsServer/tenHsServer.aspx?t=ab&f=toggleDevice&d=v90 HTTP/1.1||| 2014-4-8_06:55:31:4124: Read_Headers: Processing...|||||| 2014-4-8_06:55:31:4124: Read_Headers: No Data

and the headers are processed correctly.

Now I have to figure out how to close an existing connection before I open one...

Fvhemert: Now I have to figure out how to close an existing connection before I open one...

client.stop();

[u]SOLVED[/u]

Thanks to a fellow board member (Robert Hekkers) at http://www.domoticaforum.eu the problem has been identified and solved.

[u]Short version of his findings:[/u] Arduino's "client.println()" uses 3 TCP packets to send a header line: one for the header text, one for the carriage return (0x0d) and one for the line feed (0x0a). This seems to not work well with Homeseer.

When using :

client.print("blabla\r\n");

instead of:

client.println("blabla");

it works as a charm.

Full details can be found here : http://www.domoticaforum.eu/viewtopic.php?f=63&t=9603

THanks for all the help,

Francois

Since your sub-thread appears to have nothing to do with the original thread, maybe you should click the "report to moderator" button on your initial post. Then ask to have the topic split so you can rename it to something more appropriate. Like "GET requests with http [SOLVED]"

[quote author=James C4S link=topic=201278.msg1676552#msg1676552 date=1397269831] Since your sub-thread appears to have nothing to do with the original thread, maybe you should click the "report to moderator" button on your initial post. Then ask to have the topic split so you can rename it to something more appropriate. Like "GET requests with http [SOLVED]"[/quote] Have you tried that? You can't report your own threads.