• [C#]Winform后台提交数据且获取远程接口返回的XML数据,转换成DataSet


        #region 接口返回的Xml转换成DataSet
        /// <summary>
        /// 返回的Xml转换成DataSet
        /// </summary>
        /// <param name="text">Xml字符</param>
        /// <returns></returns>
        private DataSet GetDataSet(string text)
        {
            try
            {
                XmlTextReader reader = new XmlTextReader(new StringReader(text));
                reader.WhitespaceHandling = WhitespaceHandling.None;
                DataSet ds = new DataSet();
                ds.ReadXml(reader);
                reader.Close();
                ds.Dispose();
                return ds;
            }
            catch
            {
                return null;
            }
        }
        #endregion
    
    
        #region 后台提交数据且获取接口返回的数据
        /// <summary>  
        /// 后台提交数据且获取接口返回的数据 
        /// </summary>  
        /// <param name="relativePath">地址</param>  
        /// <returns></returns>  
        public static string GetRequestString(string relativePath)
        {
            string requestUrl = relativePath;
            try
            {
                // 创建一个HTTP请求
                HttpWebRequest request = (System.Net.HttpWebRequest)WebRequest.Create(requestUrl);
                request.Method = "GET";
    
                StreamReader jsonStream = new StreamReader(request.GetResponse().GetResponseStream());
                string jsonObject = jsonStream.ReadToEnd();
    
                return jsonObject;
            }
            catch
            {
                return string.Empty;
            }
        }
        #endregion  
    #region 提交Json数据且获取接口返回的数据    
    /// <summary>  
    /// 提交Json数据且获取接口返回的数据  
    /// </summary>  
    /// <param name="url">网址</param>  
    /// <param name="method">Get/Post</param>  
    /// <param name="postDataStr">提交数据{"name":"zhangsan","pwd":"123456"}</param>  
    /// <returns></returns>  
    private string HttpPost(string url, string method, string postDataStr)  
    {  
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);  
        request.Method = method;  
      
        if (!string.IsNullOrWhiteSpace(postDataStr))  
        {  
            request.ContentType = "application/json";  
            byte[] postData = Encoding.UTF8.GetBytes(postDataStr);  
            request.ContentLength = postData.Length;  
            System.IO.Stream outputStream = request.GetRequestStream();  
            outputStream.Write(postData, 0, postData.Length);  
            outputStream.Close();  
        }  
      
        HttpWebResponse response = (HttpWebResponse)request.GetResponse();  
        Stream myResponseStream = response.GetResponseStream();  
        StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8"));  
        string retString = myStreamReader.ReadToEnd();  
        myStreamReader.Close();  
        myResponseStream.Close();  
        return retString;  
    }  
    #endregion  

  • 相关阅读:
    Log4j2 打印日志实践
    解决docker容器启动不了,处于Exited问题
    dockerfile如何编写
    mysql(查询从句)
    线性表代码实现
    mysql(子查询)
    mysql(连表查询)
    mysql(基础命令)
    mysql安装
    mysql建表
  • 原文地址:https://www.cnblogs.com/smartsmile/p/6234208.html
Copyright © 2020-2023  润新知