コード例 #1
0
        /// <summary>
        /// Download string data with a specific UserAgent.
        /// </summary>
        /// <param name="url"></param>
        /// <param name="UserAgentType"></param>
        /// <returns></returns>
        public static async Task <string> DownloadStringData(string url, WebUserAgentType UserAgentType)
        {
            using (var client = new HttpClient())
            {
                client.DefaultRequestHeaders.UserAgent.TryParseAdd(StrWebUserAgentType(UserAgentType));

                var request = new HttpRequestMessage()
                {
                    RequestUri = new Uri(url),
                    Method     = HttpMethod.Get
                };

                HttpResponseMessage response;
                try
                {
                    response = client.SendAsync(request).Result;
                }
                catch (Exception ex)
                {
                    Common.LogError(ex, "PluginCommon", $"Error on Download {url}");
                    return(string.Empty);
                }

                int statusCode = (int)response.StatusCode;
                if (statusCode == 200)
                {
                    return(await response.Content.ReadAsStringAsync());
                }
                else
                {
                    logger.Warn($"PluginCommon - DownloadStringData() with statuscode {statusCode} for {url}");
                    return(string.Empty);
                }
            }
        }
コード例 #2
0
 private static string StrWebUserAgentType(WebUserAgentType UserAgentType)
 {
     switch (UserAgentType)
     {
     case (WebUserAgentType.Request):
         return("request");
     }
     return(string.Empty);
 }