Skip to content

Instantly share code, notes, and snippets.

@gsedubun
Last active May 12, 2018 06:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gsedubun/4920e39a78abaefc7458579a8766839c to your computer and use it in GitHub Desktop.
Save gsedubun/4920e39a78abaefc7458579a8766839c to your computer and use it in GitHub Desktop.
http client consume rest json api
static void SerializeHttpClient(string resourceUrl)
{
HttpClient client = new HttpClient();
client.BaseAddress = new Uri("https://www.idx.co.id");
client.DefaultRequestHeaders.Add(HttpRequestHeader.ContentType.ToString(), "application/json");
var task = client.GetAsync(resourceUrl);
task.Wait();
var response = task.Result;
if (response.StatusCode == HttpStatusCode.OK)
{
var reader = response.Content.ReadAsStringAsync();
string result = reader.Result;
reader.Wait();
// parse the escaped string json response to proper JSON Object
JToken obj = JToken.Parse(result);
System.Console.WriteLine(obj.ToString());
var listingdata = JsonConvert.DeserializeObject<ListingData>((string)obj);
System.Console.WriteLine("{0}-{1}", listingdata.Activities[0].KodeEmiten, listingdata.Activities[0].NamaEmiten);
}
else
System.Console.WriteLine(response.StatusCode);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment