3

I am fetching WEB.API from xamarin forms application.Web API created in .Net Web API. But i am getting error

{StatusCode: 403, ReasonPhrase: 'ModSecurity Action', Version: 1.1, Content: System.Net.Http.StreamContent, Headers:
{
Server: Microsoft-IIS/8.5
X-Powered-By: ASP.NET
X-Powered-By-Plesk: PleskWin
Date: Sat, 02 Sep 2017 07:59:38 GMT
Content-Type: text/html
Content-Length: 2684
}}

Same URL is giving proper output from Browser and postman also. I am using below code to fetch web api. I am not getting exactly what happening.

string RestUrl = "http://msystemtest.msystem.co/api/Users?userId={0}&pass={1}";

            var uri = new Uri(string.Format(RestUrl, uid, pass));
            string resp = string.Empty;

            var response = await client.GetAsync(uri);

            string statHold = string.Empty;
            if(response.IsSuccessStatusCode)
            {
                var content = await response.Content.ReadAsStringAsync();
                 resp = JsonConvert.DeserializeObject<string>(content);
            }

If "http://msystemtest.msystem.co/api/Users?userId=test&pass=1" this URL directly run from browser and from postman its giving proper output.

2
  • Have you checked the uri result? Maybe the format has wrong input parameters Commented Sep 2, 2017 at 8:11
  • Format is right. I have tested URI result directly. Its working fine. Commented Sep 2, 2017 at 8:13

1 Answer 1

2

Seems like the server does not like you reading this from a non browser. Here is code to emulate a browser from a console application:

using System;
using System.Net.Http;
namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            using (var httpClient = new HttpClient())
            {
                httpClient.DefaultRequestHeaders.TryAddWithoutValidation("Accept", "application/json");
                httpClient.DefaultRequestHeaders.TryAddWithoutValidation("User-Agent", "Mozilla/5.0 (Windows NT 6.2; WOW64; rv:19.0) Gecko/20100101 Firefox/19.0");

                var response = httpClient.GetAsync("http://msystemtest.msystem.co/api/Users?userId=test&pass=1").Result;
                Console.WriteLine(response.Content.ReadAsStringAsync().Result);
                Console.ReadKey();
            }
        }
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

Its working fine from console application. But when I try to implement from Xamarin Forms application it is giving same status.
Try using fiddler to check that the Xamarin Forms application does not overwrite the user-agent heading
Its working fine by slight changes in code. Thanks for your assist.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.