【问题标题】:The remote server returned an error: "(405) Method Not Allowed"远程服务器返回错误:“(405) Method Not Allowed”
【发布时间】:2014-04-22 08:08:31
【问题描述】:

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

错误号 1The remote server returned an error: (404) Not Found.

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

【问题讨论】:

  • 你从哪里返回 json?
  • 你能说得更具体些吗... ???我从服务器返回它(当然)
  • 我想你不知道调用方法是post还是get?编辑:你知道网络调用的参数是什么吗?
  • 是的,但是返回方法看起来如何?不太清楚你的意思,但是当我从我的控制器返回 json 时,我需要添加一个 JsonRequestBehaviour.AllowGet 作为我返回结果的一部分。
  • @kostasch。事实上我确实知道它的帖子..但是很多其他线程说我们应该在这里和那里更改它以查看它是否有效..这就是为什么我已经在我的问题中提到它,否则它的“帖子”你可以在我的代码中.

标签: c#


【解决方案1】:

用途:

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

在您声明方法的服务页面上,输入大写的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>
    

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

    【讨论】:

      【解决方案3】:

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

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

      【讨论】:

        【解决方案4】:

        查看您的代码,您创建了 URL 并将数据作为 url 格式编码传递,但随后您将内容类型设置为 application/json。我想你也可以:

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

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

        【讨论】:

          猜你喜欢
          • 2014-08-22
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-10-14
          • 2016-12-02
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多