【问题标题】:HttpException Request Time outHttpException 请求超时
【发布时间】:2020-05-31 02:54:31
【问题描述】:

我可以通过这个网址进入我的浏览器并获取服务器时间,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();
        }
    }
}

【问题讨论】:

  • 我得到serverTime 1581787122886 作为输出。你在看什么?
  • 这是完美的工作。 responseFromServer 具有从 api 接收到的响应 json
  • @as.if.i.code 我收到超时异常。如果它对你有用,也许还有其他问题,虽然我应该得到一些响应错误,因为它可以从我的网络浏览器工作
  • @Clint 我遇到了超时异常。如果它对你有用,也许有一个 ip 块或其他东西,虽然我应该得到一些响应错误
  • 好的,它现在正在工作,我唯一要做的就是重新启动我的 VPN,它没有活动连接并且它正在工作:/

标签: c# asp.net .net


【解决方案1】:

问题出在我的 vpn 上。即使我没有活跃的连接。我杀死了后台服务并重新启动它,现在正在工作

类似的问题 httpclient-getasync-times-out-when-connected-to-vpn

【讨论】:

    【解决方案2】:

    我看到您可以从网站上检索 json 数据。在我这边测试了这个。 但是,如果您尝试获取该值,则只需读取 json 字符串(responsefromServer)。这可以通过使用名为 Newtonsoft 的 nuget 包来完成。 然后您必须将以下内容添加到您的代码中。

    命名空间之上:

    using Newtonsoft.Json.Linq;
    

    在关闭阅读器enz之前的main函数中:

            //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();
            }
        }
    }
    

    【讨论】:

    • 我没有得到任何回应,也许我身边还有其他问题
    • 好的,它现在正在工作,我唯一要做的就是重新启动我的 VPN,它没有活动连接并且它正在工作:/
    • 啊,ok不知道你在哪里使用vpn。很高兴它现在正在工作!编码愉快!
    • 很奇怪,因为我说 vpn 没有激活,但我重新启动了服务并确认它可以工作。这个用户在这里遇到了完全相同的问题stackoverflow.com/questions/52452455/…无论如何,谢谢你的帮助,伙计
    猜你喜欢
    • 2014-12-30
    • 2017-04-30
    • 1970-01-01
    • 2011-02-26
    • 2018-08-06
    • 1970-01-01
    • 2016-02-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多