using System.IO; using System.Net; using System.Text; using BWP.ABCClient.Exceptions; namespace BWP.ABCClient { public class Request { public string Message { get; set; } public string Url { get; set; } public Encoding RequestEncoding { get; set; } public Encoding ResponseEncoding { get; set; } public string ProxyIP { get; set; } public int ProxyPort { get; set; } public string Run() { ServicePointManager.ServerCertificateValidationCallback = delegate { return true; }; var request = (HttpWebRequest)WebRequest.Create(Url); if (!string.IsNullOrEmpty(ProxyIP) && ProxyPort != 0) { request.Proxy = new WebProxy(ProxyIP, ProxyPort); } request.Method = "POST"; request.ProtocolVersion = HttpVersion.Version10; request.ContentType = "application/x-www-form-urlencoded"; byte[] bytes = RequestEncoding.GetBytes(Message); request.ContentLength = bytes.Length; using (var stream = request.GetRequestStream()) { stream.Write(bytes, 0, bytes.Length); } var response = (HttpWebResponse)request.GetResponse(); if (response.StatusCode != HttpStatusCode.OK) { throw new HttpException("网上支付平台服务暂时停止 " + response.StatusCode); } using (var responseStream = response.GetResponseStream()) { using (var reader = new StreamReader(responseStream, ResponseEncoding)) { return reader.ReadToEnd(); } } } } }