public async Task <T> DeleteAsync <T>(HttpEnum httpEnum, string url, Dictionary <string, string> headers = null) { try { var client = _clientFactory.CreateClient(httpEnum.ToString()); if (headers != null) { foreach (var header in headers) { if (!client.DefaultRequestHeaders.Contains(header.Key)) { client.DefaultRequestHeaders.Add(header.Key, header.Value); } } } var response = await client.DeleteAsync(url); if (response.StatusCode == System.Net.HttpStatusCode.OK) { string result = await response.Content.ReadAsStringAsync(); return(JsonConvert.DeserializeObject <T>(result)); } else { throw new Exception($"Http Error StatusCode:{response.StatusCode}"); } } catch (Exception) { throw; } }
public async Task <string> PostAsync <R>(HttpEnum httpEnum, string url, R request, Dictionary <string, string> headers = null) { try { var client = _clientFactory.CreateClient(httpEnum.ToString()); if (headers != null) { foreach (var header in headers) { if (!client.DefaultRequestHeaders.Contains(header.Key)) { client.DefaultRequestHeaders.Add(header.Key, header.Value); } } } var stringContent = new StringContent(JsonConvert.SerializeObject(request), Encoding.UTF8, "application/json"); var response = await client.PostAsync(url, stringContent); if (response.StatusCode == System.Net.HttpStatusCode.OK) { return(await response.Content.ReadAsStringAsync()); } else { throw new Exception($"Http Error StatusCode:{response.StatusCode}"); } } catch (Exception) { throw; } }
public string GoRequest(string sURL, HttpEnum eMode, string postData,bool bKeepAlive=false,string sReferer="") { LastError = null; string Resbody = null; HttpWebRequest req; req = HttpWebRequest.Create(sURL) as HttpWebRequest; req.Method = eMode.ToString(); req.Accept = "*/*"; req.KeepAlive = bKeepAlive; //req.CachePolicy = new HttpRequestCachePolicy(HttpRequestCacheLevel.NoCacheNoStore); if (!string.IsNullOrEmpty(sReferer)) req.Referer = sReferer; Encoding myEncoding = _ReqEncoding; string sContentType = "application/x-www-form-urlencoded; charset=UTF-8"; req.ContentType = sContentType; //req.UseDefaultCredentials = true; //req.UserAgent = "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E)"; //req.Headers.Add("x-requested-with", "XMLHttpRequest"); if (sURL.IndexOf("https") == 0 && _cert!=null) { req.ClientCertificates.Add(MyCert); Util.SetCertificatePolicy(); } try { if (eMode == HttpEnum.POST) { byte[] bufPost = myEncoding.GetBytes(postData); req.ContentLength = bufPost.Length; Stream newStream = req.GetRequestStream(); newStream.Write(bufPost, 0, bufPost.Length); newStream.Close(); } HttpWebResponse res = req.GetResponse() as HttpWebResponse; try { #region resMsg //string.Format("Response.ContentLength:\t{0}", res.ContentLength); //string.Format("Response.ContentType:\t{0}", res.ContentType); //string.Format("Response.CharacterSet:\t{0}", res.CharacterSet); //string.Format("Response.ContentEncoding:\t{0}", res.ContentEncoding); //string.Format("Response.IsFromCache:\t{0}", res.IsFromCache); //string.Format("Response.IsMutuallyAuthenticated:\t{0}", res.IsMutuallyAuthenticated); //string.Format("Response.LastModified:\t{0}", res.LastModified); //string.Format("Response.Method:\t{0}", res.Method); //string.Format("Response.ProtocolVersion:\t{0}", res.ProtocolVersion); //string.Format("Response.ResponseUri:\t{0}", res.ResponseUri); //string.Format("Response.Server:\t{0}", res.Server); //string.Format("Response.StatusCode:\t{0}\t# {1}", res.StatusCode, (int)res.StatusCode); //string.Format("Response.StatusDescription:\t{0}", res.StatusDescription); //// header //for (int i = 0; i < res.Headers.Count; ++i) //{ // string.Format("[{2}] {0}:\t{1}", res.Headers.Keys[i], res.Headers[i], i); //} #endregion Encoding encoding = _ResEncoding; using (Stream resStream = res.GetResponseStream()) { using (StreamReader resStreamReader = new StreamReader(resStream, encoding)) { Resbody = (resStreamReader.ReadToEnd()); } } } finally { res.Close(); } } catch (Exception ex) { LastError = ex.ToString(); } return Resbody; }
public string GoRequest(string sURL, HttpEnum eMode, string postData, bool bKeepAlive = false, string sReferer = "") { LastError = null; string Resbody = null; HttpWebRequest req; req = HttpWebRequest.Create(sURL) as HttpWebRequest; req.Method = eMode.ToString(); req.Accept = "*/*"; req.KeepAlive = bKeepAlive; //req.CachePolicy = new HttpRequestCachePolicy(HttpRequestCacheLevel.NoCacheNoStore); if (!string.IsNullOrEmpty(sReferer)) { req.Referer = sReferer; } Encoding myEncoding = _ReqEncoding; string sContentType = "application/x-www-form-urlencoded; charset=UTF-8"; req.ContentType = sContentType; //req.UseDefaultCredentials = true; //req.UserAgent = "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E)"; //req.Headers.Add("x-requested-with", "XMLHttpRequest"); if (sURL.IndexOf("https") == 0 && _cert != null) { req.ClientCertificates.Add(MyCert); Util.SetCertificatePolicy(); } try { if (eMode == HttpEnum.POST) { byte[] bufPost = myEncoding.GetBytes(postData); req.ContentLength = bufPost.Length; Stream newStream = req.GetRequestStream(); newStream.Write(bufPost, 0, bufPost.Length); newStream.Close(); } HttpWebResponse res = req.GetResponse() as HttpWebResponse; try { #region resMsg //string.Format("Response.ContentLength:\t{0}", res.ContentLength); //string.Format("Response.ContentType:\t{0}", res.ContentType); //string.Format("Response.CharacterSet:\t{0}", res.CharacterSet); //string.Format("Response.ContentEncoding:\t{0}", res.ContentEncoding); //string.Format("Response.IsFromCache:\t{0}", res.IsFromCache); //string.Format("Response.IsMutuallyAuthenticated:\t{0}", res.IsMutuallyAuthenticated); //string.Format("Response.LastModified:\t{0}", res.LastModified); //string.Format("Response.Method:\t{0}", res.Method); //string.Format("Response.ProtocolVersion:\t{0}", res.ProtocolVersion); //string.Format("Response.ResponseUri:\t{0}", res.ResponseUri); //string.Format("Response.Server:\t{0}", res.Server); //string.Format("Response.StatusCode:\t{0}\t# {1}", res.StatusCode, (int)res.StatusCode); //string.Format("Response.StatusDescription:\t{0}", res.StatusDescription); //// header //for (int i = 0; i < res.Headers.Count; ++i) //{ // string.Format("[{2}] {0}:\t{1}", res.Headers.Keys[i], res.Headers[i], i); //} #endregion Encoding encoding = _ResEncoding; using (Stream resStream = res.GetResponseStream()) { using (StreamReader resStreamReader = new StreamReader(resStream, encoding)) { Resbody = (resStreamReader.ReadToEnd()); } } } finally { res.Close(); } } catch (Exception ex) { LastError = ex.ToString(); } return(Resbody); }