C#向指定URL Post数据的帮助类

项目中用到了…在网上找到了一个,整理了一下,发出来

/*
 * By: 落落
 * URL: Www.MyLuoLuo.Com
 */ 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;
using System.Web;

namespace NetXenClient.Tools
{
    /// <summary>
    /// 向指定URL发送Post信息的帮助类
    /// </summary>
    public class WebPostHelp
    {
        /// <summary>
        /// 请求URL
        /// </summary>
        string url;
        /// <summary>
        /// 编码方式
        /// </summary>
        Encoding dataEncoding;
        /// <summary>
        /// 超时时间
        /// </summary>
        int timeout;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="url">需要要提交数据的URL</param>
        /// <param name="dataEncoding">编码方式</param>
        /// <param name="timeout">超时时间</param>
        public WebPostHelp(string url,Encoding dataEncoding, int timeout)
        {
            this.url = url;
            this.dataEncoding = dataEncoding;
            this.timeout = timeout;
        }

        /// <summary>
        /// Post数据
        /// </summary>
        /// <param name="postData">Post参数集合</param>
        /// <param name="cookies">用来接收Cookie信息</param>
        /// <returns></returns>
        public string Post(Dictionary<string, string> postData, ref CookieContainer cookies)
        {
            //创建对象
            HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url);
            //设置提交方式
            webRequest.Method = "POST";
            //是否保持连接
            webRequest.KeepAlive = true;
            // Content-TypeHTTP标头
            webRequest.ContentType = "application/x-www-form-urlencoded";
            //RefererHTTP 标头
            webRequest.Referer = url.Remove(url.LastIndexOf("/"));
            //设置连接超时的时间
            webRequest.Timeout = timeout;
            //设置Cookie容器
            if (cookies == null)
            {
                cookies = new CookieContainer();
            }
            webRequest.CookieContainer = cookies;
            if (postData != null && postData.Count > 0)
            {
                StringBuilder newStringBuilder = new StringBuilder();
                //转换Data数据
                foreach (KeyValuePair<string, string> kvp in postData)
                {
                    newStringBuilder.Append(HttpUtility.UrlEncode(kvp.Key));
                    newStringBuilder.Append("=");
                    newStringBuilder.Append(HttpUtility.UrlEncode(kvp.Value));
                    newStringBuilder.Append("&");
                }
                //转换数据
                byte[] byteData = this.dataEncoding.GetBytes(newStringBuilder.ToString().TrimEnd('&'));
                //设置数据长度
                webRequest.ContentLength = byteData.Length;
                using (Stream reqStream = webRequest.GetRequestStream())
                {
                    //写入
                    reqStream.Write(byteData, 0, byteData.Length);
                }
            }
            //用来接收返回的数据
            string ret = String.Empty;
            using (HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse())
            {
                cookies = webRequest.CookieContainer;
                using (Stream resStream = webResponse.GetResponseStream())
                {
                    using (StreamReader sr = new StreamReader(resStream, this.dataEncoding))
                    {
                        //读取返回 数据
                        ret = sr.ReadToEnd();
                    }
                }
            }
            return ret;
        }
    }
}

完整代码如上

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据