Quantcast
Channel: Recent Threads — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 204402

How to set up a proxy on the application level

$
0
0

Hi guys,

I'm trying to create a custom web browser using UIWebView control. All web requests must go through a custom proxy and I need to set this proxy from within the app. On the Internet you can find lots of ideas but unfortunately I can not find a good enough solution that works.

First I tried with the requests interception in ShouldStartLoad event and making a new request but this is not proven to be a good solution especially for URLs that require authentication. Currently I am trying to achieve the goal with NSUrlSesion (available in iOS 7.0 and later) but without success because the application from some reason doesn't use proxy settings:

NSUrlSession session;
UIWebView wvContent;

private NSUrlSession InitSession ()
{
    using (var configuration = NSUrlSessionConfiguration.DefaultSessionConfiguration) {

        NSObject[] values = new NSObject[]
        {
            NSObject.FromObject("xx.xx.xx.xx"),         //ProxyHost
            NSNumber.FromInt32 (xxxx),                  //Port
            NSObject.FromObject("kCFProxyTypeHTTPS")    //ProxyType
        };

        NSObject[] keys = new NSObject[]
        {
            NSObject.FromObject("kCFProxyHostNameKey"),
            NSObject.FromObject("kCFProxyPortNumberKey"),
            NSObject.FromObject("kCFProxyTypeKey")
        };

        NSDictionary proxyDict = NSDictionary.FromObjectsAndKeys (values, keys);
        configuration.ConnectionProxyDictionary = proxyDict;

        return NSUrlSession.FromConfiguration (configuration);
    }
}

    public override void ViewDidLoad()
    {
        base.ViewDidLoad();

        if (session == null)
        session = InitSession ();
    ...
        LoadDataInWebView(new NSUrl("some_url"));
    }

private void LoadDataInWebView(NSUrl UrlToLoad)
{
    try
    {
        NSUrlRequest requestObj = new NSUrlRequest(UrlToLoad);
        NSUrlSessionDataTask dataTask = session.CreateDataTask(requestObj, new NSUrlSessionResponse(
        (NSData data, NSUrlResponse response, NSError error) => {
            if (error == null)
            {
            InvokeOnMainThread(() => {
                wvContent.LoadData(data, response.MimeType, response.TextEncodingName, response.Url);
            });
            }
        }
        ));
        dataTask.Resume();
    }
    catch (Exception e)
    {
        new UIAlertView("Could not load page!", e.Message, null, "OK", null).Show();
    }
}

What am I doing wrong? I'm a beginner in iOS programming so maybe I don't understand well how this works.

Maybe someone has a totally different example that works? Thanks.


Viewing all articles
Browse latest Browse all 204402

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>