Ejemplo n.º 1
0
 /// <summary>
 /// Reads JSON object from URL.
 /// </summary>
 /// <typeparam name="T">Type of object to be read.</typeparam>
 /// <param name="url">URL. Cannot be null or empty.</param>
 /// <param name="timeoutMs">Request timeout in milliseconds.</param>
 /// <returns>The read object.</returns>
 /// <exception cref="WebException">If a web request error occurred.</exception>
 /// <exception cref="JsonSerializationException">If the response wasn't valid JSON.</exception>
 public static async Task <T> ReadObjectAsync <T>(string url, int timeoutMs = 100000)
 {
     return(await HtmlRequestHelper.GetStreamAsync(url, stream => {
         using (var streamReader = new StreamReader(stream))
             using (var jsonReader = new JsonTextReader(streamReader)) {
                 var serializer = new JsonSerializer();
                 return serializer.Deserialize <T>(jsonReader);
             }
     }, timeoutSec : timeoutMs / 1000));
 }
Ejemplo n.º 2
0
 /// <summary>
 /// Reads JSON object from URL.
 /// </summary>
 /// <typeparam name="T">Type of object to be read.</typeparam>
 /// <param name="url">URL. Cannot be null or empty.</param>
 /// <param name="timeout">Request timeout.</param>
 /// <param name="userAgent">User agent string.</param>
 /// <returns>The read object.</returns>
 /// <exception cref="WebException">If a web request error occurred.</exception>
 /// <exception cref="JsonSerializationException">If the response wasn't valid JSON.</exception>
 /// <exception cref="HttpRequestException">If the request failed.</exception>
 public static async Task <T> ReadObjectAsync <T>(string url, TimeSpan timeout, string userAgent = "",
                                                  Action <HttpRequestHeaders> headers            = null)
 {
     return(await HtmlRequestHelper.GetStreamAsync(url, stream => {
         using (var streamReader = new StreamReader(stream))
             using (var jsonReader = new JsonTextReader(streamReader)) {
                 var serializer = new JsonSerializer();
                 return serializer.Deserialize <T>(jsonReader);
             }
     }, timeout : timeout, userAgent : userAgent, headers : headers));
 }
Ejemplo n.º 3
0
 /// <summary>
 /// Performers a HTTP request that returns XML and deserializes that XML result into object.
 /// </summary>
 /// <typeparam name="T">Object type.</typeparam>
 /// <param name="url">URL to be requested.</param>
 /// <returns>Deserialized object. Cannot be null.</returns>
 /// <exception cref="HttpRequestException">If the request failed.</exception>
 public static Task <T> GetXmlObjectAsync <T>(string url)
 {
     return(HtmlRequestHelper.GetStreamAsync(url, stream => GetXmlResponse <T>(stream)));
 }