从设备调用web api时,405方法未找到错误

时间:2017-12-14 06:20:46

标签: asp.net-web-api xamarin.forms

当我在设备或模拟器上从我的xamarin移动应用程序调用web API时,出现

405 method not found错误。

当我从邮递员那里打电话时,

web api成功运行。

此错误仅适用于POST方法。

有人有想法吗?

请建议。

编辑:

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

2 个答案:

答案 0 :(得分:3)

我将cookieless="AutoDetect"中的web.config更改为cookieless="UseCookies",问题就解决了。

答案 1 :(得分:0)

405是一个服务器响应,通知您目标资源不支持该方法(POSTGETPUT,...)

  

请求行中接收的方法由源服务器知道,但目标资源不支持

Source

如果您的网络API配置正确,我认为您使用错误的方法调用了API,例如POST它应该是GET或其他方式。