Click here to Skip to main content
15,892,005 members
Articles / Desktop Programming / MFC

Classes for Writing HTTP Clients in C++

Rate me:
Please Sign up or sign in to vote.
4.41/5 (22 votes)
2 Jan 2009BSD3 min read 235K   9.3K   86  
Wrapper classes for Win32 HTTP calls, URL encoding, etc.
#include <string>
#include <vector>
#include <iostream>
using namespace std;

#include <windows.h>
#include <wininet.h>

#include "web.h"
using namespace openutils;



int main() {
	/*char name[51];
	char rate[12];
	char amt[12];
	cout << "Welcome to the On-line interest calculator. " << endl;
	cout << "Enter name: ";
	gets(name);
	cout << "Enter rate: ";
	cin >> rate;
	cout << "Enter amount: ";
	cin >> amt;*/
	
	try {	
		WebForm wf;
		wf.setHost("http://www.belex.rs");
		wf.setScriptFile("/srp/akcije.php");
		wf.putVariable("t","2");
		wf.putVariable("s","AGBN");
		wf.putVariable("i","770");
		wf.sendRequest();
		char buff[1000 * 1024];
		if(wf.getResponse(buff,sizeof(buff)-1))
		{
			cout << buff << endl;
			cout << "Response length: " << strlen(buff) << endl;
		}
		else
		{
			cout << "No response from server" << endl;
		}
		return 0;		
	}catch(WebFormException ex) {
		cout << ex.getMessage() << endl;
	}
	return 0;
}

void test() {
	WebForm wf;
		// the web server name is set
		wf.setHost("http://www.yahoo.com");
		// the script to be executed on the web server...
		wf.setScriptFile("./");
		cout << "Contacting " << wf.getHost() << "..." << endl;

		// form variables are added to the request object
		// wf.putVariable("name",name);
		// wf.putVariable("rate",rate);
		// wf.putVariable("amt",amt);

		cout << "Sending request..." << endl;
		// data is encoded and send to the server script
		// for processing
		wf.sendRequest();

		// reading back any response
		char response[400 * 1024];			
		if(wf.getResponse(response,sizeof(response) - 1)) {
			cout << endl << response << endl;
		}else {
			cout << "No response from server" << endl;
		}	
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The BSD License


Written By
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions