Overview
Examples
Screenshots
Comparisons
Applications
Download
Documentation
Tutorials
Bazaar
Status & Roadmap
FAQ
Authors & License
Forums
Funding Ultimate++
Search on this site
Search in forums












SourceForge.net Logo
Home » U++ Library support » U++ Library : Other (not classified elsewhere) » HttpClient example with Post data
HttpClient example with Post data [message #8353] Sun, 04 March 2007 20:53 Go to next message
mikcsabee is currently offline  mikcsabee
Messages: 10
Registered: February 2007
Promising Member
Hello everybody!

I see the Web package in the reference, and I don't know how can I Post some data with the web client to the server. Would someone post an example?

Thanks!


5e455c75752baefde96699018d86f3d1
Re: HttpClient example with Post data [message #8356 is a reply to message #8353] Mon, 05 March 2007 07:13 Go to previous messageGo to next message
fallingdutch is currently offline  fallingdutch
Messages: 258
Registered: July 2006
Experienced Member
use AddHeader(String h)

i think it is not the way ment to be, but it works fine for me Smile

Bas
Re: HttpClient example with Post data [message #8359 is a reply to message #8353] Mon, 05 March 2007 10:32 Go to previous messageGo to next message
mikcsabee is currently offline  mikcsabee
Messages: 10
Registered: February 2007
Promising Member
Hello!

This is not works Confused
I want to read some data in the server side, for example:
<?php

print_r($_POST);

?>


5e455c75752baefde96699018d86f3d1
Re: HttpClient example with Post data [message #8362 is a reply to message #8359] Mon, 05 March 2007 13:28 Go to previous messageGo to next message
victorb is currently offline  victorb
Messages: 78
Registered: December 2005
Location: Nice, France
Member
Have you tried something like:

HttpClient client;

client.Post();
client.Url("http://myserver/index.php?var=value");
client.Execute();



Re: HttpClient example with Post data [message #8365 is a reply to message #8353] Mon, 05 March 2007 15:44 Go to previous messageGo to next message
mikcsabee is currently offline  mikcsabee
Messages: 10
Registered: February 2007
Promising Member
Yes but I think this is the Request Data, and I need Post Data.

5e455c75752baefde96699018d86f3d1
Re: HttpClient example with Post data [message #8374 is a reply to message #8365] Tue, 06 March 2007 09:41 Go to previous messageGo to next message
fallingdutch is currently offline  fallingdutch
Messages: 258
Registered: July 2006
Experienced Member
just had a look at the POST definition of HTTP:

it should work with the AddHeader method:
client.Post();
client.Url("http://www.fallingdutch.de");
client.AddHeader("\n\nvar1=hello&var2=fallingdutch");
client.Execute();


var1 and var2 are the names of values to set,
hello and fallingdutch are their values.
The newlines at the beginning should be added to ensure the header of the http-request is closed and the data-part start.


Bas
Re: HttpClient example with Post data [message #8375 is a reply to message #8353] Tue, 06 March 2007 10:53 Go to previous messageGo to next message
mikcsabee is currently offline  mikcsabee
Messages: 10
Registered: February 2007
Promising Member
Hello!

Thanks for your answer, but it not works. Confused

Here is my C++ code:
static String DumpSpecial(String s)
{
	String out;
	for(const char *p = s.Begin(), *e = s.End(); p < e; p++)
		if((byte)*p >= ' ' && *p != '\xFF')
			out.Cat(*p);
		else {
			switch(*p) {
				case '\a': out.Cat("[\\a]"); break;
				case '\b': out.Cat("[\\b]"); break;
				case '\f': out.Cat("[\\f]"); break;
				case '\v': out.Cat("[\\v]"); break;
				case '\t': out.Cat("[\\t]"); break;
				case '\r': out.Cat("[\\r]"); break;
				//case '\n': out.Cat("[\\n]\n"); break;
				case '\n': out.Cat("\n"); break;
				default:   out.Cat(NFormat("[\\x%02x", (byte)*p)); break;
			}
		}
	return out;
}

CONSOLE_APP_MAIN
{
		String url = "127.0.0.1/post/index.php";
		puts(url);
		fflush(stdout);
		HttpClient client;
		
		client.Post();
		client.URL(url);
		client.AddHeaders("\n\nvar1=hello&var2=something");


		String content = client.Execute();
		puts("[error] " + Nvl(client.GetError(), "OK (no error)"));
		puts(NFormat("[status %d] %s\n", client.GetStatusCode(), client.GetStatusLine()));
		puts(NFormat("[headers] (%d bytes)\n%s", client.GetHeaders().GetLength(), DumpSpecial(client.GetHeaders())));
		puts(NFormat("[content] (%d bytes)\n%s", content.GetLength(), DumpSpecial(content)));
		fflush(stdout);
}


And here is the PHP code in server side:
<?php

echo "\nPOST:\n";
print_r($_POST);

echo"\n\nREQUEST:\n";
print_r($_REQUEST);

print_r(getallheaders());

?>


And here is the output:
Quote:



127.0.0.1/post/index.php
[error] OK (no error)
[status 200] HTTP/1.1 200 OK

[headers] (184 bytes)
Date: Tue, 06 Mar 2007 09:35:22 GMT[\r]
Server: Apache/2.0.55 (Ubuntu) PHP/5.1.6[\r]
X-Powered-By: PHP/5.1.6[\r]
Content-Length: 231[\r]
Connection: close[\r]
Content-Type: text/html; charset=UTF-8[\r]

[content] (231 bytes)

POST:
Array
(
)


REQUEST:
Array
(
)
Array
(
[URL] => http://127.0.0.1/post/index.php
[Host] => 127.0.0.1
[Connection] => close
[Accept] => */*
[Accept-Encoding] => gzip
[Agent] => Ultimate++ HTTP client
)




And what I need? Some data in the Post array.


5e455c75752baefde96699018d86f3d1
Re: HttpClient example with Post data [message #8376 is a reply to message #8375] Tue, 06 March 2007 11:07 Go to previous messageGo to next message
fallingdutch is currently offline  fallingdutch
Messages: 258
Registered: July 2006
Experienced Member
use PostData(String pd) instead of AddHeader(); and remove the "\n\n" ... the problem might be the content-length value of the header.

Bas
Re: HttpClient example with Post data [message #8377 is a reply to message #8353] Tue, 06 March 2007 11:33 Go to previous messageGo to next message
mikcsabee is currently offline  mikcsabee
Messages: 10
Registered: February 2007
Promising Member
PostData ? I never heard this function. And not works.

5e455c75752baefde96699018d86f3d1
Re: HttpClient example with Post data [message #8385 is a reply to message #8377] Tue, 06 March 2007 19:12 Go to previous messageGo to next message
fallingdutch is currently offline  fallingdutch
Messages: 258
Registered: July 2006
Experienced Member
just had a look at an old code of mine:
i used httpclient this way:

String request, data, reply;


http.ClearHeaders();
request << "Content-Length: "<< (int) data.GetLength()<<"\r\n";
request << "Content-Type: txt/xml\r\n\r\n";
http.Post();
http.std_headers = true;
http.AddHeaders(request);

data << "var1=hello&var2=fallingdutch";

http.AddHeaders(data);
reply = http.Execute();


Hope that helps,
Bas
Re: HttpClient example with Post data [message #8387 is a reply to message #8353] Tue, 06 March 2007 19:31 Go to previous messageGo to next message
mikcsabee is currently offline  mikcsabee
Messages: 10
Registered: February 2007
Promising Member
Thanks for your help, but it still not working
Here is my program code:
	String url = "127.0.0.1/post/index.php";
	String request, data, reply;

	data << "var1=hello&var2=fallingdutch";

	request << "Content-Length: "<< (int) data.GetLength()<<"\r\n";
	request << "Content-Type: txt/xml\r\n\r\n";

	puts(url);
	fflush(stdout);
	HttpClient client;

	client.ClearHeaders();
	client.Post();
	client.std_headers = true;
	client.Headers(request);
	client.AddHeaders(data);
	client.URL(url);

	String content = client.Execute();
	puts("[error] " + Nvl(client.GetError(), "OK (no error)"));
	puts(NFormat("[status %d] %s\n", client.GetStatusCode(), client.GetStatusLine()));
	puts(NFormat("[headers] (%d bytes)\n%s", client.GetHeaders().GetLength(), DumpSpecial(client.GetHeaders())));
	puts(NFormat("[content] (%d bytes)\n%s", content.GetLength(), DumpSpecial(content)));
	fflush(stdout);

The PHP script is same.
And here is the output:
Quote:


127.0.0.1/post/index.php
[error] OK (no error)
[status 200] HTTP/1.1 200 OK

[headers] (184 bytes)
Date: Tue, 06 Mar 2007 18:24:12 GMT[\r]
Server: Apache/2.0.55 (Ubuntu) PHP/5.1.6[\r]
X-Powered-By: PHP/5.1.6[\r]
Content-Length: 288[\r]
Connection: close[\r]
Content-Type: text/html; charset=UTF-8[\r]

[content] (288 bytes)

POST:
Array
(
)


REQUEST:
Array
(
)
Array
(
[URL] => http://127.0.0.1/post/index.php
[Host] => 127.0.0.1
[Connection] => close
[Accept] => */*
[Accept-Encoding] => gzip
[Agent] => Ultimate++ HTTP client
[Content-Length] => 28
[Content-Type] => txt/xml
)


I use Ubuntu Linux 6.10 whit fresh updates, and Ultimate++ 2007.1beta3
I dont know what is the problem.


5e455c75752baefde96699018d86f3d1
Re: HttpClient example with Post data [message #8391 is a reply to message #8387] Tue, 06 March 2007 20:10 Go to previous messageGo to next message
fallingdutch is currently offline  fallingdutch
Messages: 258
Registered: July 2006
Experienced Member
hope this will do the trick:
change the request Content-type:
Content-Type: application/x-www-form-urlencoded


Good luck,
Bas
Re: HttpClient example with Post data [message #8393 is a reply to message #8353] Tue, 06 March 2007 20:20 Go to previous messageGo to next message
mikcsabee is currently offline  mikcsabee
Messages: 10
Registered: February 2007
Promising Member
This is works!
Thanks!
But what can I do, if one of may var value is for example this: "tom & jerry".


5e455c75752baefde96699018d86f3d1
Re: HttpClient example with Post data [message #8394 is a reply to message #8393] Tue, 06 March 2007 21:09 Go to previous messageGo to next message
fallingdutch is currently offline  fallingdutch
Messages: 258
Registered: July 2006
Experienced Member
You are Welcome.

i guess you will have to escape the value somehow.

but no idea how to - just search the internet, i guess you are not the first with that kind of problem Wink

Bas

[Updated on: Tue, 06 March 2007 21:10]

Report message to a moderator

Re: HttpClient example with Post data [message #8395 is a reply to message #8393] Tue, 06 March 2007 21:40 Go to previous messageGo to next message
mezise is currently offline  mezise
Messages: 54
Registered: April 2006
Member
mikcsabee wrote on Tue, 06 March 2007 20:20

This is works!
Thanks!
But what can I do, if one of may var value is for example this: "tom & jerry".

Urlencoded value for "&" is "%26", so just replace it and PHP will see this correctly.

Michal
Re: HttpClient example with Post data [message #8396 is a reply to message #8353] Tue, 06 March 2007 21:47 Go to previous messageGo to next message
mikcsabee is currently offline  mikcsabee
Messages: 10
Registered: February 2007
Promising Member
Thats great!
It's work!
Thank you all of your helpful answers!


5e455c75752baefde96699018d86f3d1
Re: HttpClient example with Post data [message #8397 is a reply to message #8396] Tue, 06 March 2007 23:39 Go to previous message
zsolt is currently offline  zsolt
Messages: 693
Registered: December 2005
Location: Budapest, Hungary
Contributor
you can use UrlEncode() function.
Previous Topic: How to run the email client [SOLVED]
Next Topic: am ashamed to ask for help on this but...
Goto Forum:
  


Current Time: Fri Mar 29 13:07:05 CET 2024

Total time taken to generate the page: 0.01489 seconds