/// <seealso cref="http://weblog.west-wind.com/posts/2012/Aug/30/Using-JSONNET-for-dynamic-JSON-parsing" /> /// <seealso cref="http://james.newtonking.com/json/help/index.html?topic=html/QueryJsonDynamic.htm" /> /// <seealso cref="http://james.newtonking.com/json/help/index.html?topic=html/LINQtoJSON.htm" /> public static List <Customer> TryParseCustomers(this WebApiConsumerResponse response) { if (response == null || string.IsNullOrWhiteSpace(response.Content)) { return(null); } //dynamic dynamicJson = JObject.Parse(response.Content); //foreach (dynamic customer in dynamicJson.value) //{ // string str = string.Format("{0} {1} {2}", customer.Id, customer.CustomerGuid, customer.Email); // Debug.WriteLine(str); //} var json = JObject.Parse(response.Content); string metadata = (string)json["odata.metadata"]; if (!string.IsNullOrWhiteSpace(metadata) && metadata.EndsWith("#Customers")) { var customers = json["value"].Select(x => x.ToObject <Customer>()).ToList(); return(customers); } return(null); }
private void GetResponse(HttpWebResponse webResponse, WebApiConsumerResponse response) { if (webResponse == null) { return; } response.Status = string.Format("{0} {1}", (int)webResponse.StatusCode, webResponse.StatusDescription); response.Headers = webResponse.Headers.ToString(); using (var reader = new StreamReader(webResponse.GetResponseStream(), Encoding.UTF8)) { // TODO: file uploads should use async and await keywords response.Content = reader.ReadToEnd(); } }
public bool ProcessResponse(HttpWebRequest webRequest, WebApiConsumerResponse response) { if (webRequest == null) { return(false); } bool result = true; HttpWebResponse webResponse = null; try { webResponse = webRequest.GetResponse() as HttpWebResponse; GetResponse(webResponse, response); } catch (WebException wexc) { result = false; webResponse = wexc.Response as HttpWebResponse; GetResponse(webResponse, response); } catch (Exception exc) { result = false; response.Content = string.Format("{0}\r\n{1}", exc.Message, exc.StackTrace); } finally { if (webResponse != null) { webResponse.Close(); webResponse.Dispose(); } } return(result); }