Exemplo n.º 1
0
        /// <summary>
        /// 创建GET方式的HTTP请求
        /// </summary>
        /// <param name="url">请求的URL</param>
        /// <param name="timeout">请求的超时时间</param>
        /// <param name="userAgent">请求的客户端浏览器信息,可以为空</param>
        /// <param name="cookies">随同HTTP请求发送的Cookie信息,如果不需要身份验证可以为空</param>
        /// <returns></returns>
        public static HttpWebResponse CreateGetHttpResponse(string url, PostHead header)
        {
            if (string.IsNullOrEmpty(url))
            {
                throw new ArgumentNullException("url");
            }
            HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;

            request.Method = "GET";
            header.FillInfo(request);
            return(request.GetResponse() as HttpWebResponse);
        }
Exemplo n.º 2
0
        /// <summary>
        /// 发送数据
        /// </summary>
        /// <param name="request">发送请求</param>
        /// <param name="prms">发送数据的字段和值</param>
        /// <param name="files">要发送的文件</param>
        /// <returns></returns>
        public static void PostData(HttpWebRequest request, PostHead head, IDictionary <string, string> prms, IEnumerable <FormFile> files)
        {
            String BOUNDARY = "---------" + CommonMethods.GuidToString(Guid.NewGuid()); //数据分隔线

            request.CachePolicy = new RequestCachePolicy(RequestCacheLevel.NoCacheNoStore);
            request.Method      = "POST";

            head.FillInfo(request);
            request.ContentType = MULTIPART_FORM_DATA + "; boundary=" + BOUNDARY;
            StringBuilder sb = new StringBuilder();

            //上传的表单参数部分,格式请参考文章
            if (prms != null)
            {
                foreach (KeyValuePair <string, string> entry in prms)
                {//构建表单字段内容
                    sb.Append("--");
                    sb.Append(BOUNDARY);
                    sb.Append("\r\n");
                    sb.Append("Content-Disposition: form-data; name=\"" + entry.Key + "\"\r\n\r\n");
                    sb.Append(entry.Value);
                    sb.Append("\r\n");
                }
            }
            Encoding useEncoding = head.PageEncoding;


            using (Stream outStream = request.GetRequestStream())
            {
                byte[] send = useEncoding.GetBytes(sb.ToString());
                WriteData(outStream, send);
                if (files != null)
                {
                    //上传的文件部分,格式请参考文章
                    foreach (FormFile file in files)
                    {
                        StringBuilder split = new StringBuilder();
                        split.Append("--");
                        split.Append(BOUNDARY);
                        split.Append("\r\n");
                        split.Append("Content-Disposition: form-data;name=\"" + file.FormName + "\";filename=\"" + file.FileName + "\"\r\n");
                        split.Append("Content-Type: " + file.ContentType + "\r\n\r\n");
                        WriteString(outStream, split.ToString(), useEncoding);
                        WriteData(outStream, file.Data);
                        WriteString(outStream, "\r\n", useEncoding);
                    }
                }
                byte[] end_data = System.Text.Encoding.UTF8.GetBytes("--" + BOUNDARY + "--\r\n");//数据结束标志
                outStream.Write(end_data, 0, end_data.Length);
                outStream.Flush();
            }
        }
Exemplo n.º 3
0
        /// <summary>
        /// 发送数据
        /// </summary>
        /// <param name="request">发送请求</param>
        /// <param name="prms">发送数据的字段和值</param>
        /// <param name="files">要发送的文件</param>
        /// <returns></returns>
        public static void Post(HttpWebRequest request, PostHead head, string prms)
        {
            request.CachePolicy = new RequestCachePolicy(RequestCacheLevel.NoCacheNoStore);
            request.Method      = "POST";
            head.FillInfo(request);

            if (!string.IsNullOrEmpty(prms))
            {
                Encoding useEncoding = head.PageEncoding;
                using (Stream outStream = request.GetRequestStream())
                {
                    byte[] send = useEncoding.GetBytes(prms);
                    WriteData(outStream, send);
                    outStream.Flush();
                }
            }
        }
Exemplo n.º 4
0
        /// <summary>
        /// 创建头
        /// </summary>
        /// <returns></returns>
        public static PostHead CreateHeader()
        {
            PostHead head = new PostHead(StringComparer.CurrentCultureIgnoreCase);

            return(head);
        }
Exemplo n.º 5
0
        /// <summary>
        /// 创建POST方式的HTTP请求
        /// </summary>
        /// <param name="url">请求的URL</param>
        /// <param name="parameters">随同请求POST的参数名称及参数值字典</param>
        /// <param name="timeout">请求的超时时间</param>
        /// <param name="userAgent">请求的客户端浏览器信息,可以为空</param>
        /// <param name="requestEncoding">发送HTTP请求时所用的编码</param>
        /// <param name="cookies">随同HTTP请求发送的Cookie信息,如果不需要身份验证可以为空</param>
        /// <returns></returns>
        public static HttpWebResponse CreatePostHttpResponse(string url, IDictionary <string, string> parameters, PostHead header)
        {
            if (string.IsNullOrEmpty(url))
            {
                throw new ArgumentNullException("url");
            }

            HttpWebRequest request = null;

            //如果是发送HTTPS请求
            if (url.StartsWith("https", StringComparison.OrdinalIgnoreCase))
            {
                ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult);
                request = WebRequest.Create(url) as HttpWebRequest;
                request.ProtocolVersion = HttpVersion.Version10;
            }
            else
            {
                request = WebRequest.Create(url) as HttpWebRequest;
            }
            request.Method = "POST";
            header.FillInfo(request);
            request.ContentType = "application/x-www-form-urlencoded";

            //如果需要POST数据
            if (!(parameters == null || parameters.Count == 0))
            {
                StringBuilder buffer = new StringBuilder();
                int           i      = 0;
                foreach (string key in parameters.Keys)
                {
                    if (i > 0)
                    {
                        buffer.AppendFormat("&{0}={1}", key, parameters[key]);
                    }
                    else
                    {
                        buffer.AppendFormat("{0}={1}", key, parameters[key]);
                    }
                    i++;
                }
                byte[] data = header.PageEncoding.GetBytes(buffer.ToString());
                using (Stream stream = request.GetRequestStream())
                {
                    stream.Write(data, 0, data.Length);
                }
            }
            return(request.GetResponse() as HttpWebResponse);
        }
Exemplo n.º 6
0
 public FormPost()
 {
     //_userAgent = DefaultUserAgent;
     _requestHead = PostHead.CreateHeader();
 }