|
|
Home » U++ Library support » U++ Library : Other (not classified elsewhere) » Web or HttpClient example?
Web or HttpClient example? [message #8208] |
Mon, 19 February 2007 04:17 |
DrGary
Messages: 7 Registered: February 2007
|
Promising Member |
|
|
I see references to a Web package and HttpClient, but haven't found any documentation. Would someone post an example showing how to retrieve a web page? For example, I want to count how many times a word occurs on a given homepage...
Thanks,
Gary
|
|
|
Re: Web or HttpClient example? [message #8222 is a reply to message #8208] |
Tue, 20 February 2007 21:14 |
rylek
Messages: 79 Registered: November 2005
|
Member |
|
|
Hello there!
I've made a very simple example (means I've put it in the 'examples' nest) with a suprising name "httpcli" which demonstrates basic use of the HttpClient. It's a console application which processes each of its commandline arguments as an URL, downloads the given page and displays its contents on the standard output. If you don't want to wait until next distribution and are reluctant to download it from our live UVS repository, here you are, just copy the folowing source into a file and add the Web package.
Regards
Tomas
#include <Web/Web.h>
using namespace Upp;
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;
default: out.Cat(NFormat("[\\x%02x", (byte)*p)); break;
}
}
return out;
}
CONSOLE_APP_MAIN
{
const Vector<String>& cmdline = CommandLine();
for(int i = 0; i < cmdline.GetCount(); i++) {
String proxy;
if(*cmdline[i] == ':' && i + 1 < cmdline.GetCount())
proxy = cmdline[i++].Mid(1);
String url = cmdline[i];
puts(url);
fflush(stdout);
HttpClient client;
client.URL(url);
client.Proxy(proxy);
String content = client.ExecuteRedirect();
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);
}
}
[Updated on: Tue, 20 February 2007 21:14] Report message to a moderator
|
|
|
|
|
|
|
|
Goto Forum:
Current Time: Tue Apr 23 08:40:13 CEST 2024
Total time taken to generate the page: 0.02239 seconds
|
|
|