/// <summary> /// 提交POST请求 /// </summary> /// <param name="url">提交地址</param> /// <param name="postData">提交的数据</param> /// <param name="sendEncode">发送时使用的编码格式</param> /// <param name="receiveEncode">接收时使用的编码格式</param> /// <param name="timeout">超时时间</param> /// <param name="signFlag">是否签名</param> /// <param name="caLogin">是否为登录,如果是登录通过post提交数据需要的+进行替换</param> /// <returns></returns> private static CResponseResult HttpPostResult(String url, String postData, Encoding sendEncode, Encoding receiveEncode, int timeout = 0, bool signFlag = false,bool caLogin=false) { using (new CWaitCursor()) { //连接网站是否出错 bool isConnErr = false; //返回结果 CResponseResult result = new CResponseResult(); //创建Http请求 HttpWebRequest request; try { byte[] postBytes = null; if (caLogin) { postBytes = sendEncode.GetBytes(postData.Replace("+", "%2B")); } else { postBytes = sendEncode.GetBytes(postData); } //设置用于验证服务器证书的回调 ServicePointManager.ServerCertificateValidationCallback = ValidateServerCertificate; //创建Http请求 //如果是发送HTTPS请求 if (url.StartsWith("https", StringComparison.OrdinalIgnoreCase)) { ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(ValidateServerCertificate); request = WebRequest.Create(url) as HttpWebRequest; request.ProtocolVersion = HttpVersion.Version10; } else request = (HttpWebRequest)WebRequest.Create(new Uri(url)); //超时 if (timeout > 0) { //此处是毫秒为单位,不能随便转成以秒单位 request.Timeout = timeout; if (request.ReadWriteTimeout < request.Timeout) request.ReadWriteTimeout = request.Timeout; } request.CookieContainer = GetCookie(url); request.Headers.Set("Cache-Control", "no-cache"); request.Headers.Set("Pragma", "no-cache"); request.Headers.Set("FRAME_SSO_TICKET", LscSSOTicket.Replace(" ","").Replace(" ","")); request.Method = "POST"; if (signFlag) { if (Encoding.Default.GetBytes(postData).Length > limitSize * 1024) { string compressDataString = BZipUtil.compressTobase64(BZipUtil.compress(postData)); postBytes = sendEncode.GetBytes(compressDataString); request.ContentType = "application/zip-compress-ca-encrypted-data"; } else { request.ContentType = "application/ca-encrypted-data"; } } else { if (Encoding.Default.GetBytes(postData).Length > limitSize * 1024) { string compressDataString = BZipUtil.compressTobase64(BZipUtil.compress(postData)); postBytes = sendEncode.GetBytes(compressDataString); request.ContentType = "application/zip-compress-data"; } else { request.ContentType = "application/x-www-form-urlencoded"; } } request.ContentLength = postBytes.Length; //发起Http请求 //设置代理 ProxySetting(request); //用于发送数据的 Stream 对象 Stream requestStream = request.GetRequestStream(); requestStream.Write(postBytes, 0, postBytes.Length); requestStream.Close(); //if (currBaseCookie == null) //{ // currBaseCookie = new CookieContainer(); //request.CookieContainer = GetCookie(url); //} } catch (Exception ex) { isConnErr = true; result.Code = "1"; result.Message = ex.Message; //出现异常,统一抛出HttpPostException throw new HttpPostException(ex); } //Http响应 if (!isConnErr) { try { HttpWebResponse response = (HttpWebResponse)request.GetResponse(); Encoding jspEncode = GetJspEncode(response.GetResponseHeader("Content-Type")); string contentType = response.ContentType; StreamReader reader = new StreamReader(response.GetResponseStream(), jspEncode == null ? receiveEncode : jspEncode); if (contentType.Contains("application/zip-compress-data;")) { //返回内容 result.Message = BZipUtil.decompress(BZipUtil.base64Tocompress(reader.ReadToEnd().Trim())); } else { result.Message = reader.ReadToEnd().Trim(); } result.Code = "0"; //关闭连接 reader.Close(); response.Close(); } catch (WebException ex) { HttpWebResponse res = ex.Response as HttpWebResponse; if (res != null) { if (res.StatusCode == HttpStatusCode.Forbidden) { Stream s = res.GetResponseStream(); Encoding jspEncode = GetJspEncode(res.GetResponseHeader("Content-Type")); string contentType = res.ContentType; StreamReader objReader = new StreamReader(s, receiveEncode); if (contentType.Contains("application/zip-compress-data;")) { //返回内容 result.Message = BZipUtil.decompress(BZipUtil.base64Tocompress(objReader.ReadToEnd().Trim())); } else { result.Message = objReader.ReadToEnd().Trim(); } objReader.Close(); objReader.Close(); result.Code = "403"; } else { result.Message = ex.Message; result.Code = ((int)(res.StatusCode)).ToString(); } return result; } throw new HttpPostException(ex); } catch (Exception ex) { result.Code = "2"; result.Message = ex.Message; throw new HttpPostException(ex); } } return result; } }