Raoul Gerbi Raoul Gerbi - 9 months ago 37
C# Question

WebRequests in C# to a REST API server

I am totally new to WebRequests. I need to implement several WebRequests to receive json files from a server. From the server I know that:

Authentication:


Authentication of each request is done via a token. The system administrator will provide you with
your token, which must be used with each request. The token should be kept secure (like a
password) as it gives anyone in it's possession with the full access that the associated user would
have.


Headers


Standard HTTP headers may be used to facilitate connection keep-alive, caching, compression etc.
All requests require the following headers

Accept: application/json

Authorization: < authentication token >


I searched for examples and I found a WebRequest implementation which I tried:

public string LoadHttpPageWithBasicAuthentication(string url, string username, string password)
{
Uri myUri = new Uri(url);
WebRequest myWebRequest = HttpWebRequest.Create(myUri);

HttpWebRequest myHttpWebRequest = (HttpWebRequest)myWebRequest;
myHttpWebRequest.Method = "GET";
myHttpWebRequest.ContentType = ".json";

NetworkCredential myNetworkCredential = new NetworkCredential(username, password);

CredentialCache myCredentialCache = new CredentialCache();
myCredentialCache.Add(myUri, "Basic", myNetworkCredential);

myHttpWebRequest.PreAuthenticate = true;
myHttpWebRequest.Credentials = myCredentialCache;

WebResponse myWebResponse = myWebRequest.GetResponse();

Stream responseStream = myWebResponse.GetResponseStream();

StreamReader myStreamReader = new StreamReader(responseStream, Encoding.Default);

string pageContent = myStreamReader.ReadToEnd();

responseStream.Close();

myWebResponse.Close();

return pageContent;
}


And I call it by

string ret=Request.LoadHttpPageWithBasicAuthentication(@"https://epica.ecninside.ecn.nl/lots", "aaa", "ccc");
File.WriteAllText("c:\\MyTransfer\\content.html", ret);


Unfortunately, my content is always the normal login page instead of any usefull data. If I use the browser, this login page appears as well at first. After login, I receive the data. But how can I handle the whole procedure by my client in c#?

Answer Source

A few things here.

Are you downloading a JSON file, or is the server actually sending you JSON directly? Either way, your content type is wrong. If the server is responding with JSON, your content type should be application/json. If it's actually sending you a text file with JSON in it, your content type should be text/plain.

Next, their documentation says each request needs an accept header. So you need to add a line:

myHttpWebRequest.Accept = "application/json";

Finally, they say each request needs a token to be sent along with it. I suspect that's why your request is returning the login page, instead of the data you're suspecting. A token is usually used in place of a user/pass, so it's doubtful that HTTP basic authentication would work in this case. I'd start by trying this:

myHttpWebRequest.Headers.Add("Authorization", "your_token");