google-code-export / open-webkit-sharp

Automatically exported from code.google.com/p/open-webkit-sharp
GNU Lesser General Public License v3.0
1 stars 0 forks source link

Fix for handling HTTP Errors #146

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
Currently open-webkit-sharp has no possibility to react on HTTP Status Errors 
manually. For example a page behind a basic authentification will simply not 
load. 

What steps will reproduce the problem?
1. Open a page behind a basic authentification

What is the expected output? What do you see instead?
Expected: The webbrowser fires an event or prompts for the credentials..

What version of the product are you using? On what operating system?
2.9

Original issue reported on code.google.com by browseem...@googlemail.com on 16 Oct 2012 at 12:21

GoogleCodeExporter commented 9 years ago
[deleted comment]
GoogleCodeExporter commented 9 years ago
To help with this issue one could add an http-error-event as follows:

Add to WebKitBrowserEvents.cs:

public delegate void HTTPErrorEventHandler(object sender, HTTPErrorEventArgs e);
public class HTTPErrorEventArgs : EventArgs
{
    public WebException WebException { get; internal set; }
}

-------------------------------------------
Add to WebKitBrowser.cs

public event HTTPErrorEventHandler HTTPErrorOccured = delegate { };

Add a RunWorkerComplete Event to 
frameLoadDelegate_DidStartProvisionalLoadForFrame()
BackgroundWorker bw = new BackgroundWorker();
bw.DoWork += new DoWorkEventHandler(bw_DoWork);
bw.RunWorkerCompleted += new 
RunWorkerCompletedEventHandler(bw_RunWorkerCompleted);

The event handler:
void bw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
    if (e.Result != null && e.Result.GetType().Equals(typeof(WebException)))
    {
        HTTPErrorOccured(this, new HTTPErrorEventArgs() { WebException = (WebException)e.Result });
    }
}

Change the Method "bw_DoWork" to catch WebExceptions:

void bw_DoWork(object sender, DoWorkEventArgs e)
{            
    HeadersAvailableEventArgs h = e.Argument as HeadersAvailableEventArgs;
    try
    {
        string Url = h.Url.ToString();
        WebRequest WebRequestObject = HttpWebRequest.Create(Url);
        WebResponse ResponseObject = WebRequestObject.GetResponse();

        List<Header> headers = new List<Header>();

        foreach (string HeaderKey in ResponseObject.Headers)
            headers.Add(new Header(HeaderKey, ResponseObject.Headers[HeaderKey]));

        ResponseObject.Close();

        h.Headers = headers.ToArray();
        HeadersAvailable(this, h);
    }
    catch (WebException ex) 
    {
        e.Result = ex;   
    }
}

Maybe you want to integrate this into the next release?

Original comment by browseem...@googlemail.com on 16 Oct 2012 at 1:03

GoogleCodeExporter commented 9 years ago
Code implemented in 3.0.1. Thanks for this contribution!

Original comment by tsumalis96@gmail.com on 10 Nov 2012 at 3:11