1

I've written some code a while back that handles POST requests. Suddenly it stopped working whilst I changed nothing in either the API (It still works fine with postman) nor the C# code. But I get a 405 error (method not allowed) when I run my code. The login method:

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

This is my POST method:

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;
        }

The error:

An unhandled exception of type 'System.Net.WebException' occurred in System.dll

Additional information:

The remote server returned an error: (405) Method Not Allowed.

I used HTTP request with post as a source (and some other topics too) but I cant seem to figure out the problem.

Hemi81
  • 578
  • 2
  • 15
  • 34
Freek W.
  • 406
  • 5
  • 20
  • It started to fail probably due to an upgrade on the server. Best method os determine the issue is to use a sniffer like wireshark or fiddler. Capture good results with postman and compare http headers with your application that is not working. Make you http headers look like postman headers. – jdweng Sep 05 '17 at 16:41

2 Answers2

1

Found the answer to my own question: I changed to protocol to HTTPS from HTTP, whilst still using the HTTP url.

Freek W.
  • 406
  • 5
  • 20
0

Another possible solution is the use SecurityProtocol. Try this before the call:

ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;