天天看点

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

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

创建HttpWebHelper.cs类

public class HttpWebHelper
{
    #region Post方式请求

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

        //加入头信息
        if (Headers != null)
        {
            foreach (KeyValuePair<string, string> 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<T>(strResult);
    }

    #endregion

    #region Get方式请求

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

        //加入头信息
        if (Headers != null)
        {
            foreach (KeyValuePair<string, string> 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<T>(strResult);
    }

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

        //加入头信息
        if (Headers != null)
        {
            foreach (KeyValuePair<string, string> 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 获取时间戳
    /// <summary>  
    /// 获取时间戳  
    /// </summary>  
    /// <returns></returns>  
    public static string GetTimeStamp()
    {
        TimeSpan ts = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0);
        return Convert.ToInt64(ts.TotalMilliseconds).ToString();
    }
    #endregion
}