示例#1
0
        public static void PostWithFile(M_Http_Request req)
        {
            // 随机序列,用作防止服务器无法识别数据的起始位置
            string formDataBoundary = String.Format("----------{0:N}", Guid.NewGuid());

            // 将数据转换为byte[]格式
            req.formDataBytes = GetMultipartFormData(req.formDatas, formDataBoundary);
            // 创建http对象
            HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(new Uri(req.PostUrl));

            // 设为post请求
            request.Method      = "POST";
            request.ContentType = "multipart/form-data; boundary=" + formDataBoundary;
            if (req.IsAsync)
            {
                request.BeginGetRequestStream(req.GetRequestStreamCallback, request);
            }
            else
            {
                var postStream = request.GetRequestStream();
                postStream.Write(req.formDataBytes, 0, req.formDataBytes.Length);
                postStream.Close();
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                StreamReader    sr       = new StreamReader(response.GetResponseStream());
                req.result.isSuccess = true;
                req.result.resultStr = sr.ReadToEnd();
                response.Close();
            }
        }
示例#2
0
        //public HttpConfig config = new HttpConfig();
        #region 文件上传 同步/异步 OLD
        /// <summary>
        /// 单文件上传,用于微信等异步上传文件后返回资源ID
        /// </summary>
        /// <param name="url">服务器URL</param>
        /// <param name="name">元素名称=Request.Files[name]</param>
        /// <param name="fileMod">文件信息(文件名称,字节,ContentType)</param>
        /// <param name="callback">文件上传统一异步,必须实现回调</param>
        public static M_Http_Request UploadSignleFile(string url, string name, M_Http_File fileMod, M_Http_Request.ResponseCallback callback = null)
        {
            M_Http_Request req = new M_Http_Request()
            {
                PostUrl = url
            };

            req.formDatas.Add(name, fileMod);
            req.callback = callback;
            if (req.callback != null)
            {
                req.IsAsync = true;
            }
            HttpHelper.PostWithFile(req);
            return(req);
        }
示例#3
0
        /// <summary>
        /// 带Cookies|证书等的提交
        /// 证书未设置好会有较长时间的卡顿
        /// </summary>
        public static M_Http_Result Post(M_Http_Request reqMod)
        {
            //System.GC.Collect();//垃圾回收,回收没有正常关闭的http连接
            HttpWebRequest  request  = null;
            HttpWebResponse response = null;

            try
            {
                request = (HttpWebRequest)WebRequest.Create(reqMod.PostUrl);
                //设置https验证方式并带证书
                if (!string.IsNullOrEmpty(reqMod.CertPath))
                {
                    //有些API请求只是需要验证自己发布的证书,并不需要https
                    if (reqMod.PostUrl.StartsWith("https", StringComparison.OrdinalIgnoreCase))
                    {
                        ServicePointManager.DefaultConnectionLimit = 200;
                        ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult);
                    }
                    //X509Certificate2 cert = new X509Certificate2(function.VToP(appMod.Pay_SSLPath), appMod.Pay_SSLPassword);
                    ////@"D:\Code\ssw.cer"  ||证书名称
                    //Cryptography.X509Certificates.X509Certificate2 cert = new System.Security.Cryptography.X509Certificates.X509Certificate2(reqMod.CertPath, reqMod.CertPwd);
                    //request.ClientCertificates.Add(cert);
                }
                if (reqMod.cookie.Count == 0)
                {
                    request.CookieContainer = new CookieContainer();
                    reqMod.cookie           = request.CookieContainer;
                }
                else
                {
                    request.CookieContainer = reqMod.cookie;
                }

                request.Method      = "POST";
                request.ContentType = "application/x-www-form-urlencoded";
                byte[] data = Encoding.UTF8.GetBytes(reqMod.PostDataStr);
                request.ContentLength = data.Length;
                //向服务端写入数据
                Stream reqStream = request.GetRequestStream();
                reqStream.Write(data, 0, data.Length);
                reqStream.Close();
                //request.Timeout = 10 * 1000;

                //设置代理服务器
                //WebProxy proxy = new WebProxy();                          //定义一个网关对象
                //proxy.Address = new Uri(WxPayConfig.PROXY_URL);              //网关服务器端口:端口
                //request.Proxy = proxy;

                response = (HttpWebResponse)request.GetResponse();
                Stream       myResponseStream = response.GetResponseStream();
                StreamReader myStreamReader   = new StreamReader(myResponseStream, Encoding.UTF8);
                reqMod.result.resultStr = myStreamReader.ReadToEnd();
                myStreamReader.Close();
                myResponseStream.Close();
                reqMod.result.isSuccess = true;
            }
            catch (Exception ex)
            {
                reqMod.result.isSuccess = false;
                reqMod.result.exmsg     = ex.Message;
                ZLLog.L("Post,[" + reqMod.PostUrl + "][" + reqMod.PostDataStr + "],err:" + ex.Message);
            }
            finally
            {
                //关闭连接和流
                if (response != null)
                {
                    response.Close();
                }
                if (request != null)
                {
                    request.Abort();
                }
            }
            return(reqMod.result);
        }