标签归档:http

C# Http GET 和POST HttpClient HttpHelper

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
namespace alimama
{
    class HttpHelper
    {
        private static CookieContainer cookieContainer = new CookieContainer();
        private static int _timeOut = 1000 * 30; // 30秒超时  默认值是 100,000 毫秒(100 秒)
        static public string HttpGet(string url, string referer = "")
        {
            HttpWebRequest httpWebRequest = null;
            HttpWebResponse httpWebResponse = null;
            httpWebRequest = (HttpWebRequest)HttpWebRequest.Create(url);
            httpWebRequest.CookieContainer = cookieContainer;
            httpWebRequest.Method = "GET";
            httpWebRequest.ServicePoint.ConnectionLimit = int.MaxValue;
            if (!string.IsNullOrEmpty(referer))
            {
                httpWebRequest.Referer = referer;
            }
            //httpWebRequest.Host = Host;
            httpWebRequest.UserAgent = "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.63 Safari/537.36";
            httpWebRequest.KeepAlive = true;
            httpWebRequest.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
            httpWebRequest.ServicePoint.Expect100Continue = false;
            httpWebRequest.Timeout = _timeOut; //默认值是 100,000 毫秒(100 秒)
            httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();
            Stream responseStream = httpWebResponse.GetResponseStream();
            StreamReader streamReader = new StreamReader(responseStream, Encoding.UTF8);
            string html = streamReader.ReadToEnd();
            streamReader.Close();
            responseStream.Close();
            httpWebRequest.Abort();
            httpWebResponse.Close();
            return html;
        }
        static public string HttpPost(string url, string postData, string referer = "")
        {
            HttpWebRequest httpWebRequest = null;
            HttpWebResponse httpWebResponse = null;
            httpWebRequest = (HttpWebRequest)HttpWebRequest.Create(url);
            httpWebRequest.CookieContainer = cookieContainer;
            httpWebRequest.Method = "POST";
            httpWebRequest.ServicePoint.ConnectionLimit = int.MaxValue;
            if (!string.IsNullOrEmpty(referer))
            {
                httpWebRequest.Referer = referer;
            }
            //httpWebRequest.Host = Host;
            httpWebRequest.UserAgent = "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.63 Safari/537.36";
            httpWebRequest.KeepAlive = true;
            httpWebRequest.ContentType = "application/x-www-form-urlencoded; charset=UTF-8";
            httpWebRequest.ServicePoint.Expect100Continue = false;
            httpWebRequest.Timeout = _timeOut; //默认值是 100,000 毫秒(100 秒)
            byte[] byteArray = Encoding.UTF8.GetBytes(postData); //转化
            httpWebRequest.ContentLength = byteArray.Length;
            Stream newStream = httpWebRequest.GetRequestStream();
            newStream.Write(byteArray, 0, byteArray.Length); //写入参数
            newStream.Close();
            httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();
            Stream responseStream = httpWebResponse.GetResponseStream();
            StreamReader streamReader = new StreamReader(responseStream, Encoding.UTF8);
            string html = streamReader.ReadToEnd();
            streamReader.Close();
            responseStream.Close();
            httpWebRequest.Abort();
            httpWebResponse.Close();
            return html;
        }
        static public void Download(string url, string savePath)
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
            request.CookieContainer = cookieContainer;
            WebResponse response = request.GetResponse();
            Stream reader = response.GetResponseStream();
            FileStream writer = new FileStream(savePath, FileMode.OpenOrCreate, FileAccess.Write);
            byte[] buff = new byte[512];
            int c = 0; //实际读取的字节数
            while ((c = reader.Read(buff, 0, buff.Length)) > 0)
            {
                writer.Write(buff, 0, c);
            }
            writer.Close();
            writer.Dispose();
            reader.Close();
            reader.Dispose();
            response.Close();
        }
    }
}