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) » Web or HttpClient example?
Web or HttpClient example? [message #8208] Mon, 19 February 2007 04:17 Go to next message
DrGary is currently offline  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 Go to previous messageGo to next message
rylek is currently offline  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

Re: Web or HttpClient example? [message #8226 is a reply to message #8208] Wed, 21 February 2007 08:48 Go to previous messageGo to next message
DrGary is currently offline  DrGary
Messages: 7
Registered: February 2007
Promising Member
Fantastic. Worked great.

Note for other novices: As Tomas said, don't forget to add the Web package with "Project | Add Package to 'yourprojectname'"

Thanks!
Re: Web or HttpClient example? [message #8244 is a reply to message #8208] Fri, 23 February 2007 12:32 Go to previous messageGo to next message
mikcsabee is currently offline  mikcsabee
Messages: 10
Registered: February 2007
Promising Member
Hello!
DELETED


5e455c75752baefde96699018d86f3d1

[Updated on: Mon, 18 February 2008 23:44]

Report message to a moderator

Re: Web or HttpClient example? [message #8245 is a reply to message #8244] Fri, 23 February 2007 12:38 Go to previous messageGo to next message
mikcsabee is currently offline  mikcsabee
Messages: 10
Registered: February 2007
Promising Member
DELETED

5e455c75752baefde96699018d86f3d1

[Updated on: Mon, 18 February 2008 23:44]

Report message to a moderator

Re: Web or HttpClient example? [message #31750 is a reply to message #8208] Thu, 24 March 2011 12:31 Go to previous messageGo to next message
jibe is currently offline  jibe
Messages: 294
Registered: February 2007
Location: France
Experienced Member
Hi,

I'm not experienced on working with http... I used this exemple to get some XML results from ISBNdb and Worldcat APIs, and it's working well.

Now, I should like to get the html source code of some web page. But with httpcli, I get only this :
$./httpcli www.google.com
www.google.com
[error] OK (no error)
[status 200] HTTP/1.0 200 OK

[headers] (626 bytes)
Date: Thu, 24 Mar 2011 11:09:07 GMT[\r][\n]
Expires: -1[\r][\n]
Cache-Control: private, max-age=0[\r][\n]
Content-Type: text/html; charset=ISO-8859-1[\r][\n]
Set-Cookie: PREF=ID=acbb220690b61042:FF=0:TM=1300964947:LM=1300964947:S=dwbPpco5W2KSRXwI; expires=Sat, 23-Mar-2013 11:09:07 GMT; path=/; domain=.google.fr[\r][\n]
Set-Cookie: NID=45=xqvQ_JKFFBdZiWZTNBxxojL61-CE3dq-nwCvypqvVhNya4zlJE77YGz-ICGidH1g6BuXntMbD36obwUEnH4FwSNirQ98nQnr2TtrMdwmorr7dPmBXyCNzKYw9Y9kSfwP; expires=Fri, 23-Sep-2011 11:09:07 GMT; path=/; domain=.google.fr; HttpOnly[\r][\n]
Server: gws[\r][\n]
X-XSS-Protection: 1; mode=block[\r][\n]
X-Cache: MISS from orange.beeservices.dyndns.org[\r][\n]
Connection: close[\r][\n]
$

How can I get the source code of the page ?
Re: Web or HttpClient example? [message #31751 is a reply to message #8208] Thu, 24 March 2011 14:38 Go to previous message
jibe is currently offline  jibe
Messages: 294
Registered: February 2007
Location: France
Experienced Member
Embarassed Never mind ! I didn't payed enough attention to the code concerning the output !

Adding just
puts(~content);
does the job...

Anyway, I still have some problems using httpclient. Some more doc or examples about the various methods should be very helpfull...
Previous Topic: Apps might need to be recompiled after libnotify update
Next Topic: XmlRpc Bugfixes
Goto Forum:
  


Current Time: Tue Dec 10 18:06:23 CET 2019

Total time taken to generate the page: 0.00982 seconds