zoukankan      html  css  js  c++  java
  • [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  

  • 相关阅读:
    Java第一次作业
    第十一次
    第十次
    第九次
    第八次作业
    第七次
    第六次
    第五次作业
    ##JAVA作业3
    ##Java作业2
  • 原文地址:https://www.cnblogs.com/smartsmile/p/6234208.html
Copyright © 2011-2022 走看看