1
votes

I have created the simple login page in xamarin.forms,i have API for those logins,while running at postman iam getting the output,but while logging from the simulator iam getting the following error.

{StatusCode: 404, ReasonPhrase: 'Not Found', Version: 1.1, Content: System.Net.Http.StreamContent, Headers: { Cache-Control: private Server: Microsoft-IIS/8.0 X-AspNet-Version: 4.0.30319 X-Powered-By: ASP.NET Set-Cookie: ARRAffinity=639506ba4afdd530b4429c0d57e89977accb4b666a1e17dbe3fcc5c1fce369d5;Path=/;HttpOnly;Domain=snovahub.azurewebsites.net Date: Wed, 13 Sep 2017 13:23:00 GMT Content-Length: 3485 Content-Type: text/html; charset=utf-8 }}

My Api method is as follows:

#region Get results from api
    public static async Task<T> GetResultFromApi<T>(string serviceUrl,bool isTrue=true)
    {
        try
        {              
            GetConnection();

            var response = await _httpClient.GetAsync(new Uri(SnovaHubApiUrls.SnovaHubWebUrl + serviceUrl));

            var stringAsync = await response.Content.ReadAsStringAsync();

            if (response.IsSuccessStatusCode)
            {
                var responseJson = stringAsync;

                return JsonConvert.DeserializeObject<T>(responseJson);
            }

            LoggingManager.Error("Received error response: " + stringAsync);
            return default(T);
        }
        catch (Exception exception)
        {
            LoggingManager.Error(exception);
            return default(T);
        }
    }
#endregion
1
What gets passed in for serviceUrl? Also, do you change the URL based on a DEBUG vs RELEASE build? Also, what does GetConnection() do? - hvaughan3
@hvaughan3,email Id and password are the parameters passed through service url and private static void GetConnection() { if (_httpClient == null) { _httpClient = new HttpClient { BaseAddress = new Uri(SnovaHubApiUrls.SnovaHubWebUrl) }; } } for getting connetion this GetConnection(). - sahithi

1 Answers

1
votes

The issue is that you are setting the HttpClient.BaseAddress and then also passing in a full URL when calling HttpClient.GetAsync(). You need to choose one or the other. So:

Option 1:

private static void GetConnection() {
    if (_httpClient == null) {
         _httpClient = new HttpClient { BaseAddress = new Uri(SnovaHubApiUrls.SnovaHubWebUrl) }; //You MUST place a / (slash) at the end of your BaseAddress ("http://something.com/api/" for example)
    }
}

Then in your GetResultFromApi() method:

...

var response = await _httpClient.GetAsync(serviceUrl); //You MUST NOT place a slash at the beginning of 'serviceUrl' when using BaseAddress

Option 2:

private static void GetConnection() {
    if (_httpClient == null) {
         _httpClient = new HttpClient(); //Removed BaseAddress
    }
}

Then in your GetResultFromApi() method:

...

var response = await _httpClient.GetAsync(new Uri(SnovaHubApiUrls.SnovaHubWebUrl + serviceUrl)); //Passing full URL