public string DoPost(string url, string contentType, IDictionary <string, string> headParams, string bodyParam, SecurityProtocolType securityProtocolType = SecurityProtocolType.Tls, int timeOut = 200000, bool isSecurity = false) { if (isSecurity) { ServicePointManager.SecurityProtocol = securityProtocolType; } ServicePointManager.DefaultConnectionLimit = 1000; var wc = new CustomWebClient(); wc.Timeout = timeOut; wc.Encoding = Encoding.UTF8; wc.Headers.Add(HttpRequestHeader.ContentType, contentType); //添加参数 if (headParams != null) { foreach (KeyValuePair <string, string> s in headParams) { wc.Headers.Add(s.Key, s.Value); } } var buffer = Encoding.UTF8.GetBytes(bodyParam); var resultBuffer = wc.UploadData(url, buffer); return(Encoding.UTF8.GetString(resultBuffer)); }
public string DoPost(string url, string questParam, IDictionary <string, string> headParams = null, bool security = false, SecurityProtocolType protocolType = SecurityProtocolType.Ssl3, int timeout = 200000) { if (security) { ServicePointManager.SecurityProtocol = protocolType; } ServicePointManager.DefaultConnectionLimit = 1000; var wc = new CustomWebClient(); wc.Encoding = Encoding.UTF8; wc.Timeout = timeout; wc.Headers.Add(HttpRequestHeader.ContentType, "application/x-www-form-urlencoded"); //添加参数 if (headParams != null) { foreach (KeyValuePair <string, string> s in headParams) { wc.Headers.Add(s.Key, s.Value); } } var buffer = Encoding.UTF8.GetBytes(questParam); var resultBuffer = wc.UploadData(url, buffer); return(Encoding.UTF8.GetString(resultBuffer)); }
/// <summary> /// Execute HTTP request /// </summary> /// <param name="url">address</param> /// <param name="method">HTTP Method Definitions</param> /// <param name="data">data buffer to send</param> /// <returns>Response object</returns> public HttpResponse execute(String url, string method, byte[] data) { try { _httpClient.UploadData(url, method, data); } catch (WebException ex) { //Creating custom response for upper use. if (ex.Response != null) { var statusCode = ((HttpWebResponse)ex.Response).StatusCode; return(new HttpResponse(statusCode, ex.Message)); } throw new WebException(ex.Message, ex); } //If no exception, the response is in OK status (200) return(new HttpResponse(HttpStatusCode.OK, "Request Succeeded")); }
public T Post <T>(string controller, object request) where T : class { if (string.IsNullOrWhiteSpace(controller)) { throw new ArgumentException("controller"); } try { var policy = new RetryPolicy(new ErrorDetectionStrategy(), new FixedInterval(Settings.Default.RequestRetryCount, TimeSpan.FromSeconds(Settings.Default.RequestRetryIntervalInSeconds))); return(policy.ExecuteAction(() => { using (var wc = new CustomWebClient()) { wc.BaseAddress = _baseUri; wc.Headers[HttpRequestHeader.ContentType] = "text/xml"; wc.Credentials = WMSEnvironment.Instance.AuthenticatedUser.GetCredentials(); var buffer = Encoding.UTF8.GetBytes(XmlDocumentConverter.ConvertFrom(request).InnerXml); var resp = wc.UploadData(controller, "POST", buffer); if (typeof(WMSBusinessObject).IsAssignableFrom(typeof(T))) { using (var ms = new MemoryStream(resp)) using (var reader = XmlReader.Create(ms)) { return XmlDocumentConverter.ConvertTo <T>(reader); } } else if (typeof(T) == typeof(Stream)) { return new MemoryStream(resp) as T; } else { return (T)Convert.ChangeType(resp, typeof(T)); } } })); } catch (WebException ex) { if (ex.Status != WebExceptionStatus.ProtocolError) { throw; } var response = ex.Response as HttpWebResponse; if (response == null) { throw; } if (response.StatusCode != HttpStatusCode.InternalServerError && response.StatusCode != HttpStatusCode.Unauthorized) { throw; } using (var responseStream = response.GetResponseStream()) using (var reader = XmlReader.Create(responseStream)) { try { var err = XmlDocumentConverter.ConvertTo <HttpError>(reader); throw new Exception(err.ContainsKey("ExceptionMessage") ? string.Format("{0}: {1}", err["ExceptionType"], err["ExceptionMessage"]) : err.Message); } catch (XmlException) { throw new Exception("Could not deserialize error response."); } } } }