[C++ CLI] Get response from website

Pages: 12
when i use classic httprequest and response with try and catch, it works, i can make infinite loop with while(true) and with Thread::Sleep(10000) refresh this statuscode every 10 seconds if some webpage is down or some error appear. but when i use my function(its same with try catch but have return values to String^), then my loop go only one time and stuch on second run on line with first function. (first run is done correctly, but second never appear). Where in function i have problem please ? :/

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;
	}
}
1
2
3
4
if (response->StatusCode == HttpStatusCode::OK)
{
  return response->StatusCode.ToString();
}


What happens if response->StatusCode != HttpStatusCode::OK

Why don't you use atimer instead of loop and sleep ?
if statuscode isn't OK, then httpwebresponse throw an exception and i catch this with try/catch (it works, i try it, if i dont make try catch, then program throw error and crash if code is not 200). i can use timer(first i must check how it works), but still dont know, why my function stuck on second run and when i use same code (but change return to dp->val1 for example), then it works like a charm.
It seems that HttpWebResponse throws an exception if the statuscode is 400, but not for other statuscodes.
http://stackoverflow.com/questions/692342/net-httpwebrequest-getresponse-raises-exception-when-http-status-code-400-ba

Try this:
1
2
3
4
5
6
7
8
if (response->StatusCode == HttpStatusCode::OK)
{
  return response->StatusCode.ToString();
}
else
{
   return "Problem with website: " + response->StatusCode.ToString();
}
Topic archived. No new replies allowed.
Pages: 12