I have tried to connect to a server with HttpWebRequest & HttpWebResponse and it works fine, but I got another problem I want to know when the server have been time out or disconnected, suppose something happened to my connection and I got disconnected I want to know how can I understand this in the following code:
string uri = @"myUrl";
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
request.Credentials = new NetworkCredential(User, Pass);
ServicePointManager.ServerCertificateValidationCallback = ((sender, certificate, chain, sslPolicyErrors) => true);
byte[] buf = new byte[10000];
int count = -1;
String read = "";
HttpWebResponse response;
//MessageBox.Show("OK");
//response = (HttpWebResponse)request.GetResponse();
//count = response.GetResponseStream().Read(buf, 0, buf.Length);
//read = Encoding.UTF8.GetString(buf, 0, count);
//MessageBox.Show(read + "SALAM");
//while (true)
//{
response = (HttpWebResponse)request.GetResponse();
//while (true)
//{
do
{
count = response.GetResponseStream().Read(buf, 0, buf.Length);
read += Encoding.UTF8.GetString(buf, 0, count);
} while (response.GetResponseStream().CanRead && count != 0);
if (read != "")
{
// MessageBox.Show(read);
XDocument xdoc = XDocument.Parse(read);
//Filter EventXML
var lv1s = from lv1 in xdoc.Descendants("event")
select new
{
Event_id = lv1.Attribute("id").Value,
Header = lv1.Attribute("name").Value,
Children = lv1.Descendants("argument")
};
List<event_details> event_detail = new List<event_details>();
foreach (var lv1 in lv1s)
{
if (lv1.Event_id == event_id)
foreach (var lv2 in lv1.Children)
{
event_details x = new event_details();
x.type = lv2.Attribute("type").Value;
x.value = lv2.Attribute("value").Value;
event_detail.Add(x);
}
}
//inja chun ke daram rooye MsgDGV ke ye k Datagridview minevisam bayad hatman az Invoke estefade konam
// ta kharabkari nashe:P:D
Point detail_point = new Point();
detail_point.X = MsgDGV.Width / 2 + (this.Width - MsgDGV.Width) / 2;
detail_point.Y = MsgDGV.Height / 2 + (this.Height - MsgDGV.Height) / 2;
Details detail = new Details(event_detail, timestamp, EVENT, detail_point);
detail.ShowDialog();
event_details.Abort();
}