Tomer W
Tomer W

Reputation: 3433

WebHttpRequest fails (500) while postman successfully runs the web GET request

I try to get a specific value from a specific site...

the site periodically updating the value using an Ajax call to https://www.plus500.co.il/api/LiveData/FeedUpdate?instrumentId=19

(you can Navigate to the address and see you get the XML response.)

using Postman: sending

GET /api/LiveData/FeedUpdate?instrumentId=19 HTTP/1.1
Host: www.plus500.co.il
Cache-Control: no-cache
Postman-Token: f823c87d-3edc-68ce-e1e7-02a8fc68be7a

I get a valid Json Response...

Though, when i try it from C#:

var webRequest = WebRequest.CreateHttp(@"https://www.plus500.co.il/api/LiveData/FeedUpdate?instrumentId=19");
webRequest.Method = "GET";
using (var response = webRequest.GetResponse())
{...}

The request Fails with Error-Code 403 (Forbidden)

when adding:

webRequest.UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.84 Safari/537.36";

The request Fails with Error-Code 500 (Internal Server Error)

Addition (Edit)

I also initiate with

ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };
        ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 |
                                   SecurityProtocolType.Tls11 |
                                   SecurityProtocolType.Tls |
                                   SecurityProtocolType.Ssl3;

Also, I Tried Setting a CookieContainer, but the result is the same 500.

Why is Postman/Chrome Successfuly querying this API while C# Webrequest do not?
What is the difference?

Upvotes: 2

Views: 3368

Answers (1)

Gibbon
Gibbon

Reputation: 2773

So, the reason that this is failing is because of the headers being included in the client request from postman by default, though not from the C# request.

Using a program like Fiddler (https://www.telerik.com/fiddler) you can watch the request to see that the headers from the postman request are:

Accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8    
Accept-Encoding: gzip, deflate, br    
Accept-Language: en-US,en;q=0.9    
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.84 Safari/537.36

Yet from C# are just

User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36

Filling in the extra client request headers like this allows it to go through fine:

webRequest.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8";
webRequest.Headers.Add("Accept-Encoding", "gzip deflate,br");
webRequest.Headers.Add("Accept-Language", "en-US,en;q=0.9");

Upvotes: 1

Related Questions