C#使用GET、POST请求获取结果 (二)

C#使用GET、POST请求获取结果。

创建HttpWebHelper.cs类

public class HttpWebHelper
{
    #region Post方式请求

    public static T Post(string url, string data, Dictionary Headers = null)
    {
        byte[] bytes = Encoding.UTF8.GetBytes(data);
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);

        //加入头信息
        if (Headers != null)
        {
            foreach (KeyValuePair item in Headers)
            {
                request.Headers.Add(item.Key, item.Value);
            }
        }

        //写数据
        request.Method = "POST";
        request.ContentLength = bytes.Length;
        request.ContentType = "application/json";
        Stream reqstream = request.GetRequestStream();
        reqstream.Write(bytes, 0, bytes.Length);

        //读数据
        request.Timeout = 300000;
        request.Headers.Set("Pragma", "no-cache");
        HttpWebResponse response = (HttpWebResponse)request.GetResponse();
        Stream streamReceive = response.GetResponseStream();
        StreamReader streamReader = new StreamReader(streamReceive, Encoding.UTF8);
        string strResult = streamReader.ReadToEnd();

        //关闭流
        reqstream.Close();
        streamReader.Close();
        streamReceive.Close();
        request.Abort();
        response.Close();

        return JsonConvert.DeserializeObject(strResult);
    }

    #endregion

    #region Get方式请求

    public static T Get(string url, Dictionary Headers = null)
    {
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);

        //加入头信息
        if (Headers != null)
        {
            foreach (KeyValuePair item in Headers)
            {
                request.Headers.Add(item.Key, item.Value);
            }
        }

        request.Method = "GET";
        request.ContentType = "application/json";
        request.Timeout = 300000;
        request.Headers.Set("Pragma", "no-cache");
        HttpWebResponse response = (HttpWebResponse)request.GetResponse();
        Stream streamReceive = response.GetResponseStream();
        StreamReader streamReader = new StreamReader(streamReceive, Encoding.UTF8);
        string strResult = streamReader.ReadToEnd();

        streamReader.Close();
        streamReceive.Close();
        request.Abort();
        response.Close();

        return JsonConvert.DeserializeObject(strResult);
    }

    public static string Get(string url, Dictionary Headers = null)
    {
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);

        //加入头信息
        if (Headers != null)
        {
            foreach (KeyValuePair item in Headers)
            {
                request.Headers.Add(item.Key, item.Value);
            }
        }

        request.Method = "GET";
        request.ContentType = "application/json";
        request.Timeout = 300000;
        request.Headers.Set("Pragma", "no-cache");
        HttpWebResponse response = (HttpWebResponse)request.GetResponse();
        Stream streamReceive = response.GetResponseStream();
        StreamReader streamReader = new StreamReader(streamReceive, Encoding.UTF8);
        string strResult = streamReader.ReadToEnd();

        streamReader.Close();
        streamReceive.Close();
        request.Abort();
        response.Close();

        return strResult;
    }
    #endregion

    #region 获取时间戳
    ///   
    /// 获取时间戳  
    ///   
    ///   
    public static string GetTimeStamp()
    {
        TimeSpan ts = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0);
        return Convert.ToInt64(ts.TotalMilliseconds).ToString();
    }
    #endregion
}

 

 

 

 

 

你可能感兴趣的:(C#/.NET编程,我の原创)