马上加入IBC程序猿 各种源码随意下,各种教程随便看! 注册 每日签到 加入编程讨论群

C#教程 ASP.NET教程 C#视频教程程序源码享受不尽 C#技术求助 ASP.NET技术求助

【源码下载】 社群合作 申请版主 程序开发 【远程协助】 每天乐一乐 每日签到 【承接外包项目】 面试-葵花宝典下载

官方一群:

官方二群:

HTTP相关工具类/帮助类分享

[复制链接]
查看4475 | 回复2 | 2017-7-12 17:39:12 | 显示全部楼层 |阅读模式
HTTP上传、下载、post、get等工具类


[C#] 纯文本查看 复制代码
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;

namespace SunCreate.CombatPlatform.Server.ComLib
{
    /// <summary>
    /// Http上传下载文件
    /// </summary>
    public class HttpUtil
    {
        #region cookie设置
        private static CookieContainer m_Cookie = new CookieContainer();

        public static void SetHttpCookie(CookieContainer cookie)
        {
            m_Cookie = cookie;
        }
        #endregion

        #region HttpDownloadFile 下载文件
        public static MemoryStream HttpDownloadFile(string url)
        {
            // 设置参数
            HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
            request.Method = "GET";
            request.CookieContainer = m_Cookie;

            //发送请求并获取相应回应数据
            HttpWebResponse response = request.GetResponse() as HttpWebResponse;
            //直到request.GetResponse()程序才开始向目标网页发送Post请求
            Stream responseStream = response.GetResponseStream();

            //创建写入流
            MemoryStream stream = new MemoryStream();

            byte[] bArr = new byte[1024];
            int size = responseStream.Read(bArr, 0, (int)bArr.Length);
            while (size > 0)
            {
                stream.Write(bArr, 0, size);
                size = responseStream.Read(bArr, 0, (int)bArr.Length);
            }
            stream.Seek(0, SeekOrigin.Begin);
            responseStream.Close();
            return stream;
        }
        #endregion

        #region HttpUploadFile 上传文件
        /// <summary>
        /// Http上传文件
        /// </summary>
        public static string HttpUploadFile(string url, byte[] bArr, string fileName)
        {
            // 设置参数
            HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
            CookieContainer cookieContainer = new CookieContainer();
            request.CookieContainer = cookieContainer;
            request.AllowAutoRedirect = true;
            request.Method = "POST";
            string boundary = DateTime.Now.Ticks.ToString("X"); // 随机分隔线
            request.ContentType = "text/plain;charset=utf-8";
            request.CookieContainer = m_Cookie;

            Stream postStream = request.GetRequestStream();
            postStream.Write(bArr, 0, bArr.Length);
            postStream.Close();

            //发送请求并获取相应回应数据
            HttpWebResponse response = request.GetResponse() as HttpWebResponse;
            //直到request.GetResponse()程序才开始向目标网页发送Post请求
            Stream instream = response.GetResponseStream();
            StreamReader sr = new StreamReader(instream, Encoding.UTF8);
            //返回结果网页(html)代码
            string content = sr.ReadToEnd();
            return content;
        }
        #endregion

        #region HttpPost
        /// <summary>
        /// HttpPost
        /// </summary>
        public static string HttpPost(string url, string data)
        {
            byte[] bArr = ASCIIEncoding.UTF8.GetBytes(data);

            // 设置参数
            HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
            request.CookieContainer = m_Cookie;
            request.Method = "POST";
            request.ContentType = "text/plain;charset=utf-8";
            request.ContentLength = bArr.Length;

            Stream postStream = request.GetRequestStream();
            postStream.Write(bArr, 0, bArr.Length);
            postStream.Close();

            //发送请求并获取相应回应数据
            HttpWebResponse response = request.GetResponse() as HttpWebResponse;
            //直到request.GetResponse()程序才开始向目标网页发送Post请求
            Stream instream = response.GetResponseStream();
            StreamReader sr = new StreamReader(instream, Encoding.UTF8);
            //返回结果网页(html)代码
            string content = sr.ReadToEnd();
            return content;
        }
        #endregion

        #region HttpPost
        /// <summary>
        /// HttpPost
        /// </summary>
        public static string HttpPost(string url)
        {
            // 设置参数
            HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
            request.CookieContainer = m_Cookie;
            request.Method = "POST";
            request.ContentType = "text/plain;charset=utf-8";

            //发送请求并获取相应回应数据
            HttpWebResponse response = request.GetResponse() as HttpWebResponse;
            //直到request.GetResponse()程序才开始向目标网页发送Post请求
            Stream instream = response.GetResponseStream();
            StreamReader sr = new StreamReader(instream, Encoding.UTF8);
            //返回结果网页(html)代码
            string content = sr.ReadToEnd();
            return content;
        }
        #endregion

        #region HttpGet
        /// <summary>
        /// HttpGet
        /// </summary>
        public static string HttpGet(string url)
        {
            // 设置参数
            HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
            request.CookieContainer = m_Cookie;
            request.Method = "GET";
            request.ContentType = "text/plain;charset=utf-8";

            //发送请求并获取相应回应数据
            HttpWebResponse response = request.GetResponse() as HttpWebResponse;
            //直到request.GetResponse()程序才开始向目标网页发送Post请求
            Stream instream = response.GetResponseStream();
            StreamReader sr = new StreamReader(instream, Encoding.UTF8);
            //返回结果网页(html)代码
            string content = sr.ReadToEnd();
            return content;
        }
        #endregion

    }
}

C#论坛 www.ibcibc.com IBC编程社区
C#
C#论坛
IBC编程社区
jacksonwong | 2019-4-19 21:22:36 | 显示全部楼层
谢谢!
*滑块验证:
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则