Graent.Hu 的博客

新浪微薄腾讯微薄

最新碎语:最近感觉有些迷茫,怎么办~

您的位置:Graent.Hu 的博客 >C#> c# 通过URl 获取返回的json格式数据

c# 通过URl 获取返回的json格式数据


protected string GetJson(string url)
    {
        //访问https需加上这句话 
       // ServicePointManager.ServerCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback(CheckValidationResult);
        //访问http(不需要加上面那句话) 
        WebClient wc = new WebClient();
        wc.Credentials = CredentialCache.DefaultCredentials;
        wc.Encoding = Encoding.UTF8;
        string returnText = wc.DownloadString(url);
 
        if (returnText.Contains("errcode"))
        {
            //可能发生错误 
        }
        //Response.Write(returnText); 
        return returnText;
    
② webservice



/// <summary>
       /// 通过Post方式获取数据
       /// </summary>
       /// <param name="url"></param>
       /// <param name="data">可为空</param>
       /// <returns></returns>
public string DoPost(string url, string data)
       {
           HttpWebRequest req = GetWebRequest(url, "POST");
           byte[] postData = Encoding.UTF8.GetBytes(data);
           Stream reqStream = req.GetRequestStream();
           reqStream.Write(postData, 0, postData.Length);
           reqStream.Close();
           HttpWebResponse rsp = (HttpWebResponse)req.GetResponse();
           Encoding encoding = Encoding.GetEncoding(rsp.CharacterSet);
           return GetResponseAsString(rsp, encoding);
       }
 
       public HttpWebRequest GetWebRequest(string url, string method)
       {
           HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
           req.ServicePoint.Expect100Continue = false;
           req.ContentType = "application/x-www-form-urlencoded;charset=utf-8";
           req.ContentType = "text/json";
           req.Method = method;
           req.KeepAlive = true;
           req.UserAgent = "guanyisoft";
           req.Timeout = 1000000;
           req.Proxy = null;
           return req;
       }
 
       public string GetResponseAsString(HttpWebResponse rsp, Encoding encoding)
       {
           StringBuilder result = new StringBuilder();
           Stream stream = null;
           StreamReader reader = null;
           try
           {
               // 以字符流的方式读取HTTP响应
               stream = rsp.GetResponseStream();
               reader = new StreamReader(stream, encoding);
               // 每次读取不大于256个字符,并写入字符串
               char[] buffer = new char[256];
               int readBytes = 0;
               while ((readBytes = reader.Read(buffer, 0, buffer.Length)) > 0)
               {
                   result.Append(buffer, 0, readBytes);
               }
           }
           finally
           {
               // 释放资源
               if (reader != null) reader.Close();
               if (stream != null) stream.Close();
               if (rsp != null) rsp.Close();
           }
 
           return result.ToString();
       }


我的微信:graent_hu

欢迎扫码加我和微信好友,有什么问题我们可以一起探讨,有什么需要也随时欢迎发消息给我~

转载请注明出处:

本文标题:c# 通过URl 获取返回的json格式数据

本文链接:https://www.wlyc.cn/post-97.html

发表评论

路人甲 表情
看不清楚?点图切换

网友评论(2)

感谢分享
加气块设备 2年前 (2017-07-05) 回复
@加气块设备::)
9ra3nt 2年前 (2017-07-07) 回复