[英].NET , The remote server returned an error: (405) Method Not Allowed
I used the following code to call a web service written in C# and hosted in IIS, but it returns an error我使用以下代码调用了一个用 C# 编写并托管在 IIS 中的 Web 服务,但它返回一个错误
Remote server returned an error: (405) Method not enabled
远程服务器返回错误:(405) 方法未启用
But when I use Postman with the same input info, it works fine.但是当我使用具有相同输入信息的 Postman 时,它工作正常。
var httpWebRequest = (HttpWebRequest)WebRequest.Create(url_to_post);
httpWebRequest.Method = "POST";
httpWebRequest.ContentType = "application/json; charset=utf-8";
httpWebRequest.KeepAlive = true;
using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
{
streamWriter.Write(json_object);
streamWriter.Flush();
}
var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
var result = streamReader.ReadToEnd();
// return result;
}
First of all, please capture the request fiddler and check its response server.首先,请捕获请求提琴手并检查其响应服务器。
We need to figure out where the 405 come from.我们需要弄清楚405来自哪里。 If the response server is HTTP API/2.0 then it must come from http.sys.
如果响应服务器是 HTTP API/2.0,那么它必须来自 http.sys。 You may need to check your request URL.
您可能需要检查您的请求 URL。
However, if the response server is IIS.但是,如果响应服务器是 IIS。 Then please enable failed request tracing.
然后请启用失败的请求跟踪。 Most of time, IIS will return 405 error because either IIS use the wrong handler or your correct handler didn't allow POST method.
大多数情况下,IIS 将返回 405 错误,因为 IIS 使用了错误的处理程序或您的正确处理程序不允许 POST 方法。
So please enable failed request tracing and check what module/handler is returning 405 error.所以请启用失败的请求跟踪并检查哪个模块/处理程序返回 405 错误。
https://docs.microsoft.com/en-us/iis/troubleshoot/using-failed-request-tracing/troubleshooting-failed-requests-using-tracing-in-iis https://docs.microsoft.com/en-us/iis/troubleshoot/using-failed-request-tracing/troubleshooting-failed-requests-using-tracing-in-iis
Please ensure WebDAV handler is not handling the request.请确保 WebDAV 处理程序未处理请求。 And Static content feature has been installed.
并且已安装静态内容功能。 You need to ensure correct handler is handling the request and the handler also allow the POST method.
您需要确保正确的处理程序正在处理请求,并且处理程序还允许 POST 方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.