我可以将这个url输入到我的浏览器中,得到服务器时间https://api.binance.je/api/v3/time
但是我无法使用下面的代码得到响应。我如何调试它?
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();
}
}
}发布于 2020-02-16 01:20:17
我看到您能够从网站检索json数据。在我这边做了测试。但是,如果您只想获取值,则需要读取json字符串(responsefromServer)。这可以通过使用一个名为Newtonsoft的nuget包来完成。然后,您必须将以下内容添加到代码中。
以上命名空间:
using Newtonsoft.Json.Linq;在关闭阅读器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);您的代码将如下所示:
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();
}
}
}发布于 2020-02-16 21:14:21
问题出在我的vpn上。即使我没有一个活跃的连接。我关闭了后台服务并重新启动了它,现在正在工作
https://stackoverflow.com/questions/60241032
复制相似问题