Akash Rekalwar
Akash Rekalwar

Reputation: 85

Getting 403 status while fetching data from ASP.Net Web Api

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.

Upvotes: 3

Views: 2004

Answers (1)

devzero
devzero

Reputation: 2680

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();
            }
        }
    }
}

Upvotes: 2

Related Questions