HttpRequest 工具

1.根据 url 和 encoding 获取当前url页面的 html 源代码

  public static string GetHtml(string url, Encoding encoding)
        {
            HttpWebRequest request = null;
            HttpWebResponse response = null;
            StreamReader reader = null;
            try
            {
                ServicePointManager.Expect100Continue = true;
                ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls;
                request = (HttpWebRequest)WebRequest.Create(url);
                request.Method = "GET";
                response = (HttpWebResponse)request.GetResponse();
                if (response.StatusCode == HttpStatusCode.OK && response.ContentLength < 1024 * 1024)
                {
                    if (response.ContentEncoding != null && response.ContentEncoding.Equals("gzip", StringComparison.InvariantCultureIgnoreCase))
                        reader = new StreamReader(new GZipStream(response.GetResponseStream(), CompressionMode.Decompress), encoding);
                    else
                        reader = new StreamReader(response.GetResponseStream(), encoding);
                    string html = reader.ReadToEnd();

                    return html;
                }
            }
            catch (Exception ex)
            {
            }
            finally
            {

                if (response != null)
                {
                    response.Close();
                    response = null;
                }
                if (reader != null)
                    reader.Close();

                if (request != null)
                    request = null;
            }
            return string.Empty;
        }

2.获取URL访问的HTML内容

 public static string GetWebContent(string Url)
        {
            string strResult = "";
            try
            {
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);
                request.Timeout = 30000;
                request.Headers.Set("Pragma", "no-cache");

                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                Stream streamReceive = response.GetResponseStream();

                Encoding encoding = Encoding.GetEncoding("utf-8");
                StreamReader streamReader = new StreamReader(streamReceive, encoding);
                strResult = streamReader.ReadToEnd();
            }
            catch
            {

            }

            return strResult;
        }

3.根据网站url获取流

  public static Stream GetWebUrlStream(string Url)
        {
            Stream stream = null;
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            if (response.StatusCode == HttpStatusCode.OK)
            {
                stream = response.GetResponseStream();
            }
            return stream;
        }

4.根据发送数据获取url 流对象

 public static Stream GetWebUrlStream(string Url, string body)
        {
            Stream stream = null;
            Encoding code = Encoding.GetEncoding("utf-8");
            byte[] bytesRequestData = code.GetBytes(body);
            try
            {
                //设置HttpWebRequest基本信息
                HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(Url);
                request.Timeout = 30000;
                request.Method = "post";
                request.ContentType = "application/octet-stream";
                //填充POST数据
                request.ContentLength = bytesRequestData.Length;

                Stream reqStream = request.GetRequestStream();
                reqStream.Write(bytesRequestData, 0, bytesRequestData.Length);
                HttpWebResponse wr = (HttpWebResponse)request.GetResponse();
                if (wr.StatusCode == HttpStatusCode.OK)
                {
                    //在这里对接收到的页面内容进行处理
                    stream = wr.GetResponseStream();
                }
            }
            catch { }
            return stream;
        }

5.根据post 数据填充url 获取html对象

 public static string PostHtml(string Url, string body)
        {
            Encoding code = Encoding.GetEncoding("utf-8");
            byte[] bytesRequestData = code.GetBytes(body);
            string strResult = "";
            try
            {
                //设置HttpWebRequest基本信息
                HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(Url);
                request.Timeout = 30000;
                request.Method = "post";
                request.ContentType = "application/x-www-form-urlencoded; charset=UTF-8";
   
                //填充POST数据
                request.ContentLength = bytesRequestData.Length;

                using (Stream reqStream = request.GetRequestStream())
                {
                    reqStream.Write(bytesRequestData, 0, bytesRequestData.Length);
                }
                using (WebResponse wr = request.GetResponse())
                {
                    //在这里对接收到的页面内容进行处理
                    Stream myStream = wr.GetResponseStream();
                    //获取数据必须用UTF8格式
                    StreamReader sr = new StreamReader(myStream, code);
                    strResult = sr.ReadToEnd();
                }
            }
            catch { }
            return strResult;
        }

