Go Down

Topic: Sending HTTP Request (Read 18737 times) previous topic - next topic

danpe

Aug 22, 2010, 09:38 pm Last Edit: Aug 22, 2010, 09:38 pm by danpe Reason: 1
I'm trying to send data to an .aspx (ASP .NET) page by posing a reuqest (POST), and to make my aspx page save the data into SQL..

here is my http_request code:
Code: [Select]
void http_request()
{
 if (client.connect())
   {
     Serial.println("Connecting to Client...");
     // Make a HTTP request:
     client.print("POST http://www.green-storms.info/wtcs/SQL/UpdateSQL.aspx?Relay1=Hello");
     client.println(" HTTP/1.1\n");
     client.println("Host: green-storms.info\n");
     client.print("Content-Length: 12\n");
     client.println();
     blinkie(5, 50);
   }
   else {
       // kf you didn't get a connection to the server:
       Serial.println("Client Connection Failed!");
   }
}


before this code im connecting to the client with the IP of the domain (97,74,215,128) the client succeds to connect but nothing heppends..

Please help..

PaulS

Quote
the client succeds to connect but nothing heppends..

Do you have access to the log files on the server? Something is happening.

Code: [Select]
     client.print("Content-Length: 12\n");
You are aware, I hope, that client.println adds a \r and a \n. Perhaps the \r is needed, too.

What content length are you referring to? Does blinkie() add something to the stream being sent to the client?
The art of getting good answers lies in asking good questions.

danpe

I dont have access to the logs...?

and i dont really know why im adding this length and what it means...

PaulS

Typically, the content length header is needed for a multi-content send - for example uploading a picture file where the binary file might contain embedded carriage returns and line feeds.

If that is the case, the number of bytes in the content needs to be determined another way. That is what that particular header is for.

If there is not mixed content in the header, the end of the header is generally detected by the presence of a blank line (\r\n\r\n sequence) (or maybe it's \n\r\n\r).

The client.println function outputs the \r and \n in the correct order, for each non-blank record. Then, a client.println(); is used to output the final \r and \n in the correct order.
The art of getting good answers lies in asking good questions.

danpe

#4
Aug 23, 2010, 11:21 am Last Edit: Aug 23, 2010, 11:43 am by danpe Reason: 1
Thx for the explenation :)

the address im posting to is this "UpdateSQL.aspx?Relay1=Hello"
I used basic logic so im posting to UpdateSQL.aspx page with the parameter named Relay1=Hello...

But nothing heppends...
And it works if im going trough my browser with this address

I changed my code to this:
Code: [Select]
Serial.println("Connecting to Client...");
     // Make a HTTP request:
     client.println("POST http://www.green-storms.info/wtcs/SQL/UpdateSQL.aspx?Relay1=Hello");
     client.println(" HTTP/1.1");
     client.println("Host: green-storms.info");
     client.println("Content-Length: 12");
     client.println();


And i get this response:
Code: [Select]
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN""http://www.w3.org/TR/html4/strict.dtd">
<HTML><HEAD><TITLE>Bad Request</TITLE>
<META HTTP-EQUIV="Content-Type" Content="text/html; charset=us-ascii"></HEAD>
<BODY><h2>Bad Request - Invalid Verb</h2>
<hr><p>HTTP Error 400. The request verb is invalid.</p>
</BODY></HTML>


andrew0

I would make it a GET request and remove the Content-Length header (only needed for POSTs) as it looks like you're supplying all the info in your URL. Also I think the "HTTP/1.1" bit should be on the same line as the GET request, so make the first println a print instead.

Try this:

Code: [Select]
     Serial.println("Connecting to Client...");
     // Make a HTTP request:
     client.print("GET http://www.green-storms.info/wtcs/SQL/UpdateSQL.aspx?Relay1=Hello");
     client.println(" HTTP/1.1");
     client.println("Host: green-storms.info");
     client.println();


Andrew

danpe

HEY man it works !! :)
thank you here is my working code:
Code: [Select]
Serial.println("Connecting to Client...");
     // Make a HTTP request:
     client.print("GET http://www.green-storms.info/wtcs/SQL/UpdateSQL.aspx?Relay1=OMGitWORKS!!");
     client.println(" HTTP/1.1");
     client.println("Host: green-storms.info");


But as far as i know GET has some restrictions or capabilities.. am i right?
If i want to send more then 1 parameter on the URL is it possible?

Send like 10 parameters ?

thx :)

andrew0

#7
Aug 23, 2010, 03:34 pm Last Edit: Aug 23, 2010, 03:34 pm by Andrew Reason: 1
Sending more parameters is as easy as

Code: [Select]
client.print("GET http://www.green-storms.info/wtcs/SQL/UpdateSQL.aspx?Relay1=OMGitWORKS!!&param2=54&param3=Hello&param5=blue");

There's probably a restriction on the total length of the query string, but it should be high enough to do anything sensible.  POST requests come in when you want to send more info like a whole block of text, binary data representing a picture etc.

Andrew

Go Up