繁体   English   中英

远程服务器返回错误:“(405) Method Not Allowed”

[英]The remote server returned an error: "(405) Method Not Allowed"

在阅读了针对同一线程的不同答案后,我尝试了他们讨论中提到的几乎所有选项,但是我仍然遇到错误:

错误 No1The remote server returned an error: (404) Not Found. 或者

错误2: 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。

用:

[OperationContract]
[WebInvoke(Method = "POST", ResponseFormat = WebMessageFormat.Json, UriTemplate = "abcxx")]

在您声明方法的服务页面上,以大写形式键入POST 我认为您正在输入小写字母。

我也面临这个问题。 此错误的根本原因可能是 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>

现在它工作正常......最好的是你的......祝你好运。

我敢打赌 Paramter1 或用户名或密码不正确 - 换句话说,您尝试使用的用户不存在。

服务器返回404 Not Found ,不是因为您没有找到正确的 API 方法,而是因为您请求的对象不存在。

查看您的代码,您创建了 URL 并将数据作为 url 表单编码传递,但随后将内容类型设置为application/json 我认为你要么:

  1. 打算使用application/x-www-form-urlencoded作为内容类型

或者

  1. 意在不发送 URL 中的数据,而是需要为您的 POST 设置一个 JSON 正文

我也遇到过这个问题。 在我的情况下,原因是不正确(不完整)URL。

我打了电话

60033/COMPANY_NAME/api/Company/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 错误消失了

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM