2017-09-05 100 views
0

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

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 request with post作为一个来源(和其他一些主题),但我似乎无法弄清楚这个问题。

+0

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

回答

0

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