【发布时间】: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