コード例 #1
0
        /// <exception cref="UriFormatException">NoteIn the .NET for Windows Store apps or the Portable Class Library, catch the base class exception, <see cref="T:System.FormatException" />, instead.The length of <paramref name="stringToEscape" /> exceeds 32766 characters.</exception>
        /// <exception cref="EncoderFallbackException">A fallback occurred (see Character Encoding in the .NET Framework for complete explanation)-and-<see cref="P:System.Text.Encoding.EncoderFallback" /> is set to <see cref="T:System.Text.EncoderExceptionFallback" />.</exception>
        /// <exception cref="ArgumentNullException">Value is null. </exception>
        /// <exception cref="ArgumentException">Stream does not support reading. </exception>
        /// <exception cref="HttpRequestException"></exception>
        /// <exception cref="WebSocketException">Condition.</exception>
        /// <exception cref="WebException">Condition.</exception>
        public static async Task <Prtg> Read(RequestPrams request)
        {
            // Allow for multiple server.  If the first server returns any kind of error, we will try the next servers.
            using (var servers = request.ServerAndPort.GetEnumerator())
            {
                if (!servers.MoveNext())
                {
                    throw new ArgumentException("No Servers specified", nameof(request));
                }

                while (true)
                {
                    try
                    {
                        using (var client = new HttpClient
                        {
                            BaseAddress = new Uri(servers.Current + "/pools/default/buckets/")
                        })
                        {
                            var auth = Convert.ToBase64String(Encoding.ASCII.GetBytes(request.User + ":" + request.Password));
                            client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", auth);

                            using (var result = await client.GetAsync(Uri.EscapeUriString(request.BucketName) + @"/stats?zoom=minute"))
                            {
                                result.EnsureSuccessStatusCode();
                                using (var s = await result.Content.ReadAsStreamAsync())
                                {
                                    var reader = new JsonTextReader(new StreamReader(s));
                                    return(new Prtg {
                                        Results = (ReadData(JToken.Load(reader)).ToList())
                                    });
                                }
                            }
                        }
                    }
                    // If it's an exception that we know is from connecting to the server
                    // let's try the next one
                    // TODO: Report back error status.
                    catch (WebSocketException)
                    {
                        if (!servers.MoveNext())
                        {
                            throw;
                        }
                    }
                    catch (WebException)
                    {
                        if (!servers.MoveNext())
                        {
                            throw;
                        }
                    }
                    catch (HttpRequestException)
                    {
                        if (!servers.MoveNext())
                        {
                            throw;
                        }
                    }
                }
            }
        }
コード例 #2
0
ファイル: Program.cs プロジェクト: AdamCLarsen/PRTG-Couchbase
		/// <exception cref="UriFormatException">NoteIn the .NET for Windows Store apps or the Portable Class Library, catch the base class exception, <see cref="T:System.FormatException" />, instead.The length of <paramref name="stringToEscape" /> exceeds 32766 characters.</exception>
		/// <exception cref="EncoderFallbackException">A fallback occurred (see Character Encoding in the .NET Framework for complete explanation)-and-<see cref="P:System.Text.Encoding.EncoderFallback" /> is set to <see cref="T:System.Text.EncoderExceptionFallback" />.</exception>
		/// <exception cref="ArgumentNullException">Value is null. </exception>
		/// <exception cref="ArgumentException">Stream does not support reading. </exception>
		/// <exception cref="HttpRequestException"></exception>
		/// <exception cref="WebSocketException">Condition.</exception>
		/// <exception cref="WebException">Condition.</exception>
		public static async Task<Prtg> Read(RequestPrams request)
		{
			// Allow for multiple server.  If the first server returns any kind of error, we will try the next servers.
			using (var servers = request.ServerAndPort.GetEnumerator())
			{
				if (!servers.MoveNext())
					throw new ArgumentException("No Servers specified", nameof(request));

				while (true)
				{
					try
					{
						using (var client = new HttpClient
						{
							BaseAddress = new Uri(servers.Current + "/pools/default/buckets/")
						})
						{
							var auth = Convert.ToBase64String(Encoding.ASCII.GetBytes(request.User + ":" + request.Password));
							client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", auth);

							using (var result = await client.GetAsync(Uri.EscapeUriString(request.BucketName) + @"/stats?zoom=minute"))
							{
								result.EnsureSuccessStatusCode();
								using (var s = await result.Content.ReadAsStreamAsync())
								{
									var reader = new JsonTextReader(new StreamReader(s));
									return new Prtg { Results = (ReadData(JToken.Load(reader)).ToList()) };
								}
							}
						}
					}
					// If it's an exception that we know is from connecting to the server
					// let's try the next one
					// TODO: Report back error status.
					catch (WebSocketException)
					{
						if (!servers.MoveNext())
							throw;
					}
					catch (WebException)
					{
						if (!servers.MoveNext())
							throw;
					}
					catch (HttpRequestException)
					{
						if (!servers.MoveNext())
							throw;
					}
				}
			}
		}