Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // public async Task<string> GetJson(string path)
- // {
- // var result = string.Empty;
- // var response = await client.GetAsync(path);
- //
- // if (response.IsSuccessStatusCode)
- // {
- // result = await response.Content.ReadAsStringAsync();
- // }
- // else
- // {
- // result = null;
- // }
- // return result;
- // }
- public async Task<string> GetJson(string path)
- {
- var result = String.Empty;
- try
- {
- HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create(path);
- HttpWebResponse myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse();
- if (myHttpWebResponse.StatusCode == HttpStatusCode.OK)
- {
- Task<WebResponse> task = Task.Factory.FromAsync(
- myHttpWebRequest.BeginGetResponse,
- asyncResult => myHttpWebRequest.EndGetResponse(asyncResult),
- (object) null);
- var resultStream = task.Result.GetResponseStream();
- using (Stream stream = resultStream)
- {
- StreamReader sr = new StreamReader(stream, Encoding.UTF8);
- result = sr.ReadToEnd();
- }
- myHttpWebResponse.Close();
- }
- }
- catch (WebException e)
- {
- result = null;
- }
- return result;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement