Page 1 of 1

Getting data from a remote web server in C++/CLI Uses C++/CLI Rate Topic: -----

#1 aj32  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 30
  • View blog
  • Posts: 577
  • Joined: 30-August 07

Post icon  Posted 21 March 2008 - 04:43 PM

In this tutorial, I will be showing you how to connect to a remote web server, read a file off of it, close the connection and display it's contents. I will be using Visual Studio for this tutorial.

First, create a CLR console application. When you create the project, make sure "yournamehere.cpp" is open (it should open automatically by default). The code in that file should look something like this:

// Tutorial 5 - getting data off a server.cpp : main project file.

#include "stdafx.h"

using namespace System;

int main(array<System::String ^> ^args)
{
    Console::WriteLine(L"Hello World");
    return 0;
}


Remove the Console:WriteLine(L"Hello World"); code.

First, we add the namespaces and includes that are required, add the following under #include "stdafx.h":
#using <System.dll>.

Add this under using namespace System;:

using namespace System::IO;
using namespace System::Net;
using namespace System::Net::NetworkInformation;
using namespace System::Text;


Now, that we have our includes and namespaces in place, we will define our variables. After int main(...) {, add these variables:
WebRequest^ Request;
	HttpWebResponse^ Response;
	Stream^ Stream1;
	StreamReader^ SReader;
	String^ Responsestring;
      String^ PATH = "http://www.miller-warner.com/foxsoftware/product/dicwp4/SYSTEM/tutorialexample.log"; //PATH to File on server //
	int CONN;


These are our variables that will be used in our program.


Now we want to check if the user is online or not, we will do this by pinging an IP address that will always be online (such as Microsoft):
//CHECK TO SEE IF SERVER IS ONLINE//
   Ping ^pingSender = gcnew Ping;
   PingReply ^ reply = pingSender->Send("209.85.165.99");
   if ( reply->Status == IPStatus::Success )
   {
	   CONN = 10;//The user is online, set our connection variable (CONN) to 10;
   }
   else
   {
	   CONN = 400;//The user is offline, set our connection variable to 400;
   }
   //END

   if(CONN == 10)//is the user online?
   {
	   //... (User is online, get the data...) ...//
   }
   else//User is not online//
   {
	   Console::WriteLine("Sorry, cannot detect a internet connection.");
   }


What the code you just entered into your IDE will 1. Ping a remote server, and 2. if the ping is a success, it will set our CONN variable to 10, indicating the user online, if not, it will set our CONN variable to 400, the user is offline.

Now, if the user is connected to the internet (where I added the comment "//... (User is online, get the data...) ...//"), our program will first create a http Web Request:
Request = WebRequest::Create( PATH );


Now that we have created a Web Request, the program will then set the Credentials if needed:
Request->Credentials = CredentialCache::DefaultCredentials;


Next, it will, of course, send the request, and get the response:
Response = dynamic_cast<HttpWebResponse^>(Request->GetResponse());
Stream1 = Response->GetResponseStream();
SReader = gcnew StreamReader( Stream1 );
Responsestring = SReader->ReadToEnd();


Now, the variable String^ Responsestring contains the contents of the file read. There is not much to do after that, we just have to display the contents:
Console::WriteLine("Data from remote server:\n" + Responsestring + ".\n");


... and close the streams:
SReader->Close();
Stream1->Close();
Response->Close();


After that, we are pretty much done, the complete code should look like this:
// Tutorial 5 - getting data off a server.cpp : main project file.

#include "stdafx.h"
#using <System.dll>

using namespace System;
using namespace System::IO;
using namespace System::Net;
using namespace System::Net::NetworkInformation;
using namespace System::Text;

int main(array<System::String ^> ^args)
{
	WebRequest^ Request;
	HttpWebResponse^ Response;
	Stream^ Stream1;
	StreamReader^ SReader;
	String^ Responsestring;
      String^ PATH = "http://www.miller-warner.com/foxsoftware/product/dicwp4/SYSTEM/tutorialexample.log"; //PATH to File on server //
	int CONN;

   //CHECK TO SEE IF SERVER IS ONLINE//
   Ping ^pingSender = gcnew Ping;
   PingReply ^ reply = pingSender->Send("209.85.165.99");
   if ( reply->Status == IPStatus::Success )
   {
	   CONN = 10;//The user is online, set our connection variable (CONN) to 10;
   }
   else
   {
	   CONN = 400;//The user is offline, set our connection variable to 400;
   }
   //END

   if(CONN == 10)//is the user online?
   {
	   Request = WebRequest::Create( PATH );
	   Request->Credentials = CredentialCache::DefaultCredentials;
	   Response = dynamic_cast<HttpWebResponse^>(Request->GetResponse());
		Stream1 = Response->GetResponseStream();
		 SReader = gcnew StreamReader( Stream1 );
		 Responsestring = SReader->ReadToEnd();
		 Console::WriteLine("Data from remote server:\n" + Responsestring + ".\n");
		 SReader->Close();
		 Stream1->Close();
		 Response->Close();
   }
   else//User is not online//
   {
	   Console::WriteLine("Sorry, cannot detect a internet connection.");
   }
    return 0;
}



If you run the program (and the miller-warner server is up :P) it will connect to it, read the file and display the contents of the file!

This can be useful in several ways, for instance, if you want to automatically notify your user of an update, bug fix. ect. this technique can be used!


I hope this tutorial is useful, thanks for reading!

-AJ32 :)

Is This A Good Question/Topic? 0
  • +

Replies To: Getting data from a remote web server in C++/CLI

#2 chowdawg  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 28-July 09

Posted 28 July 2009 - 10:54 AM

Good tutorial! The server you provided gave a 404 though. Switched to my company's IP and URL and it worked great.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1