Reputation: 15
im having a problem with catching an exception in proxy
private static async Task Check()
{
foreach (var cookieList in CookieList)
{
var user = JsonReader.readJSON("dataproxy.json", "proxyDetails", "proxyuser");
var pass = JsonReader.readJSON("dataproxy.json", "proxyDetails", "proxypass");
var proxyIp = JsonReader.readJSON("dataproxy.json", "proxyDetails", "proxyip");
var proxyPort = JsonReader.readJSON("dataproxy.json", "proxyDetails", "proxyport");
var container = new CookieContainer();
var handler = new HttpClientHandler();
handler.CookieContainer = container;
var client = new HttpClient(handler);
WebProxy proxy = new WebProxy(proxyIp,Convert.ToInt32(proxyPort));
if(user != null)
{
proxy.Credentials = new NetworkCredential(user, pass);
}
client.BaseAddress = BaseAddress; try
{
handler.Proxy = proxy;
}
catch
{
LogSystem.SendMessage("Proxy Error!", Log.Type.Message);
}
whenever a proxy is not working im getting this error:
any idea how to catch this error and let it retry if not then skip ?
Upvotes: 0
Views: 50
Reputation: 3696
The designers of .Net have considered these issues and provided us with an event called UnhandledExceptionEventHandler, through which we can intercept uncaught exceptions and process them. The event parameter UnhandledExceptionEventArgs e of this event has two attributes, one is ExceptionObject, and this attribute returns the object instance that intercepted the exception. Another property is IsTerminating, which tells us whether this exception will cause the application to terminate. Let's take a look at how Asp.net applications intercept uncaught exceptions.
First step:Create a class that implements the IHttpModule interface
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
namespace WebMonitor
{
/// <summary>
/// Summary description for UnhandledExceptionModule
/// </summary>
public class UnhandledExceptionModule : IHttpModule
{
static object _initLock = new object();
static bool _initialized = false;
public UnhandledExceptionModule()
{
//
// TODO: Add constructor logic here
//
}
void OnUnhandledException(object o, UnhandledExceptionEventArgs e)
{
// Do some thing you wish to do when the Unhandled Exception raised.
try
{
using (System.IO.FileStream fs = new System.IO.FileStream(@" c:/testme.log ",
System.IO.FileMode.Append, System.IO.FileAccess.Write))
{
using (System.IO.StreamWriter w = new System.IO.StreamWriter(fs, System.
Text.Encoding.UTF8))
{
w.WriteLine(e.ExceptionObject);
}
}
}
catch
{
}
}
IHttpModule Members
}
}
Step two:Modify web.config
In the system.web section add
< httpModules >
< add name ="UnhandledExceptionModule" type="WebMonitor.UnhandledExceptionModule" />
</ httpModules >
After completing these two steps, your ASP.NET application can intercept uncaught exceptions.
Here is the test code:
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void TestMe(object state)
{
byte[] buf = new byte[2];
buf[2] = 0;
}
protected void Button1_Click(object sender, EventArgs e)
{
System.Threading.ThreadPool.QueueUserWorkItem(new System.Threading.WaitCallback(TestMe),
null);
}
}
After pressing Button1, w3wp.exe is terminated, and the abnormal information is recorded in testme.log.
Upvotes: 1