I am doing a program to get information from web page. After running long time(several hours), I always get a exception "Unable to read data from the transport connection : An existing connection was forcibly closed by the remote host", but, my try...Catch... doesn't capture the exception lead to program crash.
My Code:
public string GetPage(string posturl, string encode, string postData, string CompanyName, string currentMainThreadName)
{
string contentResult = string.Empty;
string err = string.Empty;
int couterTry = 0;
Encoding encoding = System.Text.Encoding.GetEncoding(encode);
byte[] data = encoding.GetBytes(postData);
do
{
this.request = WebRequest.Create(posturl) as HttpWebRequest;
CookieContainer cookieContainer = new CookieContainer();
this.request.CookieContainer = cookieContainer;
this.request.AllowAutoRedirect = true;
this.request.Method = "POST";
this.request.Accept = "text/html, application/xhtml+xml, */*";
this.request.Headers.Add("Accept-Language", "en-US,en;q=0.8,zh-Hans-CN;q=0.5,zh-Hans;q=0.3");
this.request.KeepAlive = true;
this.request.ContentLength = data.Length;
this.request.ContentType = "application/x-www-form-urlencoded";
this.request.UserAgent = "Mozilla/5.0 (Windows NT 6.3; WOW64; Trident/7.0; rv:11.0) like Gecko";
Cookie ck = new Cookie("JSESSIONID", "074B29A53D2368D7C1ABC586821A8FA7");
ck.Domain = target.Host;
cookieContainer.Add(ck);
this.request.Timeout = 60000;
try
{
couterTry++;
this.outStream = request.GetRequestStream();
this.outStream.Write(data, 0, data.Length);
this.response = request.GetResponse() as HttpWebResponse;
if (this.response.StatusCode == HttpStatusCode.OK)
{
this.inStream = this.response.GetResponseStream();
this.SR = new StreamReader(this.inStream, encoding);
contentResult = this.SR.ReadToEnd();
lock (this.webLogWebLog)
{
this.webLogWebLog.Invoke((MethodInvoker)delegate()
{
this.webLogWebLog.AppendText(string.Format("{0}: Post: Get web page resource code success! Need to retrieve infor of :{1}.\r\n", currentMainThreadName, Name));
this.webLogWebLog.ScrollToCaret();
});
}
this.DisposeStream();
break;
}
else
{
this.DisposeStream();
contentResult = string.Empty;
lock (this.webLogWebLog)
{
this.webLogWebLog.Invoke((MethodInvoker)delegate()
{
this.webLogWebLog.AppendText(currentMainThreadName + ": Post: Connection fail: this.response.StatusCode:" + this.response.StatusCode + "\r\n");
if (couterTry < 10)
{
this.webLogWebLog.AppendText(string.Format("{0}: Post: Re-send Post request after 20 seconds ..., Name:{1} !!! times:{2}. \r\n", currentMainThreadName, Name, couterTry));
}
else
{
this.webLogWebLog.AppendText(string.Format("{0}: Post: Give up getting infor of Name:{1}, after trying 10 times !!! \r\n", currentMainThreadName, Name));
}
this.webLogWebLog.ScrollToCaret();
});
}
if (couterTry >= 10)
{
break;
}
}
}
catch (Exception ex)
{
this.DisposeStream();
contentResult = string.Empty;
err = ex.Message;
lock (this.webLogWebLog)
{
this.webLogWebLog.Invoke((MethodInvoker)delegate()
{
if (err.Contains("Thread was being aborted"))
{
this.webLogWebLog.AppendText(string.Format("---------{0}: was being aborted when send Post request to server! ---------Name{1} \r\n", currentMainThreadName, Name));
}
else
{
this.webLogWebLog.AppendText(currentMainThreadName + ":Post: --Exception--:" + err + "\r\n");
if (couterTry < 10)
{
this.webLogWebLog.AppendText(string.Format("{0}: Post: Re-send Post request after 20 seconds ..., Name:{1} !!! times:{2}. \r\n", currentMainThreadName, Name, couterTry));
}
else
{
this.webLogWebLog.AppendText(string.Format("{0}: Post: Give up getting infor of Name:{1} after trying 10 times !!! \r\n", currentMainThreadName, Name));
}
}
this.webLogWebLog.ScrollToCaret();
});
}
if (couterTry >= 10)
{
break;
}
}
Thread.Sleep(20000);
} while (true);
this.DisposeStream();
return contentResult;
}