6.将本地文件上传到指定的服务器(HttpWebRequest方法)

 public static string PostUpData(string address, string fileNamePath, string saveName, string dataName)
        {
            string returnValue = "";
            FileStream fs = new FileStream(fileNamePath, FileMode.Open, FileAccess.Read);//时间戳
            string strBoundary = "----------" + DateTime.Now.Ticks.ToString("x");
            byte[] boundaryBytes = Encoding.ASCII.GetBytes("
--" + strBoundary + "
");
            //请求头部信息
            StringBuilder sb = new StringBuilder();
            sb.Append("--");
            sb.Append(strBoundary);
            sb.Append("
");
            sb.AppendFormat("Content-Disposition: form-data; name="{0}"; filename="{1}"", dataName, saveName);
            sb.Append("
");
            sb.Append("Content-Type: application/octet-stream");
            sb.Append("
");
            sb.Append("
");
            string strPostHeader = sb.ToString();
            byte[] postHeaderBytes = Encoding.UTF8.GetBytes(strPostHeader);
            //根据uri创建HttpWebRequest对象
            HttpWebRequest httpReq = (HttpWebRequest)WebRequest.Create(address);
            httpReq.Method = "POST";
            //对发送的数据不使用缓存
            httpReq.AllowWriteStreamBuffering = false;
            //设置获得响应的超时时间(300秒)
            httpReq.Timeout = 300000;
            httpReq.ContentType = "multipart/form-data; boundary=" + strBoundary;
            long length = fs.Length + postHeaderBytes.Length + boundaryBytes.Length;
            long fileLength = fs.Length;
            httpReq.ContentLength = length;
            try
            {
                Stream postStream = httpReq.GetRequestStream();
                //发送请求头部消息
                postStream.Write(postHeaderBytes, 0, postHeaderBytes.Length);

                //每次上传4k
                byte[] buffer = new Byte[checked((uint)Math.Min(4096, (int)fs.Length))];
                fs.Seek(0, SeekOrigin.Begin);
                int bytesRead = 0;
                while ((bytesRead = fs.Read(buffer, 0, buffer.Length)) != 0)
                    postStream.Write(buffer, 0, bytesRead);
                //添加尾部的时间戳
                postStream.Write(boundaryBytes, 0, boundaryBytes.Length);
                postStream.Close();
                //获取服务器端的响应
                WebResponse webRespon = httpReq.GetResponse();
                Stream s = webRespon.GetResponseStream();
                StreamReader sr = new StreamReader(s);
                //读取服务器端返回的消息
                returnValue = sr.ReadToEnd();
                s.Close();
                sr.Close();
            }
            catch
            {

            }
            finally
            {
                fs.Close();
            }
            return returnValue;
        }

7.将网址图片上传到指定的服务器(HttpWebRequest方法)

   public static string PostUpDataUrlImg(string address, string fileNamePath, string saveName, string dataName)
        {
            string returnValue = "";
            // 要上传的文件
            System.Drawing.Imaging.ImageFormat format = System.Drawing.Imaging.ImageFormat.Jpeg;
            Image original_image = Image.FromStream(GetWebUrlStream(fileNamePath));
            MemoryStream ms = new MemoryStream();
            original_image.Save(ms, format);
            original_image.Clone();
            //时间戳
            string strBoundary = "----------" + DateTime.Now.Ticks.ToString("x");
            byte[] boundaryBytes = Encoding.ASCII.GetBytes("
--" + strBoundary + "
");
            //请求头部信息
            StringBuilder sb = new StringBuilder();
            sb.Append("--");
            sb.Append(strBoundary);
            sb.Append("
");
            sb.AppendFormat("Content-Disposition: form-data; name="{0}"; filename="{1}"", dataName, saveName);
            sb.Append("
");
            sb.Append("Content-Type: application/octet-stream");
            sb.Append("
");
            sb.Append("
");
            string strPostHeader = sb.ToString();
            byte[] postHeaderBytes = Encoding.UTF8.GetBytes(strPostHeader);
            //根据uri创建HttpWebRequest对象
            HttpWebRequest httpReq = (HttpWebRequest)WebRequest.Create(address);
            httpReq.Method = "POST";
            //对发送的数据不使用缓存
            httpReq.AllowWriteStreamBuffering = false;
            //设置获得响应的超时时间(30秒)
            httpReq.Timeout = 30000;
            httpReq.ContentType = "multipart/form-data; boundary=" + strBoundary;
            long length = ms.Length + postHeaderBytes.Length + boundaryBytes.Length;
            long fileLength = ms.Length;
            httpReq.ContentLength = length;
            try
            {
                Stream postStream = httpReq.GetRequestStream();
                //发送请求头部消息
                postStream.Write(postHeaderBytes, 0, postHeaderBytes.Length);

                //每次上传4k
                byte[] buffer = new Byte[checked((uint)Math.Min(4096, (int)ms.Length))];
                ms.Seek(0, SeekOrigin.Begin);
                int bytesRead = 0;
                while ((bytesRead = ms.Read(buffer, 0, buffer.Length)) != 0)
                    postStream.Write(buffer, 0, bytesRead);
                //添加尾部的时间戳
                postStream.Write(boundaryBytes, 0, boundaryBytes.Length);
                postStream.Close();
                //获取服务器端的响应
                WebResponse webRespon = httpReq.GetResponse();
                Stream s = webRespon.GetResponseStream();
                StreamReader sr = new StreamReader(s);
                //读取服务器端返回的消息
                returnValue = sr.ReadToEnd();
                s.Close();
                sr.Close();
            }
            catch
            {

            }
            finally
            {
                ms.Close();
            }
            return returnValue;
        }

8.将网络地址文件上传到指定的服务器

  public static string PostHttpUpData(string address, string fileNamehttp, string saveName, string dataName)
        {
            string returnValue = "";
            WebClient wc = new WebClient();
            //网址文件下载
            byte[] downData = wc.DownloadData(fileNamehttp);
            wc.Dispose();
            //时间戳
            string strBoundary = "----------" + DateTime.Now.Ticks.ToString("x");
            byte[] boundaryBytes = Encoding.ASCII.GetBytes("
--" + strBoundary + "
");
            //请求头部信息
            StringBuilder sb = new StringBuilder();
            sb.Append("--");
            sb.Append(strBoundary);
            sb.Append("
");
            sb.AppendFormat("Content-Disposition: form-data; name="{0}"; filename="{1}"", dataName, saveName);
            sb.Append("
");
            sb.Append("Content-Type: application/octet-stream");
            sb.Append("
");
            sb.Append("
");
            string strPostHeader = sb.ToString();
            byte[] postHeaderBytes = Encoding.UTF8.GetBytes(strPostHeader);
            //根据uri创建HttpWebRequest对象
            HttpWebRequest httpReq = (HttpWebRequest)WebRequest.Create(address);
            httpReq.Method = "POST";
            //对发送的数据不使用缓存
            httpReq.AllowWriteStreamBuffering = false;
            //设置获得响应的超时时间(300秒)
            httpReq.Timeout = 300000;
            httpReq.ContentType = "multipart/form-data; boundary=" + strBoundary;
            long length = downData.Length + postHeaderBytes.Length + boundaryBytes.Length;
            httpReq.ContentLength = length;
            try
            {
                Stream postStream = httpReq.GetRequestStream();
                //发送请求头部消息
                postStream.Write(postHeaderBytes, 0, postHeaderBytes.Length);

                //发送内容消息
                postStream.Write(downData, 0, downData.Length);

                //添加尾部的时间戳
                postStream.Write(boundaryBytes, 0, boundaryBytes.Length);

                postStream.Close();
                //获取服务器端的响应
                WebResponse webRespon = httpReq.GetResponse();
                Stream s = webRespon.GetResponseStream();
                StreamReader sr = new StreamReader(s);
                //读取服务器端返回的消息
                returnValue = sr.ReadToEnd();
                s.Close();
                sr.Close();
            }
            catch { }
            return returnValue;
        }

9.将网址图片上传到指定的服务器(HttpWebRequest方法)

  public static string PostUpDataUrlImg(string address, string fileNamePath, string saveName, string dataName, string body)
        {
            string returnValue = "";
            Stream stream = GetWebUrlStream(fileNamePath, body);
            List<byte> bytes = new List<byte>();
            int temp = stream.ReadByte();
            while (temp != -1)
            {
                bytes.Add((byte)temp);
                temp = stream.ReadByte();
            }
            byte[] WebUrlStream = bytes.ToArray();//时间戳
            string strBoundary = "----------" + DateTime.Now.Ticks.ToString("x");
            byte[] boundaryBytes = Encoding.ASCII.GetBytes("
--" + strBoundary + "
");
            //请求头部信息
            StringBuilder sb = new StringBuilder();
            sb.Append("--");
            sb.Append(strBoundary);
            sb.Append("
");
            sb.AppendFormat("Content-Disposition: form-data; name="{0}"; filename="{1}"", dataName, saveName);
            sb.Append("
");
            sb.Append("Content-Type: application/octet-stream");
            sb.Append("
");
            sb.Append("
");
            string strPostHeader = sb.ToString();
            byte[] postHeaderBytes = Encoding.UTF8.GetBytes(strPostHeader);
            //根据uri创建HttpWebRequest对象
            HttpWebRequest httpReq = (HttpWebRequest)WebRequest.Create(address);
            httpReq.Method = "POST";
            //对发送的数据不使用缓存
            httpReq.AllowWriteStreamBuffering = false;
            //设置获得响应的超时时间(30秒)
            httpReq.Timeout = 30000;
            httpReq.ContentType = "multipart/form-data; boundary=" + strBoundary;
            long length = WebUrlStream.Length + postHeaderBytes.Length + boundaryBytes.Length;
            long fileLength = WebUrlStream.Length;
            httpReq.ContentLength = length;
            try
            {
                Stream postStream = httpReq.GetRequestStream();
                //发送请求头部消息
                postStream.Write(postHeaderBytes, 0, postHeaderBytes.Length);
                postStream.Write(WebUrlStream, 0, WebUrlStream.Length);
                //添加尾部的时间戳
                postStream.Write(boundaryBytes, 0, boundaryBytes.Length);
                postStream.Close();
                //获取服务器端的响应
                WebResponse webRespon = httpReq.GetResponse();
                Stream s = webRespon.GetResponseStream();
                StreamReader sr = new StreamReader(s);
                //读取服务器端返回的消息
                returnValue = sr.ReadToEnd();
                s.Close();
                sr.Close();
            }
            catch
            {

            }return returnValue;
        }
原文地址:https://www.cnblogs.com/linsu/p/4478396.html