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

client.PostAsync("Url", null) Gets 401 unauthorized error REST API

$
0
0

I am developing a cross mobile app using Xamarin and I am trying to post some items to a SharePoint List.

I have created a windows form app just to test the SharePoint Rest API and it is working good, I have the following code:

        var username = "user";
        var password = "pass";
        var domain = "domain";

        var handler = new HttpClientHandler
        {
            Credentials = new NetworkCredential(username, password, domain)
        };

        var client = new HttpClient(handler);

        HttpResponseMessage content = client.PostAsync("/mysite/_api/contextinfo", null).Result;

the content variable (HttpResponseMessage) returns me a 200 Status code, which is OK and everything is working.

However I have the same code in my Xamrin Froms but it is not working it throws me a 401 unauthorized error

this is my Xamarin Code

    private async void BtnRestApi_OnClicked(object sender, EventArgs e)
    {
         var username = "user";
         var password = "pass";
         var domain = "domain";

         var handler = new HttpClientHandler
         {
             Credentials = new NetworkCredential(username, password, domain)
         };

          var client = new HttpClient(handler);

         HttpResponseMessage content = await client.PostAsync("/mysite/_api/contextinfo", null).Result;
    } 

Viewing all articles
Browse latest Browse all 204402

Trending Articles



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