Webclient POST 405 错误 API
Webclient POST 405 Error in API
我前一段时间写了一些代码来处理 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;
}
错误:
An unhandled exception of type 'System.Net.WebException' occurred in
System.dll
附加信息:
The remote server returned an error: (405) Method Not Allowed.
我使用 作为来源(以及其他一些主题),但我似乎无法弄清楚问题所在。
找到了我自己问题的答案:我将协议从 HTTP 更改为 HTTPS,同时仍在使用 HTTP url。
另一种可能的解决方案是使用 SecurityProtocol。在通话前试试这个:
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
我前一段时间写了一些代码来处理 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;
}
错误:
An unhandled exception of type 'System.Net.WebException' occurred in System.dll
附加信息:
The remote server returned an error: (405) Method Not Allowed.
我使用 作为来源(以及其他一些主题),但我似乎无法弄清楚问题所在。
找到了我自己问题的答案:我将协议从 HTTP 更改为 HTTPS,同时仍在使用 HTTP url。
另一种可能的解决方案是使用 SecurityProtocol。在通话前试试这个:
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;