Explore Courses Blog Tutorials Interview Questions
0 votes
in Salesforce by (11.9k points)

I have some existing code that was working and all-of-a-sudden quit.

I can't figure out why...

Here is my code:

public static string RequestToken(string u, string pw)


    string result = string.Empty;

    string strUrl = "" + u + "&password=" + pw;

    HttpWebRequest tokenRequest = WebRequest.Create(strUrl) as HttpWebRequest;


    tokenRequest.Method = "POST";



        using (HttpWebResponse tokenResponse = tokenRequest.GetResponse() as HttpWebResponse)


            if (tokenResponse.StatusCode != HttpStatusCode.OK)

                throw new Exception(String.Format(

                    "Server error (HTTP {0}: {1}).",



            DataContractJsonSerializer jsonSerializer2 = new DataContractJsonSerializer(typeof(ResponseAuthentication));

            object objTokenResponse = jsonSerializer2.ReadObject(tokenResponse.GetResponseStream());

            ResponseAuthentication jsonResponseAuthentication = objTokenResponse as ResponseAuthentication;

            result = jsonResponseAuthentication.strAccessToken;



    catch (Exception ex)




    return result;


I am now getting a 500 Internal Server Error where before this was working cleanly.

When I try to debug using Postman, I pass the URL directly and it works fine. Even when I put a stop in the code and use the exact same URL that then fails from inside the code, it works in Postman, but not in C#.

Out of Postman, I get an orderly...


  "access_token": "XXXXXX",

  "instance_url": "",

  "id": "",

  "token_type": "Bearer",

  "issued_at": "XXXXXX",

  "signature": "XXXXXX"


To clarify, I have tried a GET request instead of POST and I receive the following response (in Postman):


  "error": "invalid_request",

  "error_description": "must use HTTP POST"


Any ideas here?

1 Answer

0 votes
by (32.1k points)
edited by

Alright, so this turns out .NET 4.6 defaults to using TLS 1.2.

It was a very easy fix. I just added this one line of code and it quickly worked:

ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;

Hope that helps someone else with the same problem.

If you are planning to break into the Salesforce domain, and are not sure where to start, I would recommend you start by enrolling yourself in a Salesforce course from Intellipaat!

Related questions

Browse Categories