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