Retrieving Content From the Web

Revision as of 20:18, 8 August 2007 by Lchrisman (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

This page details a simple example of integrating external data into Analytica. In this example, historical stock data for a given stock is read from a web site and imported into an Analytica model. The code to read data from the web is written in C++/CLR using Microsoft Visual Studio 2005.

Minimal Requirements:

  • Analytica Enterprise 4.0
  • Microsoft Visual Studio 2005
  • Internet access (to Yahoo finance)

Reading Data from the Web

To read data from the web, a simple C++/CLR application was created in Microsoft Visual Studio 2005. Select File->New->Project/Solution->C++->CLR/.NET Console Application. I named my project ReadURL and placed it in C:\Src.

In the file ReadURL.cpp, I placed the following code:

// ReadURL.cpp : main project file.

#include "stdafx.h"

using namespace System;
using namespace System::Net;
using namespace System::IO;

int main(array<String ^> ^args)
{
	if (args->Length != 1) {
		Console::Error->WriteLine(L"Hello World");
		return -1;
	}

	String^ url = args[0];
	WebRequest^ request = WebRequest::Create(url);
	WebResponse^ response = nullptr;
	StreamReader^ reader = nullptr;
	try {
		response = request->GetResponse();
		Stream^ stream = response->GetResponseStream();
		reader = gcnew StreamReader(stream,Text::Encoding::UTF8);
		Console::Write(reader->ReadToEnd());

	} catch (WebException^ x) {
		Console::Error->WriteLine(x->Message);
		return -2;
	}

	if (response!=nullptr) response->Close();
	if (reader!=nullptr)   reader->Close();

	return 0;
}

This program accepts one command line parameter, a URL, and then reads from that URL and echos the text to StdOut.

Comments


You are not allowed to post comments.