Home > Net >  Get Data of online API
Get Data of online API

Time:06-22

I want to download data of this website into a json file but as I am quite new to coding with C# I cant manage to get the data. I want to get Data of https://discosweb.esoc.esa.int/api/objects the authorization via token works but I dont know how I can send a request so the server gives me a json back and I cant find a solution online. I cant give you a screenshot of the API because you have to be logged in to see it. Plz ask me for detailed information if you can help me. Thank you realy for trying.

The code I want to run is here.

class Program
    {
        static HttpClient client = new HttpClient();
        static void Main(string[] args)
        {
            client.BaseAddress = new Uri("https://discosweb.esoc.esa.int");
            client.DefaultRequestHeaders.Accept.Clear();
            client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/vnd.api json"));
            client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("my_token");

            var httpRequest = (HttpWebRequest)WebRequest.Create(client.BaseAddress);

            var httpResponse = (HttpWebResponse)httpRequest.GetResponse();
            using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
            {
                var streamReaderResult = streamReader.ReadToEnd();
            }
            Console.WriteLine("Status https://discosweb.esoc.esa.int : "   httpResponse.StatusCode);
        }
}

CodePudding user response:

Try this

var url = "https://discosweb.esoc.esa.int/api/objects";
var httpRequest = (HttpWebRequest)WebRequest.Create(url);
httpRequest.Method = "POST";

httpRequest.Headers["Authorization"] = "Basic XXXx";
httpRequest.ContentType = "";
httpRequest.Headers["Content-Length"] = "0";


var httpResponse = (HttpWebResponse)httpRequest.GetResponse();
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
   var result = streamReader.ReadToEnd();
}

Console.WriteLine(httpResponse.StatusCode);

Where XXXx is user:password in base64.

CodePudding user response:

Here is a basic implementation for making that API call to get the JSON result. You will need to parse that JSON into something other than a string but I'll assume you can handle that part.

This uses System.Net.HttpClient which is the modern HTTP api provided by .NET. Its operations are async so hopefully your code is or can be written to properly await async operations.

//Someplace convenient, create a shared HttpClient to avoid
//creating and disposing for each request.
HttpClient client = new HttpClient();
string data = await GetObjects(client);


//Example implementation
public async Task<string> GetObjects(HttpClient client)
{
    string url = "https://discosweb.esoc.esa.int/api/objects";
    using (HttpRequestMessage msg = new HttpRequestMessage(HttpMethod.Get, url))
    {
        msg.Headers.Authorization =
            new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", "your personal access token here");

        using (var result = await client.SendAsync(msg))
        {
            string content = await result.Content.ReadAsStringAsync();

            return content;
        }
    }
}
  • Related