I have a RESTful Apex service that I can browse to and call using my web browser.

A problem occurs when I try to call it from C#:

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

Additional information: The remote server returned an error: (400) Bad Request.

This is my code:

using (var client = new WebClient())
    var url = $"https://x.force.com/activations/services/apexrest/sample/get?id=123";
    var json = client.DownloadString(url);

How can I call this RESTful service from C#? It works just fine from an unauthorized browser. The service itself returns a set of JSON which I would like to parse.

Edit: Just to clarify, my RESTful service is exposed as a force.com site and I can hit its controller methods from a fresh browser with a completely clean browsing history and get a response back (never having to type in my username or password or anything).

  • Comments are not for extended discussion; this conversation has been moved to chat.
    – Adrian Larson
    Commented May 25, 2017 at 16:12

2 Answers 2


You either need to authenticate yourself with one user credentials in Salesforce, and then you will be able to access the REST service. Otherwise, you will have to make the REST service available on a public site (force.com site), which will allow to access REST service, without any authentication.

You can refer to below link, where I recently gave an example of accessing the REST api.


Hope this helps :)

  • It is a public site, sorry I forgot to specify that. I don't get why its giving back a 400 error. I updated my question with an edit.
    – Alexandru
    Commented May 25, 2017 at 16:19
  • You may as well delete your answer :P Sorry about that
    – Alexandru
    Commented May 25, 2017 at 16:29

I figured it out. Jesus, not in all my years have I ever encountered this. I was using .NET Framework 4.0, which does not support TLS 1.2.

However, Salesforce decided to disable TLS 1.0 on July 22, 2017. I realize this is in the future from now, but I am using a Salesforce sandbox which must have it enabled, because once I updated the code to .NET 4.6, it worked immediately.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .