public async Task <GenResponse <T> > CallWithJsonAsync <T>(Uri uri, string method, Tuple <string, string>[] dataParameters, params Tuple <string, string>[] headers) { HttpResponseMessage resp1; var method1 = new HttpMethod(method); using (var handler1 = new HttpClientHandler() { Credentials = new NetworkCredential(UAS.User, UAS.Password), CookieContainer = CookieContainer, }) using (var client1 = new HttpClient(handler1)) using (var requestMessage = new HttpRequestMessage(method1, uri)) { foreach (var header in headers) { requestMessage.Headers.Add(header.Item1, header.Item2); } requestMessage.Headers.Accept.ParseAdd("application/x-www-form-urlencoded"); resp1 = await client1.SendAsync(requestMessage).ConfigureAwait(false); } using (var sr = new StreamReader(await resp1.Content.ReadAsStreamAsync().ConfigureAwait(false))) { var content = sr.ReadToEnd(); GenResponse <T> jsonResponse = default; jsonResponse = JsonConvert.DeserializeObject <GenResponse <T> >(content); return(jsonResponse); } }
public GenResponse <T> CallWithJson <T>(Uri uri, string method, Tuple <string, string>[] dataParameters, params Tuple <string, string>[] headers) { var request = WebRequest.CreateHttp(uri); request.Credentials = new NetworkCredential(UAS.User, UAS.Password); if (CookieContainer != null) { request.CookieContainer = CookieContainer; } foreach (var header in headers) { request.Headers.Add(header.Item1, header.Item2); } request.Proxy = null; request.Method = method; // request.PreAuthenticate = true; request.ContentType = "application/x-www-form-urlencoded"; if (dataParameters != null && dataParameters.Length > 0 && method != "GET") { StringBuilder combinedParams = new StringBuilder(); foreach (var item in dataParameters) { combinedParams.Append((item.Item1 == dataParameters[0].Item1 ? "" : "&") + item.Item1 + "=" + item.Item2); } Stream stream = request.GetRequestStream(); if (method == "PUT") { using (FileStream fs = File.OpenRead(combinedParams.ToString().Split('=')[0])) { using (BinaryReader br = new BinaryReader(fs)) { byte[] bb = br.ReadBytes((int)fs.Length); stream.Write(bb, 0, bb.Length); } } } else { stream.Write(Encoding.ASCII.GetBytes(combinedParams.ToString()), 0, combinedParams.ToString().Length); } stream.Close(); } using (var httpResponse = (HttpWebResponse)request.GetResponse()) { string cookieHeader = httpResponse.Headers[HttpResponseHeader.SetCookie]; if (cookieHeader != null) { CookieContainer.SetCookies(new Uri("http://" + IP), cookieHeader); } //if (httpResponse.StatusCode == HttpStatusCode.OK) //{ using (var sr = new StreamReader(httpResponse.GetResponseStream())) { var content = sr.ReadToEnd(); GenResponse <T> jsonResponse = default; jsonResponse = JsonConvert.DeserializeObject <GenResponse <T> >(content); return(jsonResponse); } // } } }