Domanda

I am using FiddlerCore as my proxy framework.

When I am surfing the web to a non-existing site (such as http://asdasd) I get the fiddler generated page for dns lookup failure - [Fiddler] DNS lookup for "asdasd" failed. No such host is known

I want it to present the usual browser error, Is it possible?

È stato utile?

Soluzione

The first thing to understand is that the term "usual browser error" is misleading, since there are two scenarios in the browser.

  1. The user is directly connected to the web
  2. The user is behind a proxy of any sort (e.g. Fiddler, Squid, etc).

If the user is directly connected to the web, the browser itself generates various error pages when a DNS lookup or a connection fails.

If the browser is behind a proxy, the proxy must generate the error page and return it to the client. If it wanted, it could try to mimic the browser's error pages, but this would require different logic for each client.

You can, however, override FiddlerCore's default error responses with one of your own choosing.

First, attach a delegate:

FiddlerApplication.BeforeReturningError += FiddlerApplication_BeforeReturningError;

Then, add code to generate a new error response:

private void FiddlerApplication_BeforeReturningError(Session oSession) {
 if (oSession.bHasResponse) { 
  string sTitle = "Unable to load page";
  string sOriginalMessage = oSession.GetResponseBodyAsString().Trim().Replace("[Fiddler] ", String.Empty);
  oSession.oResponse["Content-Type"] = "text/html; charset=utf-8";
  oSession.oResponse["Cache-Control"] = "max-age=0, must-revalidate";
  string sEnhancedError = 
    String.Format("<!doctype html><html><head><title>{0}</title>\r\n<style>" +
    "body {{ background-color: #CCDDDD; font-family: sans-serif }}\r\npre {{ max-width:600px; white-space:pre-wrap;}}\r\n" +
    "</style></head>\r\n<body><h1>MyProxy - Page Unavailable</h1>The specified resource could not be loaded.<br /><pre>{1}</pre></body></html>", sTitle, sOriginalMessage);

    oSession.utilSetResponseBody(sEnhancedError);
  }
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top