远程服务器返回错误:“(405) 方法不允许”
本文关键字:方法 不允许 服务器 返回 错误 | 更新日期: 2023-09-27 17:59:14
在针对同一线程阅读了不同的答案后,我尝试了他们讨论中提到的几乎所有选项,但是我仍然收到错误:
错误 No1: The remote server returned an error: (404) Not Found.
OR
错误 No2:The remote server returned an error: (405) Method Not Allowed
。
下面是我的代码:
var httpWebRequest = (HttpWebRequest)WebRequest.Create("URL?Paramter1=pc&user=u1&password=p1");
httpWebRequest.ContentType = "application/json; charset=utf-8";
httpWebRequest.Method = "POST";
httpWebRequest.Accept = "application/json";
var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
我在最后一行收到错误,即
var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
如果我使用httpWebRequest.Method="GET"
则会收到上述错误号1,如果我使用httpWebRequest.Method="Post"
则会收到错误号2。
我也面临这个问题。此错误的根本原因是可能是 Web 配置文件中的端点地址错误。
这是我错误的网络配置
<services>
<service behaviorConfiguration="mexBehavior" name="sample.sample">
<endpoint address="http://xxx.xxx.x.xxx:9335" binding="basicHttpBinding" contract="sample.Isamplebase" />
<host>
<baseAddresses>
<add baseAddress="http://xxx.xxx.x.xxx:9335" />
</baseAddresses>
</host>
</service>
</services>
应该是这样的
<services>
<service behaviorConfiguration="mexBehavior" name="sample.sample">
<endpoint address="http://xxx.xxx.x.xxx:9335/sample.svc" binding="basicHttpBinding" contract="sample.Isamplebase" />
<host>
<baseAddresses>
<add baseAddress="http://xxx.xxx.x.xxx:9335" />
</baseAddresses>
</host>
</service>
</services>
现在它工作正常...最好的是你的..祝你好运。
使用:
[OperationContract]
[WebInvoke(Method = "POST", ResponseFormat = WebMessageFormat.Json, UriTemplate = "abcxx")]
在声明方法的服务页面上,以大写形式键入POST
。我认为您正在输入小写字母。
我敢打赌 Paramter1 或用户名或密码不正确 - 换句话说,您尝试使用 doid 的用户不存在。
服务器返回 404 未找到,不是因为您没有命中正确的 API 方法,而是因为您请求的对象不存在。
查看代码,创建 URL 并将数据作为编码的 url 表单传递,但随后将内容类型设置为 application/json
。我想你要么:
- 旨在使用
application/x-www-form-urlencoded
作为内容类型
或
- 意味着不发送 URL 中的数据,而是需要为您的 POST 设置 JSON 正文
我也遇到了这个问题。就我而言,原因是不正确(不完整(的URL。
我打电话给
60033/COMPANY_NAME/api/公司/MOM/v1.0/companies(5a56e3ea-cd63-ec11-b6ea-6045bd874170(/
但是你不能在一般的公司URL上做一个POST。
我应该使用URL不同的标题,这取决于我的目的
60033/COMPANY_NAME/api/Company/MOM/v1.0/companies(5a56e3ea-cd63-ec11-b6ea-6045bd874170(/receiptHeaders
当我修复URL错误消失时