2017-05-28 8 views
1

マイCurrentCode:は: `C#でsystem.io.stream``にsystem.net.http.httpresponemessage`

using (var client = new HttpClient 
{ 
    Timeout = TimeSpan.FromMilliseconds(2000) 
}) 
{ 
    client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); 

    var response = await client.GetAsync(new Uri(Constants.mUrl)); 

    // convert stream to string 
    StreamReader reader = new StreamReader(respons); 

    var myItems = Newtonsoft.Json.JsonConvert.DeserializeObject<ModelClassObject>(response); 

    return myItems; 
} 

答えて

2

応答の内容の使用ReadAsStreamAsync

using(var response = await client.GetAsync(new Uri(Constants.mUrl))) { 
    var stream = await response.Content.ReadAsStreamAsync(); 
    //... 
} 

レスポンスから直接JOSNを解析する場合は、

using(var response = await client.GetAsync(new Uri(Constants.mUrl))) { 
    var myItems = await response.Content.ReadAsAsync<ModelClassObject>(); 
    return myItems; 
} 
関連する問題