C#通讯API获得北京时间的方法

   private string GetBeijingTime()
        {
            ///获得标准的北京时间;
            ///
            // Creates an HttpWebRequest with the specified URL. 
            HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create("http://api.k780.com/?app=life.time&appkey=10003&sign=b59bc3ef6191eb9f747dd4e83c99f2a4&format=json"); //构建http request
            // Sends the HttpWebRequest and waits for the response.            
            HttpWebResponse myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse();
            // Gets the stream associated with the response.
            Stream receiveStream = myHttpWebResponse.GetResponseStream();
            Encoding encode = System.Text.Encoding.GetEncoding("utf-8");
            // Pipes the stream to a higher level stream reader with the required encoding format. 
            StreamReader readStream = new StreamReader(receiveStream, encode);

            Char[] read = new Char[256];
            // Reads 256 characters at a time.    
            int count = readStream.Read(read, 0, 256);

            while (count > 0)
            {
                // Dumps the 256 characters on a string and displays the string to the console.
                String strnow = new String(read, 0, count);
                count = readStream.Read(read, 0, 256);
                JObject jo = (JObject)JsonConvert.DeserializeObject(strnow);
                if (jo.ToString() != null)
                {
                    string zone = jo["result"]["datetime_1"].ToString();
                    //lb_DBWrite_DAY.Visible = true;
                    //lb_DBWrite_DAY.Text = zone;
                    BeijingTime = zone;
                }

            }

            TH.SetRichTextBoxADD(rtxtServerMsgList, BeijingTime + "\r\n");
            // Releases the resources of the response.
            myHttpWebResponse.Close();
            // Releases the resources of the Stream.

            return BeijingTime;

        }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值