首页 新闻 赞助 找找看

System.Net的一个报错。

0
[待解决问题]
复制代码
static int i = 0;
        static int contectCount = 20;
        private static System.Timers.Timer aTimer;

        static void Main(string[] args)
        {
            SetTimer();
            Console.WriteLine("\nPress the Enter key to exit the application...\n");
            Console.WriteLine("The application started at {0:HH:mm:ss.fff}", DateTime.Now);
            Console.ReadLine();
            aTimer.Stop();
            aTimer.Dispose();

            Console.WriteLine("Terminating the application...");
        }

        private static void SetTimer()
        {
            // Create a timer with a two second interval.
            aTimer = new System.Timers.Timer(2000);
            // Hook up the Elapsed event for the timer. 
            aTimer.Elapsed += OnTimedEvent;
            aTimer.AutoReset = true;
            aTimer.Enabled = true;
        }


        static void OnTimedEvent(object source, System.Timers.ElapsedEventArgs e)
        {
            HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create("http://localhost:14238/HTMLPage.html");//这个是我本地的一个网页
            HttpWebResponse myHttpWebResponse;
            try
            {
                myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse();
                contectCount = 20;
                if (i == 0)
                {
                    Console.WriteLine("网络连接成功");
                    i++;
                }
            }
            catch (Exception ex)//这个地方会报错,操作超时的错误
            {
                --contectCount;
                if (contectCount == 0)
                {
                    Console.WriteLine("网络连接失败");
                }
                else if (contectCount > 0)
                {
                    Console.WriteLine(contectCount);
                    if(contectCount<10)//因为误报断开得错误,所以暂时用这个判断跳过  这个遇到得问题,只是我想知道这个错误到底怎么解决。
                    {
                        i = 0;
                    }
                }
            }
        }
复制代码

System.Net.WebException: 操作超时

是这个(HttpWebResponse)myHttpWebRequest.GetResponse();

方法本身的问题,还是,定时器抽搐弄得问题。

一首歌听到忘世的主页 一首歌听到忘世 | 初学一级 | 园豆:138
提问于:2018-07-04 16:50
< >
分享
所有回答(1)
0

一般http超时,先检查服务起来没有,手工能用postman或者其他httpclient调通,然后再看你写的调用代码,是不是写的对。

我印象里,本地用IIS Express起的服务不能用这样的方式调用,这个你用postman一试就知道了,因为IIS Express起的Web服务一般只用作本程序的调试,不开放别的程序(哪怕是同一个机器来调用)。

不管怎么说,先用httpclient试验。

rinson | 园豆:293 (菜鸟二级) | 2018-07-04 18:35
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册