zoukankan      html  css  js  c++  java
  • winform c# 请求网站,返回Json字符串

     1  private void callApibjhb()
     2         {
     3              //输出执行的开始时间
     4             Console.WriteLine(string.Format("Bind {0}", DateTime.Now));
     5             Thread T = new Thread(() =>
     6          {  //表示不停刷新,如果不需要可以删除
     7                 while (true) {  
     8                   
     9                     string strURL = "";                 
    10                     try
    11              {
    12                System.GC.Collect();
    13                         string strURL =“你请求的网址”;
    14             System.Net.HttpWebRequest request;
    15                         // 创建一个HTTP请求
    16                         request = (System.Net.HttpWebRequest)WebRequest.Create(strURL);
    17                         request.Method = "GET";
    18                         request.Timeout = 600000;
    19                         request.ContentType = "application/json;charset=UTF-8";
    20                         request.ServicePoint.Expect100Continue = false;
    21                         request.KeepAlive = false;
    22                         System.Net.ServicePointManager.DefaultConnectionLimit = 50;
    23             System.Net.HttpWebResponse response;
    24                         response = (System.Net.HttpWebResponse)request.GetResponse();
    25                         Stream myResponseStream = response.GetResponseStream();
    26                         StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8"));
    27                         string retString = myStreamReader.ReadToEnd();
    28                         Console.WriteLine("ret string === {0}+  {1}", retString, strURL);
    29                         //创建RepParambj28hb类,字段格式是:根据“请求网站返回的Json字符串”,到网上搜“在线Json格式化”去解析
    30                         RepParambj28hb rep = JsonConvert.DeserializeObject<RepParambj28hb>(retString);
    31                         for (int i = 0; i < rep.result.lotterys.Length; i++)    
    32                {
    33                                      //根据自己的需求,得到值
    34                                     string jh = rep.result.lotterys[i].opencode.Replace(",", "");
    35                                     dt.Rows[dt.Rows.Count - 1]["cl1"] = rep.result.lotterys[i].expect;
    36                   }      
    37                   myStreamReader.Close();
    38                   myResponseStream.Close();
    39 
    40                     }
    41                     catch (Exception webEx)
    42                     {
    43                     }
    44                 }
    45             });
    46            
    47             T.Start();
    48            Console.WriteLine(string.Format("Bind end {0}", DateTime.Now));
    49         }

     

  • 相关阅读:
    vue 设置初始值 为null 进行文字长度判断
    js ajax传参 定义的参数和传的参数不一样
    js 删除字符串前几位字符,和删除最后一个字符
    js splice 删除数组 相邻的数据满足条件未被删掉
    js 将对象增加新键值对
    js 将数组添加key
    js 删除掉数组中的 ' '
    js 如何删除数组中指定对象及其值
    接口
    分支结构
  • 原文地址:https://www.cnblogs.com/bonnie-w/p/10207714.html
Copyright © 2011-2022 走看看