Skip to content
This repository has been archived by the owner on Apr 10, 2018. It is now read-only.

Proxy property is not working on iOS devices #57

Closed
zevsst opened this issue Mar 22, 2016 · 7 comments
Closed

Proxy property is not working on iOS devices #57

zevsst opened this issue Mar 22, 2016 · 7 comments

Comments

@zevsst
Copy link

zevsst commented Mar 22, 2016

Hello.

Please have a look at the bug description and one of the possible solutions in the comment section.
http://stackoverflow.com/questions/24340570/restsharp-portable-restclient-proxy-property-is-not-working/36165221#36165221

Thanks.

@fubar-coder
Copy link
Contributor

Thank you, I'll take a look at this problem. Sadly, the main reason why I added the Proxy property access using reflection, was, that the property should be available according to the PCL contract, but it was missing when used on a Xamarin supported platform. I'll take a look if this workaround can be removed now.

@zevsst
Copy link
Author

zevsst commented Mar 22, 2016

Not at all, man.

I could not set Proxy property of HttpClientHandler in my implementation directly. Only the solution contains reflection works good for me. While I was trying to set Proxy directly I caught an exception which said that there was no setter for property Proxy. I guess it might be happened only for iOS platform. So I decided to set private field "proxy" by reflection.

Maybe below code will help you.

public virtual async Task PreAuthenticate(IHttpClient client, IHttpRequestMessage request, ICredentials credentials)
    {
        await Task.Yield();

        // Below is a fix with proxy for iOS devices
        var defaultHttpClient = client as DefaultHttpClient;

        var httpClient = defaultHttpClient?.Client;
        if (httpClient != null)
        {
            var httpClientType = httpClient.GetType();
            var handlerFieldInfo = httpClientType.GetField("handler", BindingFlags.NonPublic | BindingFlags.FlattenHierarchy);

            var handler = handlerFieldInfo?.GetValue(httpClient) as HttpClientHandler;
            if (handler != null && handler.SupportsProxy)
            {
                var handlerType = handler.GetType();

                var proxyFieldInfo = handlerType.GetField("proxy", BindingFlags.NonPublic);
                if (proxyFieldInfo != null)
                {
                    if (proxyFieldInfo.FieldType == typeof(IWebProxy))
                    {
                        var proxy = proxyFieldInfo.GetValue(handler);
                        if (proxy == null && webProxy != null && !proxyFieldInfo.IsInitOnly)
                        {
                            proxyFieldInfo.SetValue(handler, webProxy);
                        }
                    }
                }
            }
        }
    }

@fubar-coder
Copy link
Contributor

Fixed in 3.1.1

@zevsst
Copy link
Author

zevsst commented Mar 24, 2016

The problem still exists.

fubar-coder added a commit that referenced this issue Mar 24, 2016
@fubar-coder
Copy link
Contributor

Please try 3.1.2. It's difficult for me to test, because I don't have an iOS device/emulator ATM

@zevsst
Copy link
Author

zevsst commented Mar 24, 2016

I have tested 3.1.2 and the problem still exists. Please see my thoughts here

fubar-coder added a commit that referenced this issue Mar 24, 2016
…it should fix the problem according to zevsst. We now use the USE_TYPEINFO variant for iOS.
@adamfisher
Copy link

adamfisher commented Jun 1, 2016

I am still seeing this in 3.3.0 using the latest version of Xamarin at the time of writing. I am currently testing iOS and getting proxy is missing. I am attempting to integrate a NativeMessageHandler from a third party library.

Method 'HttpClientHandler.set_Proxy' not found.

Using:
Xamarin.iOS
Version: 9.6.2.4
Build date: 2016-05-05 17:43:01-0400

Xamarin.Android
Version: 6.0.4.0

protected override HttpMessageHandler CreateMessageHandler(IRestClient client, IRestRequest request)
        {
            var proxy = GetProxy(client);
            var cookies = GetCookies(client, request);
            var credentials = client.Credentials;
            var httpClientHandler = new NativeMessageHandler();

            if (httpClientHandler.SupportsProxy && proxy != null)
            {
                httpClientHandler.UseProxy = true;
                httpClientHandler.Proxy = new RequestProxyWrapper(proxy);
            }

            if (cookies != null)
            {
                httpClientHandler.UseCookies = true;
                httpClientHandler.CookieContainer = cookies;
            }

            if (credentials != null)
            {
                httpClientHandler.Credentials = credentials;
            }

            return httpClientHandler;
        }

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants