public static string QueryStringForTR(Request trParams, Request req, string postURL, BraintreeService service) { #if netcore string trData = TrUtil.BuildTrData(trParams, "http://example.com", service); string postData = "tr_data=" + WebUtility.UrlEncode(trData) + "&"; postData += req.ToQueryString(); var request = new HttpRequestMessage(new HttpMethod("POST"), postURL); request.Headers.Add("KeepAlive", "false"); request.Headers.Add("Accept", "application/json"); byte[] buffer = Encoding.UTF8.GetBytes(postData); request.Content = new StringContent(postData, Encoding.UTF8, "application/x-www-form-urlencoded"); request.Content.Headers.Add("Content-Length", buffer.Length.ToString()); var httpClientHandler = new HttpClientHandler { AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate, AllowAutoRedirect = false, }; HttpResponseMessage response; using (var client = new HttpClient(httpClientHandler)) { response = client.SendAsync(request).GetAwaiter().GetResult(); } StreamReader reader = new StreamReader(response.Content.ReadAsStreamAsync().Result, Encoding.UTF8); string responseBody = reader.ReadToEnd(); string query = response.Headers.Location.Query; return query; #else string trData = TrUtil.BuildTrData(trParams, "http://example.com", service); string postData = "tr_data=" + HttpUtility.UrlEncode(trData, Encoding.UTF8) + "&"; postData += req.ToQueryString(); var request = WebRequest.Create(postURL) as HttpWebRequest; request.Method = "POST"; request.KeepAlive = false; request.AllowAutoRedirect = false; byte[] buffer = Encoding.UTF8.GetBytes(postData); request.ContentType = "application/x-www-form-urlencoded"; request.ContentLength = buffer.Length; Stream requestStream = request.GetRequestStream(); requestStream.Write(buffer, 0, buffer.Length); requestStream.Close(); var response = request.GetResponse() as HttpWebResponse; string query = new Uri(response.GetResponseHeader("Location")).Query; response.Close(); return query; #endif }
private IEnumerable<GraphiteGraphDto> GetAllFrom(GraphiteGraphDto dto = null) { var q = dto == null ? "*" : dto.Id + ".*"; var request = new HttpRequestMessage(HttpMethod.Get, _graphiteSettings.GetSearchUri(q)); HttpResponseMessage response = null; try { response = request.GetResponse(); } catch { // TODO Not sure what to do with these now } if (null == response) yield break; // Bad response, prolly should not have these, but oh well for now if (HttpStatusCode.OK != response.StatusCode) yield break; // No bad children var json = response.Content.ReadAsStringAsync().Result; var results = JsonConvert.DeserializeObject<IEnumerable<GraphiteGraphDto>>(json).ToArray(); if (!results.Any()) { yield return dto; } else if (results.Count() > _graphiteSettings.MaximumGraphiteChildrenToSeek) { yield break; // too many results to process } else { var children = results.AsParallel().SelectMany(GetAllFrom); foreach (var child in children) { yield return child; } } }
private static HttpResponseMessage GetResponse(Uri requestUri) { var request = new HttpRequestMessage(HttpMethod.Get, requestUri); HttpResponseMessage response = null; try { response = request.GetResponse(); } catch { // TODO Not sure what to do with these now } return response; }