Last active
May 12, 2018 06:14
-
-
Save gsedubun/4920e39a78abaefc7458579a8766839c to your computer and use it in GitHub Desktop.
http client consume rest json api
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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