Exemplo n.º 1
0
        /// <summary>
        /// 日時をstringからDateTimeへ変換
        /// </summary>
        public static DateTime?FormatDate(string dateString, string dateFormat, TimeZoneInfo timeZoneInfo)
        {
            if (String.IsNullOrEmpty(dateString))
            {
                return(null);
            }

            try
            {
                DateTime dateTime = DateTime.ParseExact(dateString, dateFormat, null);
                dateTime = TimeZoneInfo.ConvertTimeToUtc(dateTime, timeZoneInfo);
                return(dateTime.ToLocalTime());
            }
            catch (FormatException)
            {
#if DEBUG
                MyTraceSource.TraceEvent(TraceEventType.Error, new StringBuilder(40).Append("[").Append(dateString).Append("] is not [").Append(dateFormat).Append("]").ToString());
#endif
                return(null);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
Exemplo n.º 2
0
 private static void BrowserProcessStart(string url)
 {
     try
     {
         //規定のブラウザで配信URLを開く
         Process.Start(url);
     }
     catch (Exception ex)
     {
         MyTraceSource.TraceEvent(TraceEventType.Error, ex);
     }
 }
Exemplo n.º 3
0
        private static void BrowserProcessStart(string fileName, string url)
        {
            try
            {
                //指定のブラウザで配信URLを開く
                Process.Start(fileName, url);
            }
            //ファイルを開いてるときにエラーが発生
            catch (Win32Exception ex)
            {
                MyTraceSource.TraceEvent(TraceEventType.Error, ex);

                //規定のブラウザで配信URLを開く
                BrowserProcessStart(url);
            }
            catch (Exception ex)
            {
                MyTraceSource.TraceEvent(TraceEventType.Error, ex);
            }
        }
Exemplo n.º 4
0
        /// <summary>
        /// 日時をstringからDateTimeへ変換
        /// </summary>
        public static DateTime?FormatDate(string dateString, string dateFormat, bool isUniversal, DateTimeFormatInfo formatInfo, DateTimeStyles styles)
        {
            if (String.IsNullOrEmpty(dateString))
            {
                return(null);
            }

            try
            {
                DateTime dateTime = DateTime.ParseExact(dateString, dateFormat, formatInfo, styles);
                return(isUniversal ? dateTime.ToLocalTime() : dateTime);
            }
            catch (FormatException)
            {
#if DEBUG
                MyTraceSource.TraceEvent(TraceEventType.Error, new StringBuilder(40).Append("[").Append(dateString).Append("] is not [").Append(dateFormat).Append("]").ToString());
#endif
                return(null);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
Exemplo n.º 5
0
        public async Task <Stream> GetStreamAsync(string url, Dictionary <string, string> param)
        {
            MemoryStream memoryStream = new MemoryStream();

            if (param != null)
            {
                string[] array = new string[param.Count];
                int      num   = 0;
                foreach (KeyValuePair <string, string> item in param)
                {
                    array[num] = item.Key + "=" + Uri.EscapeDataString(item.Value);
                    num++;
                }
                url = url + "?" + string.Join("&", array);
            }
            for (int i = 1; i <= 3; i++)
            {
                try
                {
                    HttpWebRequest httpWebRequest = WebRequest.Create(url) as HttpWebRequest;
                    httpWebRequest.Method           = "GET";
                    httpWebRequest.Proxy            = null;
                    httpWebRequest.UserAgent        = userAgent_;
                    httpWebRequest.Timeout          = 30000;
                    httpWebRequest.ReadWriteTimeout = 30000;
                    WebHeaderCollection headers = httpWebRequest.Headers;
                    headers.Add("Accept-Language", "ja,en-us;q=0.7,en;q=0.3");
#if DEBUG
                    MyTraceSource.TraceEvent(TraceEventType.Information, "[MyHttpClient] " + url);
#endif
                    using (HttpWebResponse httpWebResponse = await httpWebRequest.GetResponseAsync() as HttpWebResponse)
                    {
                        if (httpWebResponse.StatusCode == HttpStatusCode.OK)
                        {
                            using (Stream stream = httpWebResponse.GetResponseStream())
                            {
                                stream.CopyTo(memoryStream);
                            }
                            break;
                        }
                    }
                }
                catch (WebException ex)
                {
                    MyTraceSource.TraceEvent(TraceEventType.Error, ex);
                    MyTraceSource.TraceEvent(TraceEventType.Error, "WebException:" + url);
                    if (ex.Status == WebExceptionStatus.ProtocolError)
                    {
                        HttpWebResponse httpWebResponse2 = (HttpWebResponse)ex.Response;
                        if (httpWebResponse2.StatusCode == HttpStatusCode.InternalServerError || httpWebResponse2.StatusCode == HttpStatusCode.ServiceUnavailable)
                        {
                            throw new ServerErrorException(url, ex);
                        }
                        if (httpWebResponse2.StatusCode == HttpStatusCode.BadRequest || httpWebResponse2.StatusCode == HttpStatusCode.Forbidden || httpWebResponse2.StatusCode == HttpStatusCode.NotFound)
                        {
                            throw new ClientErrorException(url, ex);
                        }
                    }
                    if (i >= 3)
                    {
                        throw new HttpClientException(url, ex);
                    }
                    if (ex.Status == WebExceptionStatus.NameResolutionFailure)
                    {
#if DEBUG
                        MyTraceSource.TraceEvent(TraceEventType.Information, "[MyHttpClient] NameResolutionFailure RetryWait Start");
#endif
                        await Task.Delay(15000);

#if DEBUG
                        MyTraceSource.TraceEvent(TraceEventType.Information, "[MyHttpClient] NameResolutionFailure RetryWait end");
#endif
                    }
                }
                catch (Exception innerException)
                {
                    throw new HttpClientException(url, innerException);
                }
#if DEBUG
                MyTraceSource.TraceEvent(TraceEventType.Information, "[MyHttpClient] RetryWait Start");
#endif
                await Task.Delay(5000);

#if DEBUG
                MyTraceSource.TraceEvent(TraceEventType.Information, "[MyHttpClient] RetryWait End");
#endif
            }
            memoryStream.Position = 0L;
            return(memoryStream);
        }