【问题标题】:Webclient POST 405 Error in APIAPI 中的 Webclient POST 405 错误
【发布时间】:2017-09-05 16:35:15
【问题描述】:

不久前我写了一些代码来处理POST 请求。突然它停止工作,而我在 API(它仍然可以与邮递员一起工作)和 C# 代码中没有任何更改。但是当我运行我的代码时,我得到一个405 错误(方法不允许)。 登录方式:

public byte[] logInViaAPI(string email, string password)
        {
            var response = APIHandler.Post("http://myurlhere", new NameValueCollection() {
                {   "email", email          },
                {   "password", password    },
            });
            return response;
        }

这是我的 POST 方法:

public static byte[] Post(string uri, NameValueCollection pairs)
        {
            byte[] response = null;
            try
            {
                using (WebClient client = new WebClient())
                {

                    response = client.UploadValues(uri, pairs); //This is where I get my error
                }
            }
            catch (WebException ex)
            {
                Console.Write(ex);
                return null;
            }
            return response;
        }

错误:

“System.Net.WebException”类型的未处理异常发生在 系统.dll

补充资料:

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

我使用HTTP request with post 作为来源(以及其他一些主题),但我似乎无法找出问题所在。

【问题讨论】:

  • 它开始失败可能是由于服务器上的升级。确定问题的最佳方法是使用像wireshark或fiddler这样的嗅探器。使用邮递员捕获良好的结果,并将 http 标头与您的应用程序不工作进行比较。让您的 http 标头看起来像邮递员标头。

标签: c# .net api post webclient


【解决方案1】:

找到了我自己问题的答案:我从 HTTP 将协议更改为 HTTPS,同时仍在使用 HTTP url。

【讨论】:

    【解决方案2】:

    另一个可能的解决方案是使用 SecurityProtocol。在通话前试试这个:

    ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
    

    【讨论】:

      猜你喜欢
      • 2019-01-24
      • 1970-01-01
      • 2011-04-10
      • 2011-12-10
      • 1970-01-01
      • 1970-01-01
      • 2015-11-07
      • 1970-01-01
      • 2018-06-29
      相关资源
      最近更新 更多