Пример #1
0
        /// <summary>
        /// http的基本post方法
        /// </summary>
        /// <param name="reqData">请求数据</param>
        /// <param name="url">URL地址</param>
        /// <param name="encoding">编码</param>
        /// <returns>服务器返回的数据</returns>
        public static Dictionary <String, String> Post(Dictionary <String, String> reqData, String reqUrl, Encoding encoding)
        {
            string postData = SDKUtil.CreateLinkString(reqData, false, true);

            byte[] byteArray = encoding.GetBytes(postData);
            try
            {
                if ("false".Equals(SDKConfig.IfValidateRemoteCert)) //测试环境不验https证书
                {
                    System.Net.ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(ValidateServerCertificate);
                }
                log.Info("发送post请求,url=[" + reqUrl + "],data=[" + postData + "]");
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(reqUrl);
                request.ContentType   = "application/x-www-form-urlencoded";
                request.Method        = "POST";
                request.ContentLength = byteArray.Length;
                request.ServicePoint.Expect100Continue = false;

                Stream requestStream = request.GetRequestStream();
                requestStream.Write(byteArray, 0, byteArray.Length);

                HttpWebResponse webResponse = (HttpWebResponse)request.GetResponse();
                StreamReader    reader      = new StreamReader(webResponse.GetResponseStream(), encoding);
                String          sResult     = reader.ReadToEnd();

                requestStream.Close();
                reader.Close();
                webResponse.Close();
                if (webResponse.StatusCode == HttpStatusCode.OK)
                {
                    log.Info("收到后台应答,data=[" + sResult + "]");
                    return(SDKUtil.CoverStringToDictionary(sResult, encoding));
                }
                else
                {
                    string httpStatus = Enum.GetName(typeof(HttpStatusCode), webResponse.StatusCode);
                    log.Info("非200HTTP状态,httpStatus=" + httpStatus + ",data=[" + sResult + "]");
                    return(new Dictionary <string, string>());
                }
            }
            catch (Exception ex)
            {
                log.Error("post失败,异常:" + ex.Message);
                return(new Dictionary <string, string>());
            }
        }
Пример #2
0
        /// <summary>
        /// 建立请求,以模拟远程HTTP的POST请求方式构造并获取银联的处理结果
        /// </summary>
        /// <param name="sParaTemp">请求参数数组</param>
        /// <returns>银联处理结果</returns>
        public int Send(Dictionary <string, string> sParaTemp, Encoding encoder)
        {
            // System.Net.ServicePointManager.Expect100Continue = false;
            //待请求参数数组字符串
            //    string strRequestData = BuildRequestParaToString(sParaTemp, encoder);
            string strRequestData = SDKUtil.CreateLinkString(sParaTemp, true, true);

            //把数组转换成流中所需字节数组类型
            byte[]          bytesRequestData = encoder.GetBytes(strRequestData);
            HttpWebResponse HttpWResp        = null;

            try
            {
                System.Net.ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult);
                //设置HttpWebRequest基本信息
                HttpWebRequest myReq = (HttpWebRequest)HttpWebRequest.Create(requestUrl);
                myReq.Method      = "post";
                myReq.ContentType = "application/x-www-form-urlencoded";
                //填充POST数据
                myReq.ContentLength = bytesRequestData.Length;
                Stream requestStream = myReq.GetRequestStream();  //获得请求流
                requestStream.Write(bytesRequestData, 0, bytesRequestData.Length);
                requestStream.Close();
                //发送POST数据请求服务器
                HttpWResp = (HttpWebResponse)myReq.GetResponse();
                Stream myStream = HttpWResp.GetResponseStream();
                //获取服务器返回信息
                StreamReader reader = new StreamReader(myStream, encoder);
                result = reader.ReadToEnd();
                //释放
                myStream.Close();

                return((int)HttpWResp.StatusCode);
            }
            catch (Exception exp)
            {
                result = "报错:" + exp.Message;
                return(0);
            }
        }