0

i can past this url into my browser and get the server time, https://api.binance.je/api/v3/time

But i am unable to get a response using below code. How can i debug this

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            // Create a request for the URL.        
            WebRequest request = WebRequest.Create("https://api.binance.je/api/v3/time");
            // If required by the server, set the credentials.
            request.Credentials = CredentialCache.DefaultCredentials;
            // Get the response.
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            // Display the status.
            Console.WriteLine(response.StatusDescription);
            // Get the stream containing content returned by the server.
            Stream dataStream = response.GetResponseStream();
            // Open the stream using a StreamReader for easy access.
            StreamReader reader = new StreamReader(dataStream);
            // Read the content.
            string responseFromServer = reader.ReadToEnd();
            // Display the content.
            Console.WriteLine(responseFromServer);
            // Cleanup the streams and the response.
            reader.Close();
            dataStream.Close();
            response.Close();
        }
    }
}
user2202098
  • 830
  • 1
  • 9
  • 24
  • 1
    I am getting `serverTime 1581787122886` as the output. What are you seeing ? – Clint Feb 15 '20 at 17:18
  • 1
    This is working perfectly. `responseFromServer` has the response json which is received from api – as-if-i-code Feb 15 '20 at 17:21
  • @as.if.i.code i am getting a timeout exception. If it works for you perhaps there is something else wrong, though i should have got some response error as it works from my web broswer – user2202098 Feb 15 '20 at 19:10
  • @Clint i am getting a timeout exception. If it works for you perhaps there is an ip block or something else, though i should have got some response error – user2202098 Feb 15 '20 at 19:10
  • Ok it is now working, the only thing i have done is restart my VPN which didnt have an active connection and its working so :/ – user2202098 Feb 15 '20 at 19:55

2 Answers2

2

I see that you are able to retrieve the json data from the website. Tested this on my side. However if you are trying to get the value only you need to read the json string (responsefromServer). This can be done by using a nuget pakage called Newtonsoft. Then you will have to add the following to your code.

Above namespace:

using Newtonsoft.Json.Linq;

In main function before closing reader enz:

        //Create jsonObject object from the api call response
        JObject jObject = JObject.Parse(responseFromServer);
        //Read propertie called serverTime and convert this to string to match variabele set
        string time = jObject["serverTime"].ToString();
        //Write the given time
        Console.WriteLine(time);

Your code will look like the following:

using Newtonsoft.Json.Linq;
using System;
using System.IO;
using System.Net;

namespace StackOverflow
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");

            // Create a request for the URL.        
            WebRequest request = WebRequest.Create("https://api.binance.je/api/v3/time");
            // If required by the server, set the credentials.
            request.Credentials = CredentialCache.DefaultCredentials;
            // Get the response.
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            // Display the status.
            Console.WriteLine(response.StatusDescription);
            // Get the stream containing content returned by the server.
            Stream dataStream = response.GetResponseStream();
            // Open the stream using a StreamReader for easy access.
            StreamReader reader = new StreamReader(dataStream);
            // Read the content.
            string responseFromServer = reader.ReadToEnd();
            // Display the content.
            Console.WriteLine(responseFromServer);


            //Create jsonObject object from the api call response
            JObject jObject = JObject.Parse(responseFromServer);
            //Read propertie called serverTime and convert this to string to match variabele set
            string time = jObject["serverTime"].ToString();
            //Write the given time
            Console.WriteLine(time);


            // Cleanup the streams and the response.
            reader.Close();
            dataStream.Close();
            response.Close();
        }
    }
}
Jeremie de Vos
  • 174
  • 1
  • 11
  • i am not getting any response perhaps there is something else wrong my side – user2202098 Feb 15 '20 at 19:11
  • Ok it is now working, the only thing i have done is restart my VPN which didnt have an active connection and its working so :/ – user2202098 Feb 15 '20 at 19:55
  • ah, oke did not know that you where using a vpn. Glad it is working now! Happy coding! – Jeremie de Vos Feb 16 '20 at 08:32
  • 1
    weird as i said the vpn wasnt active but i restarted the service and confirmed it works. This user had exact same issue here https://stackoverflow.com/questions/52452455/httpclient-getasync-times-out-when-connected-to-vpn Thanks for your help anyway dude – user2202098 Feb 16 '20 at 13:12
0

The problem was down to my vpn. Even though i didnt have an active connection. I killed the background service and restarted it and is now working

Similar issue httpclient-getasync-times-out-when-connected-to-vpn

user2202098
  • 830
  • 1
  • 9
  • 24