Hello all,
I'm still learning a lot with C#, and now come across a seamingly easy question, but can't seem to find the answer.
I'm trying to write a simpke class to send a web request and return the reply. This works, but the error handling doesn't.
I'm trying to figure out whether the server is down, whether the requested file is missing, if there is a problem with the requested file, and possibly if the wireless network (which the device is using) is currently no on the network.
So far my code looks a little like this:
public string webSend(string postData, string page)
{
string website = " + activeServer + "/" + activeFolder + page;
string reply = "";
try
{
WebRequest myWebRequest = WebRequest.Create(website);
myWebRequest.Method = "POST";
myWebRequest.Timeout = 10000;
myWebRequest.ContentType = "application/x-
byte[] byteArray = Encoding.UTF8.GetBytes(postData);
myWebRequest.ContentLength = byteArray.Length;
using (Stream datastream = myWebRequest.GetRequestStream())
{
datastream.Write(byteArray, 0, byteArray.Length);
}
WebResponse resp = myWebRequest.GetResponse();
StreamReader strm = new StreamReader(resp.GetResponseStream());
reply = strm.ReadToEnd();
strm.Close();
resp.Close();
}
catch (Exception ex)
{
MessageBox.Show("website : " + website);
MessageBox.Show("postData : " + postData);
MessageBox.Show("Webrequest : " + ex.ToString());
}
return reply;
}
As you can probably see, a pretty simply procedure, but currently can't distinguish between errors, etc...
Any ideas?
Thanks
Daint
I'm still learning a lot with C#, and now come across a seamingly easy question, but can't seem to find the answer.
I'm trying to write a simpke class to send a web request and return the reply. This works, but the error handling doesn't.
I'm trying to figure out whether the server is down, whether the requested file is missing, if there is a problem with the requested file, and possibly if the wireless network (which the device is using) is currently no on the network.
So far my code looks a little like this:
public string webSend(string postData, string page)
{
string website = " + activeServer + "/" + activeFolder + page;
string reply = "";
try
{
WebRequest myWebRequest = WebRequest.Create(website);
myWebRequest.Method = "POST";
myWebRequest.Timeout = 10000;
myWebRequest.ContentType = "application/x-
byte[] byteArray = Encoding.UTF8.GetBytes(postData);
myWebRequest.ContentLength = byteArray.Length;
using (Stream datastream = myWebRequest.GetRequestStream())
{
datastream.Write(byteArray, 0, byteArray.Length);
}
WebResponse resp = myWebRequest.GetResponse();
StreamReader strm = new StreamReader(resp.GetResponseStream());
reply = strm.ReadToEnd();
strm.Close();
resp.Close();
}
catch (Exception ex)
{
MessageBox.Show("website : " + website);
MessageBox.Show("postData : " + postData);
MessageBox.Show("Webrequest : " + ex.ToString());
}
return reply;
}
As you can probably see, a pretty simply procedure, but currently can't distinguish between errors, etc...
Any ideas?
Thanks
Daint