I have tried various C# codes approaches for connecting to an API but cannot so far get it to work. The URL works every time with a Chrome browser. I have looked at many post here on Stack Overflow but have not found answers the problem.
In C# I have tried webclient
, httpclient
, no luck. With postman I get the data easily. I have tried to watch the request with Fiddler 4. The postman request works great and is easy to see but when running the C# code the request never shows up despite https traffic gets captured and decoded. Many times it is timeout or connecting was closed. I do not believe it to be a firewall issue. Here is some C# that does not work:
internal class Program
{
static void Main(string[] args)
{
Getit();
}
private static async void Getit()
{
try
{
string baseUrl = "https://api.nasdaq.com/api/nordic/instruments/CSE32679/trades?type=INTRADAY&assetClass=SHARES&lang=en";
var client = new System.Net.Http.HttpClient();
//var request = new System.Net.Http.HttpRequestMessage(System.Net.Http.HttpMethod.Get, baseUrl);
var request = new HttpRequestMessage
{
Method = HttpMethod.Get,
RequestUri = new Uri(baseUrl),
Headers = {
{ HttpRequestHeader.UserAgent.ToString(), "Could try to set this" },
{ HttpRequestHeader.Accept.ToString(), "application/json" },
{ HttpRequestHeader.Cookie.ToString(), "ak_bmsc=057AA34F94542D7FBF06A239807E1BC8~000000000000000000000000000000~YAAQHmjdWE1nYjiTAQAASfEPZRmj3xtTz7eQGDtn2GEgSGhkx070psq3nqpvIhA3wzaXGJm2o91q1M3D6DvQwqUZzuw+esKY13f8GdqUt4dJPxrgvfliIS8ehkC7YBSn7IH6Bcw/8s8hOjaP8i3WszFF0UMpQ4EaBthZep9yFYRYELgRMKXnMyzQBUsdAPhaoZIB9An1hH3LmRhIIx9zkwRt4TCY3O2eBOYzaHyo95pTmhp9SSWzHUM6RCj5+owjXtoHDlSjIHRmCVwtpNp20gn5b70Dvvb4C6tpdNUWvK8YJFbs6M5h+0ho0Z0SfGND59oPOaMSA5/i8wRKfiV/fl7ZtQtyQ8RQDxcMxA==; akaalb_ALB_Default=~op=ao_api_port_8085:ao_api_east2|~rv=23~m=ao_api_east2:0|~os=ff51b6e767de05e2054c5c99e232919a~id=ad3d95899a893885df1dfabdf47494ac" },
{ "X-Version", "1" }
},
};
var response = await client.SendAsync(request);
response.EnsureSuccessStatusCode();
Console.WriteLine(response.Content.ReadAsStringAsync());
}
catch (Exception e)
{
}
}
}
Can someone help me by showing some C# code that will do it?
2
Answers
You’re going to kick yourself.
You need an
await
statement in front ofresponse.Content.ReadAsStringAsync()
The entry for the User Agent header – not sure about the .ToString() approach, but verbatim the key needs to be "User-Agent" with a dash.
Other than that, download Fiddler Classic to view your web traffic. It’s the best! https://www.telerik.com/fiddler
Your
Getit()
is async but yourMain
isn’t. That means, when callingGetit()
the control flow Returns toMain
when the firstawait
is encountered. And as there is nothing else to do inMain
the program exits. The .net runtime does not wait for still running async tasks.Depending on your version of .net the simplest solution is to make your
Main
also async and put an await wherever you need one.Furthermore it’s bad practice to return
void
from an async method, because then you can’t await it. Return aTask
insteadAnd finally, you have also to await the
ReadStringAsAsync