|
|
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  |
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 #8375 is a reply to message #8353] |
Tue, 06 March 2007 10:53   |
mikcsabee
Messages: 10 Registered: February 2007
|
Promising Member |
|
|
Hello!
Thanks for your answer, but it not works.
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 #8387 is a reply to message #8353] |
Tue, 06 March 2007 19:31   |
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
|
|
|
|
|
|
|
|
|
Goto Forum:
Current Time: Sat Apr 26 22:14:05 CEST 2025
Total time taken to generate the page: 0.01340 seconds
|
|
|