anaisbetts / ModernHttpClient

HttpClient implementations that use platform-native HTTP clients for :rocket:
MIT License
659 stars 260 forks source link

Can you clearly explain how to use Cookies with the latest version of ModernHttpClient? #172

Open ahmedkhan25 opened 9 years ago

ahmedkhan25 commented 9 years ago

Hi - Would like to see know what's the most up to date way that we can use cookies in ModernHttpClient in Xamarin (iOS/Android)? Documentation on this is sparse and seems to have changed:

Our Code:

We are using ModernHttpClient to access a web service which uses Forms Authentication so we have a 2-step process below to get the client to authenticate and then further use that authenticated cookie based instance to get data from the service

  1. In Xamarin.ios or Xamain.Android code, Create an instance of ModernHttpClient and use it to POST to the authentication page in the web service and get a cookie that contains a session token :
 //Code to Authenticate and get a cookie with session token:
            NativeCookieHandler cookieHandler = new NativeCookieHandler ();
            NativeMessageHandler messageHandler = new NativeMessageHandler (false, false, cookieHandler);
            ProjectClient = new HttpClient (messageHandler);

            string url = ServiceURL.DSISServiceEndpointUrl;
            Uri uri = new Uri (url);

            var keyValues = new List<KeyValuePair<string, string>> ();
            keyValues.Add (new KeyValuePair<string, string> ("loginForm=loginForm&j_username", username));
            keyValues.Add (new KeyValuePair<string, string> ("&j_password", username));

            HttpContent content = new FormUrlEncodedContent (keyValues);
            var response =  await ProjectClient.PostAsync (url, content);
            IEnumerable<Cookie> responseCookies = cookieHandler.Cookies;
            return responseCookies.FirstOrDefault ();  

2 - The previous code retrieves the proper cookie we want - But we can't seem to get the resulting calls to actually use it to get data- So the question is - How do we use or set that particular cookie in ModernHttpClient for subsequent GET requests? Should we create a new instance of the HttpClient and pass in the cookie?, should it not automatically use the NativeCookieHandler that we used to authenticate? -Please elaborate -

Thanks!!!

ahmedkhan25 commented 9 years ago

FYI.. In Xamarin Forums we can also see other users mention issues with using cookies and ModernHttpClient -e.g. this link:https://forums.xamarin.com/discussion/42410/bug-httpclient-times-out-when-redirected-on-a-post-request

ahmedkhan25 commented 9 years ago

FY2 - I was able to pass in the authentication cookie manually in the request header as described here https://github.com/paulcbetts/ModernHttpClient/issues/118

.. so this works for my code now -still it appears that cookie handling features are broken -either a fix or better documentation is appreciated

Btw - thanks for this library -its really very, very helpful for those of us who are using Xamarin!!