#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  



本文转载:CSDN博客