Neelam Prajapati
Neelam Prajapati

Reputation: 3802

405 method not found error when web api call from device

405 method not found error comes when i call web API from my xamarin mobile application on device or emulator.

web api runs successfully when i call from postmen.

This error comes for only POST Method.

anybody have idea?

please suggest.

EDIT:

public async Task Request(string url, string contents, HttpMethod methodType, string mediaStream = "application/json", bool isTokenNeeded = true)
{
    bool isSuccessRequest = true;
    string responseBodyAsText;

    try
    {
        if (!Utility.IsServiceAvailable)
        {
            OnError?.Invoke(new ErrorData
            {
                ErrorText = SystemMessages.OfflineServer
            });
            return;
        }

        HttpClientHandler handler = new HttpClientHandler();
        using (HttpClient httpClient = new HttpClient(handler))
        {
            HttpRequestMessage message = new HttpRequestMessage(methodType, url);

            if (methodType == HttpMethod.Post)
            {
                message.Headers.ExpectContinue = false;
                message.Content = new StringContent(contents);
                // message.Content.Headers.ContentLength = contents.Length;
                message.Content.Headers.ContentType = new MediaTypeHeaderValue(mediaStream);
            }

            // Add accesstoken to request header
            if (isTokenNeeded)
                httpClient.DefaultRequestHeaders.Add("Authorization", "Bearer " + ApplicationConfiguration.Token.AccessToken);

            httpClient.Timeout = new TimeSpan(0, 0, 10, 0, 0);

            HttpResponseMessage response = await httpClient.SendAsync(message);
            if (response.StatusCode == System.Net.HttpStatusCode.NotFound)
            {
                var error = new ErrorData() { ErrorText = SystemMessages.OfflineServer };
                OnError?.Invoke(error);
                return;
            }
            else if (response.StatusCode == System.Net.HttpStatusCode.BadRequest)
            {
                responseBodyAsText = response.Content.ReadAsStringAsync().Result;
                var error = JsonConvert.DeserializeObject<ErrorData>(responseBodyAsText);
                OnError?.Invoke(error);
                return;
            }
            else if (response.StatusCode == System.Net.HttpStatusCode.Unauthorized)
            {
                responseBodyAsText = response.Content.ReadAsStringAsync().Result;
                var error = JsonConvert.DeserializeObject<ErrorData>(responseBodyAsText);
                OnError?.Invoke(error);
                return;
            }
            else
            {
                response.EnsureSuccessStatusCode();
                responseBodyAsText = response.Content.ReadAsStringAsync().Result;
            }
        }
    }
    catch (HttpRequestException hre)
    {
        responseBodyAsText = "Exception : " + hre.Message;
        isSuccessRequest = false;
        OnError?.Invoke(new ErrorData
        {
            ErrorText = hre.Message
        });
    }
    catch (Exception ex)
    {
        responseBodyAsText = "Exception : " + ex.Message;
        isSuccessRequest = false;
        OnError?.Invoke(new ErrorData
        {
            ErrorText = ex.Message
        });
    }
}

Upvotes: 1

Views: 708

Answers (2)

Neelam Prajapati
Neelam Prajapati

Reputation: 3802

I changed the cookieless="AutoDetect" in web.config to cookieless="UseCookies" and the problem solved.

Upvotes: 3

Paul Kertscher
Paul Kertscher

Reputation: 9723

405 is a server response that informs you, that the method (POST, GET, PUT, ...) is not supported by the target resource

The method received in the request-line is known by the origin server but not supported by the target resource

(Source)

If your web API is configured correctly I suppose that you've called the API with the wrong method, e.g. POST when it should've been a GET or other way round.

Upvotes: 0

Related Questions