[C++ CLI] Get response from website

Pages: 12
Hello again :)
I need help with one code, i want to make website checker for my websites in work when a site is online, or get some error code, for example 500 etc. How can i get this code as response in C++? This is all i want, then i can make full code alone :)

Thanks a lot :)
Thanks, i try to make a code but i have this error, what i do wrong?

http://prntscr.com/b6zk1e
Its working now

1
2
3
4
5
6
private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {
		HttpWebRequest^ request = dynamic_cast<HttpWebRequest^>(WebRequest::Create("http://google.sk"));
		request->Credentials = CredentialCache::DefaultCredentials;
		HttpWebResponse^ response = dynamic_cast<HttpWebResponse^>(request->GetResponse());
		label1->Text = response->StatusCode.ToString;
	}


but how can i get for example 200 when i see OK, i dont want string, i want number status code. thanks :)
You can try one of these:
1
2
3
4
5
6
7
8
int StatusCode = static_cast<int>(response->StatusCode);

      // or

if (response->StatusCode == HttpStatusCode::Found)
{
   StatusCode = 302;
}

Here is a list of all the codes:
https://msdn.microsoft.com/de-de/library/system.net.httpstatuscode%28v=vs.110%29.aspx
Thanks :)
and one last question about this http and background worker, how can i solve this please?

http://prntscr.com/b705ed
You can access the controls on the GUI only when the backgroundworker fires the ProgressChanged Event
https://msdn.microsoft.com/en-us/library/system.componentmodel.backgroundworker.progresschanged%28v=vs.110%29.aspx
and how can i set a label text or parse string to progresschanged event? i know how to set int, its progressvalue or percentagevalue, but now i need a string like "OK", "ConnectionClosed" etc
this works but i dont know if its correct, its any better way how to make it?

1
2
3
4
5
6
7
8
9
10
private: System::Void button_overit_Click(System::Object^  sender, System::EventArgs^  e) {
		backgroundWorker1->RunWorkerAsync();
	}
	private: System::Void backgroundWorker1_DoWork(System::Object^  sender, System::ComponentModel::DoWorkEventArgs^  e) {
		backgroundWorker1->ReportProgress(1);
	}
	private: System::Void backgroundWorker1_ProgressChanged(System::Object^  sender, System::ComponentModel::ProgressChangedEventArgs^  e) {
		label_demaBike->Text += GetWebStatusCode("http://dema.bike");
		label_demaSk->Text += GetWebStatusCode("http://dema.sk");
	}
For example i want check statuscode in background and when status code is OK, then set label to OK, when it's not OK, then set label to "Problem with website" for example.

only what i think i can do, is parse status code as progresspercentage and in progresschanged make if's, when e->progresspercentage is 200, then set label to OK

1
2
3
4
5
6
7
8
9
10
11
12
13
private: System::Void backgroundWorker1_DoWork(System::Object^  sender, System::ComponentModel::DoWorkEventArgs^  e) {
		String^ StatusCode;
		while (true)
		{
			StatusCode = GetWebStatusCode("http://google.sk");
			System::Threading::Thread::Sleep(5000);
			backgroundWorker1->ReportProgress(1);
		}
	}
	private: System::Void backgroundWorker1_ProgressChanged(System::Object^  sender, System::ComponentModel::ProgressChangedEventArgs^  e) {
		label_demaBike->Text =
		label_demaSk->Text =
	}
thanks, and one maybe stupid question, i never use datapacket, then what i need to include or which namespace to use? Thanks
i have this
1
2
3
4
5
class Datapacket
{
public:
	string val1;
};

and i get this in DoWork
-> http://prntscr.com/b710ic

EDIT: problem is class -> ref class and string -> String^
Last edited on
DataPacket need to be CLR class.
Try this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
ref class Datapacket
{
public:
	string val1;
};

backgroundWorker_DoWork(Object^  sender, DoWorkEventArgs^  e) 
{
    Datapacket^ dp = gcnew Datapacket();
    dp->Val1 = "Complete"; // or sth. else
    backgroundWorker->ReportProgress(0, dp);
}

backgroundWorker_ProgressChanged(Object^  sender, ProgressChangedEventArgs^  e)
{
    Datapacket^ dp = dynamic_cast<Datapacket^>(e->UserState);

    // update gui, etc
    textBox_Data->Text = dp->Val1;
}
yea i fix it, in post i make EDIT, thanks a lot :)

and about background worker, why i dont have loop when i make this, i change button text(for check if numbers go up), but i see only '1' and nothing more, not 2 not 3 etc

i try without Thread::Sleep(5000) or with it, same problem

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
private: System::Void button_overit_Click(System::Object^  sender, System::EventArgs^  e) {
		backgroundWorker1->RunWorkerAsync();
	}
	private: System::Void backgroundWorker1_DoWork(System::Object^  sender, System::ComponentModel::DoWorkEventArgs^  e) {
		Datapacket^ dp = gcnew Datapacket();
		int cislo = 0;
		while (true)
		{
			dp->val1 = GetWebStatusCode("http://dema.bike");
			dp->val2 = GetWebStatusCode("http://dema.sk");
			dp->val3 = GetWebStatusCode("http://urnaweb.cz");
			dp->val4 = GetWebStatusCode("http://metrumservis.sk");
			backgroundWorker1->ReportProgress(++cislo, dp);
		}
	}
	private: System::Void backgroundWorker1_ProgressChanged(System::Object^  sender, System::ComponentModel::ProgressChangedEventArgs^  e) {
		Datapacket^ dp = dynamic_cast<Datapacket^>(e->UserState);
		label_demaBike->Text = dp->val1;
		label_demaSk->Text = dp->val2;
		label_urnaweb->Text = dp->val3;
		button_overit->Text = e->ProgressPercentage + "";
	}
	};
