Tomé Cerqueira
Tomé Cerqueira

Reputation: 39

403 Forbidden with WebClient

The company I work for have a limited internet connection and we use the proxy (example: 10.10.10.10:8080) to access at some restricted connections.

I can use the API in Postman (putting the proxy in the Postman settings) but when putting in the C# WebClient code it gives me a 403-Forbidden error.

I only need the var sensorData field but I splitted in var data and var data2 to understand where was the problem. It gives me the error at the var data = ...

Uri uri = new Uri("https://XXXXXXXX/api/DatasourceData/DatasourceDataHistoryBySerialNumber/");
Token token = new Token();
token = GetToken(tokenAPI);

using (WebClient client = new WebClient())
{
  try
  {
    client.Proxy = new WebProxy("10.10.10.10", 8080);
    client.Headers.Add("Authorization", "Bearer " + token.AccessToken);
    client.QueryString.Add("serialNumbersDatasource", "I2001258");
    client.QueryString.Add("startDate", string.Format("{0:s}", "2019-12-01"));
    client.QueryString.Add("endDate", string.Format("{0:s}", DateTime.Now));
    client.QueryString.Add("isFilterDatesByDataDate", "false");

    var data = client.DownloadData(uri);
    var data2 = (Encoding.UTF8.GetString(data));

    sensorData = (JsonConvert.DeserializeObject<List<Sensor>>(Encoding.UTF8.GetString(client.DownloadData(uri))))[0];
  }
}

Upvotes: 1

Views: 3746

Answers (6)

Trinitron
Trinitron

Reputation: 384

In my case i did not specify security protocol. Paste this line of code before running any WebClient requests.

ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;

original answer: How to specify SSL protocol to use for WebClient class

Upvotes: 0

Shaggy
Shaggy

Reputation: 5790

Based on this

Try adding User-Agent in the header

client.Headers.Add("User-Agent", "PostmanRuntime/7.26.1");

Upvotes: 0

logoutrd official
logoutrd official

Reputation: 1

I would suggest you to go for xNet.dll instead of webclient Because xNet library are considered best for proxy and webrequest.

var request = new HttpRequest()
request.UserAgent = Http.ChromeUserAgent();
request.Proxy = Socks5ProxyClient.Parse("10.10.10.10:8080");//can use socks4/5 http

Upvotes: 0

Jorg busch
Jorg busch

Reputation: 21

Probably a problem with authorization header. Is the token valid? Does it work with the same token in Postman?

I bet the api can't validate the token and and gives you no authorization to the resources. This is what a 403 would mean (but don't know what the api programmer actually intended by giving you 403).

Do you have access to the api's source code?

The token is really a string "tokentest" and that works with Postman?

Upvotes: 0

Murray Foxcroft
Murray Foxcroft

Reputation: 13745

Check to see if you need any additional properties on the proxy. You may possibly need to enable:

UseDefaultCredentials (Boolean) true if the default credentials are used; otherwise, false. The default value is false

Also, check your full url and query string that you are producing - look at the outgoing request fabricated (in the debugger) or through Fiddler and make sure it all lines up, url, query string, headers, etc.

From the docs:

Address Gets or sets the address of the proxy server.

BypassArrayList Gets a list of addresses that do not use the proxy server.

BypassList
Gets or sets an array of addresses that do not use the proxy server.

BypassProxyOnLocal
Gets or sets a value that indicates whether to bypass the proxy server for local addresses.

Credentials Gets or sets the credentials to submit to the proxy server for authentication.

UseDefaultCredentials
Gets or sets a Boolean value that controls whether the DefaultCredentials are sent with requests.

Upvotes: 0

Roman Marusyk
Roman Marusyk

Reputation: 24579

Seems the problem at this line

client.Headers.Add("Authorization", "Bearer " + "tokenTest");

here you wil add header Authorization with value Bearer tokenTest

so, 403 Forbidden returns by service which you are addressing, but not a proxy

change to

client.Headers.Add("Authorization", "Bearer " + tokenTest);

and check if tokenTest has valid value

Upvotes: 3

Related Questions