Welcome to Dream.In.Code
Click Here
Getting C++ Help is Easy!

Join 117,617 C++ Programmers for FREE! Ask your question and get quick answers from experts. There are 1,946 online right now! We've got more than 500 tutorials and 2,000 snippets. Join and find out why Dream.In.Code is the #1 programming help community on the internet! Registration is fast and FREE... Join Now!



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

 
Reply to this topicStart new topic

> Getting data from a remote web server in C++/CLI, Uses C++/CLI

aj32
Group Icon



post 21 Mar, 2008 - 04:43 PM
Post #1


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:

cpp
// 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;:

cpp
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:
cpp
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):
CODE

//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:
cpp

Request = WebRequest::Create( PATH );

Now that we have created a Web Request, the program will then set the Credentials if needed:
cpp

Request->Credentials = CredentialCache::DefaultCredentials;

Next, it will, of course, send the request, and get the response:
cpp

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:
cpp

Console::WriteLine("Data from remote server:\n" + Responsestring + ".\n");

... and close the streams:
cpp

SReader->Close();
Stream1->Close();
Response->Close();

After that, we are pretty much done, the complete code should look like this:
cpp

// 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 tongue.gif) 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 smile.gif
Go to the top of the page
+Quote Post


Register to Make This Ad Go Away!


Reply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 

Lo-Fi Version Time is now: 10/8/08 12:03AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month