I try this, dont work :/
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
private: System::Void backgroundWorker1_DoWork(System::Object^  sender, System::ComponentModel::DoWorkEventArgs^  e) {
		Datapacket^ dp = gcnew Datapacket();
		int cislo = 0;
		while (true)
		{
			Console::WriteLine("Spusteny novy cyklus"); //I can see this twice, 1. this line, 2. END CONSOLE, 3. this line, 4. nothing more
			dp->val1 = GetWebStatusCode("http://dema.bike");
			dp->val2 = GetWebStatusCode("http://dema.sk");
			dp->val3 = GetWebStatusCode("http://urnaweb.cz");
			dp->val4 = GetWebStatusCode("http://metrumservis.sk");
			backgroundWorker1->ReportProgress(++cislo, dp);
			Console::WriteLine("Dokoncena uloha {0}", cislo); // END CONSOLE
			Thread::Sleep(1000);
		}
	}
	private: System::Void backgroundWorker1_ProgressChanged(System::Object^  sender, System::ComponentModel::ProgressChangedEventArgs^  e) {
		Datapacket^ dp = dynamic_cast<Datapacket^>(e->UserState);
		label_demaBike->Text = dp->val1;
		label_demaSk->Text = dp->val2;
		label_urnaweb->Text = dp->val3;
		label_metrumservis->Text = dp->val4;
		button_overit->Text = e->ProgressPercentage + "";
	}
	private: System::Void backgroundWorker1_RunWorkerCompleted(System::Object^  sender, System::ComponentModel::RunWorkerCompletedEventArgs^  e) {
	}
	};


when i edit code like this, then i see numbers like seconds, 1,2,3,4,5 .....
1
2
3
4
5
6
7
8
9
while (true)
		{
			/*dp->val1 = GetWebStatusCode("http://dema.bike");
			dp->val2 = GetWebStatusCode("http://dema.sk");
			dp->val3 = GetWebStatusCode("http://urnaweb.cz");
			dp->val4 = GetWebStatusCode("http://metrumservis.sk");*/
			backgroundWorker1->ReportProgress(++cislo, dp);
			Thread::Sleep(1000);
		}
Last edited on
This is GetWebStatusCode function what i made

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
static String^ GetWebStatusCode(String^ adresa)
{
	try
	{
		HttpWebRequest^ request = dynamic_cast<HttpWebRequest^>(WebRequest::Create(adresa));
		HttpWebResponse^ response = dynamic_cast<HttpWebResponse^>(request->GetResponse());
		if (response->StatusCode == HttpStatusCode::OK)
		{
			return response->StatusCode.ToString();
		}
		response->Close();
	}
	catch (WebException^ e)
	{
		return e->Status.ToString();
	}
	catch (Exception^ e)
	{
		return e->Message;
	}
}
This works, but why my function no?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
private: System::Void backgroundWorker1_DoWork(System::Object^  sender, System::ComponentModel::DoWorkEventArgs^  e) {
		Datapacket^ dp = gcnew Datapacket();
		int cislo = 0;
		while (true)
		{
			try
			{
				HttpWebRequest^ request = dynamic_cast<HttpWebRequest^>(WebRequest::Create("http://dema.bike"));
				HttpWebResponse^ response = dynamic_cast<HttpWebResponse^>(request->GetResponse());
				if (response->StatusCode == HttpStatusCode::OK)
				{
					dp->val1 = response->StatusCode.ToString();
				}
				response->Close();
			}
			catch (WebException^ e)
			{
				dp->val1 = e->Status.ToString();
			}
			catch (Exception^ e)
			{
				dp->val1 = e->Message;
			}

			try
			{
				HttpWebRequest^ request = dynamic_cast<HttpWebRequest^>(WebRequest::Create("http://dema.sk"));
				HttpWebResponse^ response = dynamic_cast<HttpWebResponse^>(request->GetResponse());
				if (response->StatusCode == HttpStatusCode::OK)
				{
					dp->val2 = response->StatusCode.ToString();
				}
				response->Close();
			}
			catch (WebException^ e)
			{
				dp->val2 = e->Status.ToString();
			}
			catch (Exception^ e)
			{
				dp->val2 = e->Message;
			}

			try
			{
				HttpWebRequest^ request = dynamic_cast<HttpWebRequest^>(WebRequest::Create("http://urnaweb.cz"));
				HttpWebResponse^ response = dynamic_cast<HttpWebResponse^>(request->GetResponse());
				if (response->StatusCode == HttpStatusCode::OK)
				{
					dp->val3 = response->StatusCode.ToString();
				}
				response->Close();
			}
			catch (WebException^ e)
			{
				dp->val3 = e->Status.ToString();
			}
			catch (Exception^ e)
			{
				dp->val3 = e->Message;
			}

			try
			{
				HttpWebRequest^ request = dynamic_cast<HttpWebRequest^>(WebRequest::Create("http://metrumservis.sk"));
				HttpWebResponse^ response = dynamic_cast<HttpWebResponse^>(request->GetResponse());
				if (response->StatusCode == HttpStatusCode::OK)
				{
					dp->val4 = response->StatusCode.ToString();
				}
				response->Close();
			}
			catch (WebException^ e)
			{
				dp->val4 = e->Status.ToString();
			}
			catch (Exception^ e)
			{
				dp->val4 = e->Message;
			}

			
			backgroundWorker1->ReportProgress(++cislo, dp);
		}
	}
when i edit code like this, then i see numbers like seconds, 1,2,3,4,5 .....


What go you want to see actually ?
Pages: 12