C# API中的Webclient POST 405错误
不久前我写了一些代码来处理C# API中的Webclient POST 405错误,c#,.net,api,post,webclient,C#,.net,Api,Post,Webclient,不久前我写了一些代码来处理POST请求。突然,它停止了工作,而我在API(postman仍然可以正常工作)和C代码中都没有做任何更改。但是当我运行代码时,我得到了一个405错误(不允许使用方法)。 登录方法: public byte[] logInViaAPI(string email, string password) { var response = APIHandler.Post("http://myurlhere", new NameValueCo
POST
请求。突然,它停止了工作,而我在API(postman仍然可以正常工作)和C代码中都没有做任何更改。但是当我运行代码时,我得到了一个405
错误(不允许使用方法)。
登录方法:
public byte[] logInViaAPI(string email, string password)
{
var response = APIHandler.Post("http://myurlhere", new NameValueCollection() {
{ "email", email },
{ "password", password },
});
return response;
}
这是我的发帖方法:
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”的未处理异常
System.dll
其他信息:
远程服务器返回错误:(405)方法不允许
我使用了HTTP作为源(以及其他一些主题),但我似乎无法解决问题。找到了我自己问题的答案:我从HTTP更改为HTTPS协议,同时仍然使用HTTP url。另一种可能的解决方案是使用SecurityProtocol。请在通话前尝试以下操作:
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
它开始失败,可能是由于服务器升级。确定问题的最佳方法是使用像wireshark或fiddler这样的嗅探器。使用postman捕获良好结果,并将http头与不工作的应用程序进行比较。使您的http头看起来像邮递员头。