Exemplo n.º 1
0
    //public static string randomizeShortURLProvider()
    //{
    //    string[] providers = {"Tinyurl", "Bitly", "Isgd"};
    //     Random rnd = new Random();
    //    string random = string.Empty;
    //    for (int i = 0; i <= 3; i++)
    //    {
    //         random = providers[rnd.Next(0, 3)].ToString();
    //    }
    //     return random;
    //}
    public static string ShortenURL(string strUrl, ShortURLProvider eService)
    {
        // return empty strings if not valid
        if (!IsValidURL(strUrl))
        {
            return "";
        }

        string requestUrl = string.Format(GetRequestTemplate(eService), strUrl);
        WebRequest request = HttpWebRequest.Create(requestUrl);
        request.Proxy = null;
        string strResult = null;
        try
        {
            using (Stream responseStream = request.GetResponse().GetResponseStream())
            {
                StreamReader reader = new StreamReader(responseStream, Encoding.ASCII);
                strResult = reader.ReadToEnd();
                if (!IsValidURL(strResult))
                {
                    WebException w = new WebException(strResult);

                    throw w;
                }
            }
        }
        catch (Exception)
        {
            return strUrl; // eat it and return original url
        }

        // if converted is longer than original, return original
        if (strResult.Length > strUrl.Length)
            strResult = strUrl;

        return strResult;
    }
        internal static PhotobucketException CreateFromWebException(WebException webEx)
        {
            if ((webEx.Message.Contains("401") || webEx.Message.Contains("500")) && webEx.Response.ContentType == "text/xml")
            {
                PhotobucketApiException apiEx = new PhotobucketApiException(webEx);

                apiEx._response = new XmlResponseMessage(webEx.Response.GetResponseStream());

                XmlNode messageNode = apiEx._response.ResponseXml.SelectSingleNode("descendant::message");
                apiEx._responseMessage = messageNode.InnerText;

                XmlNode errorCodeNode = apiEx._response.ResponseXml.SelectSingleNode("descendant::code");
                int     apiError      = Convert.ToInt32(errorCodeNode.InnerText);

                switch (apiError)
                {
                case 115:
                case 116:
                case 117:
                {
                    apiEx._errorCode = ErrorCode.CouldNotCreate;
                    break;
                }

                case 105:
                case 106:
                case 107:
                case 110:
                case 112:
                case 113:
                case 121:
                case 124:
                case 125:
                case 126:
                case 130:
                case 137:
                case 140:
                case 141:
                case 142:
                case 143:
                case 144:
                case 145:
                case 146:
                case 147:
                case 148:
                case 149:
                case 152:
                case 153:
                case 154:
                case 159:
                case 203:
                {
                    apiEx._errorCode = ErrorCode.CouldNotUploadOrUpdate;
                    break;
                }

                case 123:
                case 139:
                case 102:
                {
                    apiEx._errorCode = ErrorCode.CouldNotGet;
                    break;
                }

                case 108:
                case 109:
                case 009:
                case 007:
                case 111:
                case 138:
                {
                    apiEx._errorCode = ErrorCode.InvalidPermissions;
                    break;
                }

                case 131:
                case 132:
                case 133:
                case 134:
                case 135:
                case 136:
                {
                    apiEx._errorCode = ErrorCode.CouldNotAdd;
                    break;
                }

                case 118:
                case 204:
                {
                    apiEx._errorCode = ErrorCode.CouldNotEmail;
                    break;
                }

                case 103:
                case 104:
                {
                    apiEx._errorCode = ErrorCode.CouldNotSearch;
                    break;
                }

                default:
                {
                    apiEx._errorCode = ErrorCode.UnknownError;
                    break;
                }
                }

                return(apiEx);
            }
            else if (webEx.Message.Contains("404"))
            {
                PhotobucketApiException apiEx = new PhotobucketApiException(webEx);
                apiEx._errorCode = ErrorCode.CouldNotGet;
                return(apiEx);
            }
            else
            {
                return(new PhotobucketWebException(webEx));
            }
        }
Exemplo n.º 3
0
        private static Exception BeanstreamApiException(WebException webEx)
        {
            var response = webEx.Response as HttpWebResponse;

            if (response == null)
            {
                return(new CommunicationException("Could not process the request succesfully", webEx));
            }

            var statusCode = response.StatusCode;
            var data       = GetResponseBody(response);       //Get from exception

            var code     = -1;
            var category = -1;
            var message  = "";

            if (data != null)
            {
                if (response.ContentType.Contains("application/json"))
                {
                    try {
                        JToken json = JObject.Parse(data);
                        if (json != null && json.SelectToken("code") != null)
                        {
                            code = Convert.ToInt32(json.SelectToken("code"));
                        }
                        if (json != null && json.SelectToken("category") != null)
                        {
                            category = Convert.ToInt32(json.SelectToken("category"));
                        }
                        if (json != null && json.SelectToken("message") != null)
                        {
                            message = json.SelectToken("message").ToString();
                        }
                    } catch (Exception e) {
                        // data is not json and not in the format we expect
                    }
                }
            }

            switch (statusCode)
            {
            case HttpStatusCode.Found:                                                          // 302
                return(new RedirectionException(statusCode, data, message, category, code));    // Used for redirection response in 3DS, Masterpass and Interac Online requests

            case HttpStatusCode.BadRequest:                                                     // 400
                return(new InvalidRequestException(statusCode, data, message, category, code)); // Often missing a required parameter

            case HttpStatusCode.Unauthorized:                                                   // 401
                return(new UnauthorizedException(statusCode, data, message, category, code));   // authentication exception

            case HttpStatusCode.PaymentRequired:                                                // 402
                return(new BusinessRuleException(statusCode, data, message, category, code));   // Request failed business requirements or rejected by processor/bank

            case HttpStatusCode.Forbidden:                                                      // 403
                return(new ForbiddenException(statusCode, data, message, category, code));      // authorization failure

            case HttpStatusCode.MethodNotAllowed:                                               // 405
                return(new InvalidRequestException(statusCode, data, message, category, code)); // Sending the wrong HTTP Method

            case HttpStatusCode.UnsupportedMediaType:                                           // 415
                return(new InvalidRequestException(statusCode, data, message, category, code)); // Sending an incorrect Content-Type

            default:
                return(new InternalServerException(statusCode, data, message, category, code));
            }
        }
Exemplo n.º 4
0
        /// <summary>
        /// Sends the specified packet to Sentry.
        /// </summary>
        /// <param name="packet">The packet to send.</param>
        /// <param name="dsn">The Data Source Name in Sentry.</param>
        /// <returns>
        /// The <see cref="JsonPacket.EventID"/> of the successfully captured JSON packet, or <c>null</c> if it fails.
        /// </returns>
        protected virtual string Send(JsonPacket packet, Dsn dsn)
        {
            packet.Logger = Logger;

            try
            {
                var request = (HttpWebRequest)WebRequest.Create(dsn.SentryUri);
                request.Timeout          = (int)Timeout.TotalMilliseconds;
                request.ReadWriteTimeout = (int)Timeout.TotalMilliseconds;
                request.Method           = "POST";
                request.Accept           = "application/json";
                request.Headers.Add("X-Sentry-Auth", PacketBuilder.CreateAuthenticationHeader(dsn));
                request.UserAgent = PacketBuilder.UserAgent;

                if (Compression)
                {
                    request.Headers.Add(HttpRequestHeader.ContentEncoding, "gzip");
                    request.AutomaticDecompression = DecompressionMethods.Deflate;
                    request.ContentType            = "application/octet-stream";
                }
                else
                {
                    request.ContentType = "application/json; charset=utf-8";
                }

                /*string data = packet.ToString(Formatting.Indented);
                 * Console.WriteLine(data);*/

                string data = packet.ToString(Formatting.None);

                if (LogScrubber != null)
                {
                    data = LogScrubber.Scrub(data);
                }

                // Write the messagebody.
                using (Stream s = request.GetRequestStream())
                {
                    if (Compression)
                    {
                        GzipUtil.Write(data, s);
                    }
                    else
                    {
                        using (StreamWriter sw = new StreamWriter(s))
                            sw.Write(data);
                    }
                }

                using (HttpWebResponse wr = (HttpWebResponse)request.GetResponse())
                    using (Stream responseStream = wr.GetResponseStream())
                    {
                        if (responseStream == null)
                        {
                            return(null);
                        }

                        using (StreamReader sr = new StreamReader(responseStream))
                        {
                            string content  = sr.ReadToEnd();
                            var    response = JsonConvert.DeserializeObject <dynamic>(content);
                            return(response.id);
                        }
                    }
            }
            catch (Exception exception)
            {
                Console.ForegroundColor = ConsoleColor.Red;
                Console.Write("[ERROR] ");
                Console.ForegroundColor = ConsoleColor.Gray;
                Console.WriteLine(exception);

                WebException webException = exception as WebException;
                if (webException != null && webException.Response != null)
                {
                    string messageBody;
                    using (Stream stream = webException.Response.GetResponseStream())
                    {
                        if (stream == null)
                        {
                            return(null);
                        }

                        using (StreamReader sw = new StreamReader(stream))
                            messageBody = sw.ReadToEnd();
                    }

                    Console.WriteLine("[MESSAGE BODY] " + messageBody);
                }
            }

            return(null);
        }
    /// <summary>
    /// Attempt to log any detailed information we find about the failed web request
    /// </summary>
    /// <param name="webException"></param>
    /// <param name="onlineStatusLog"></param>
    private static void AttemptToLogWebException(WebException webException, string description, TaskStatusLogs onlineStatusLog)
    {
        if(onlineStatusLog == null) return; //No logger? nothing to do

        try
        {
            if(string.IsNullOrWhiteSpace(description))
            {
                description = "web request failed";
            }
            var response = webException.Response;
            var responseText = GetWebResponseAsText(response);
            response.Close();
            if(responseText == null) responseText = "";

            onlineStatusLog.AddError(description +  ": " + webException.Message + "\r\n" + responseText + "\r\n");
        }
        catch (Exception ex)
        {
            onlineStatusLog.AddError("Error in web request exception: " + ex.Message);
            return;
        }
    }
Exemplo n.º 6
0
        public void WebExceptionConstructorMessageTest()
        {
            string expected = "Exception Message";
            WebException target = new WebException(expected);
            Assert.IsNotNull(target);

            string actual = target.Message;
            Assert.AreEqual(expected, actual);
        }
Exemplo n.º 7
0
		private RavenJToken HandleErrors(WebException e)
		{
			var httpWebResponse = e.Response as HttpWebResponse;
			if (httpWebResponse == null ||
				httpWebResponse.StatusCode == HttpStatusCode.Unauthorized ||
				httpWebResponse.StatusCode == HttpStatusCode.NotFound ||
				httpWebResponse.StatusCode == HttpStatusCode.Conflict)
			{
				int httpResult = -1;
				if (httpWebResponse != null)
					httpResult = (int)httpWebResponse.StatusCode;

				factory.InvokeLogRequest(owner, () => new RequestResultArgs
				{
					DurationMilliseconds = CalculateDuration(),
					Method = webRequest.Method,
					HttpResult = httpResult,
					Status = RequestStatus.ErrorOnServer,
					Result = e.Message,
					Url = webRequest.RequestUri.PathAndQuery,
					PostedData = postedData
				});

				return null;//throws
			}

			if (httpWebResponse.StatusCode == HttpStatusCode.NotModified
				&& CachedRequestDetails != null)
			{
				factory.UpdateCacheTime(this);
				var result = factory.GetCachedResponse(this, httpWebResponse.Headers);

				HandleReplicationStatusChanges(httpWebResponse.Headers, primaryUrl, operationUrl);

				factory.InvokeLogRequest(owner, () => new RequestResultArgs
				{
					DurationMilliseconds = CalculateDuration(),
					Method = webRequest.Method,
					HttpResult = (int)httpWebResponse.StatusCode,
					Status = RequestStatus.Cached,
					Result = result.ToString(),
					Url = webRequest.RequestUri.PathAndQuery,
					PostedData = postedData
				});

				return result;
			}



			using (var sr = new StreamReader(e.Response.GetResponseStreamWithHttpDecompression()))
			{
				var readToEnd = sr.ReadToEnd();

				factory.InvokeLogRequest(owner, () => new RequestResultArgs
				{
					DurationMilliseconds = CalculateDuration(),
					Method = webRequest.Method,
					HttpResult = (int)httpWebResponse.StatusCode,
					Status = RequestStatus.Cached,
					Result = readToEnd,
					Url = webRequest.RequestUri.PathAndQuery,
					PostedData = postedData
				});

				if (string.IsNullOrWhiteSpace(readToEnd))
					return null;// throws

				RavenJObject ravenJObject;
				try
				{
					ravenJObject = RavenJObject.Parse(readToEnd);
				}
				catch (Exception)
				{
					throw new InvalidOperationException(readToEnd, e);
				}
				if (ravenJObject.ContainsKey("IndexDefinitionProperty"))
				{
					throw new IndexCompilationException(ravenJObject.Value<string>("Message"))
					{
						IndexDefinitionProperty = ravenJObject.Value<string>("IndexDefinitionProperty"),
						ProblematicText = ravenJObject.Value<string>("ProblematicText")
					};
				}
                if (httpWebResponse.StatusCode == HttpStatusCode.BadRequest && ravenJObject.ContainsKey("Message"))
			    {
                    throw new BadRequestException(ravenJObject.Value<string>("Message"), e);
			    }
				if (ravenJObject.ContainsKey("Error"))
				{
					var sb = new StringBuilder();
					foreach (var prop in ravenJObject)
					{
						if (prop.Key == "Error")
							continue;

						sb.Append(prop.Key).Append(": ").AppendLine(prop.Value.ToString(Formatting.Indented));
					}

					sb.AppendLine()
						.AppendLine(ravenJObject.Value<string>("Error"));

					throw new InvalidOperationException(sb.ToString(), e);
				}
				throw new InvalidOperationException(readToEnd, e);
			}
		}
Exemplo n.º 8
0
        void httpPostSync(string command, string data, Action <object> callback = null) // for send version only
        {
            string url = server_url_ + command;

            // Request
            HttpWebRequest web_request = (HttpWebRequest)WebRequest.Create(url);

            web_request.Method      = "POST";
            web_request.ContentType = "application/octet-stream";

            Request request = new Request();

            request.web_request = web_request;
            request.callback    = callback;
            request.command     = command;

            try
            {
                byte[] bytes             = System.Text.Encoding.UTF8.GetBytes(data);
                ArraySegment <byte> body = new ArraySegment <byte>(bytes);
                web_request.ContentLength = body.Count;
                request.body = body;

                Stream stream = web_request.GetRequestStream();
                stream.Write(request.body.Array, 0, request.body.Count);
                stream.Close();

                request.web_response = (HttpWebResponse)web_request.GetResponse();
                request.web_request  = null;

                if (request.web_response.StatusCode == HttpStatusCode.OK)
                {
                    request.read_stream = request.web_response.GetResponseStream();
                    StreamReader sr = new StreamReader(request.read_stream);

                    byte[] buffer = System.Text.Encoding.UTF8.GetBytes(sr.ReadToEnd());
                    request.body = new ArraySegment <byte>(buffer);

                    webRequestCallback(request);

                    request.read_stream.Close();
                    request.web_response.Close();
                }
                else
                {
                    FunDebug.LogError("Host manager response failed. status:{0}",
                                      request.web_response.StatusDescription);
                }
            }
            catch (Exception e)
            {
                WebException we = e as WebException;
                if (we != null && we.Status == WebExceptionStatus.ConnectFailure)
                {
                    onRequestFailed(request);
                }
                else if ((we != null && we.Status == WebExceptionStatus.RequestCanceled) ||
                         (e is ObjectDisposedException || e is NullReferenceException))
                {
                    FunDebug.LogDebug("Dedicated Server - httpPostServerVersionSync operation has been cancelled.");
                }
            }
        }
        private static void MaybeMapException(WebException we)
        {
            var response = we.Response;

            if (response == null || response.ContentLength == 0)
            {
                return;
            }
            try {
                using var stream = response.GetResponseStream();
                if (stream == null)
                {
                    return;
                }
                if (response.ContentType.StartsWith("application/xml"))
                {
                    Debug.Print($"[{DateTime.UtcNow}] => RESPONSE ({response.ContentType}): <{response.ContentLength} byte(s)>");
                    StringBuilder?sb    = null;
                    var           xpath = new XPathDocument(stream).CreateNavigator().Select("/error/text");
                    while (xpath.MoveNext())
                    {
                        if (sb == null)
                        {
                            sb = new StringBuilder();
                        }
                        else
                        {
                            sb.AppendLine();
                        }
                        sb.Append(xpath.Current?.InnerXml);
                    }
                    if (sb == null)
                    {
                        Debug.Print($"[{DateTime.UtcNow}] => NO ERROR TEXT FOUND");
                        return;
                    }
                    Debug.Print($"[{DateTime.UtcNow}] => ERROR: \"{sb}\"");
                    throw new QueryException(sb.ToString(), we);
                }
                if (response.ContentType.StartsWith("application/json"))
                {
                    var characterSet = "utf-8";
                    if (response is HttpWebResponse httpResponse)
                    {
                        characterSet = httpResponse.CharacterSet;
                        if (string.IsNullOrWhiteSpace(characterSet))
                        {
                            characterSet = "utf-8";
                        }
                    }
                    var enc = Encoding.GetEncoding(characterSet);
                    using var sr = new StreamReader(stream, enc);
                    var json = sr.ReadToEnd();
                    Debug.Print($"[{DateTime.UtcNow}] => RESPONSE ({response.ContentType}): \"{JsonUtils.Prettify(json)}\"");
                    var moe = Query.Deserialize <MessageOrError>(json);
                    if (moe?.Error == null)
                    {
                        Debug.Print($"[{DateTime.UtcNow}] => NO ERROR TEXT FOUND");
                        return;
                    }
                    Debug.Print($"[{DateTime.UtcNow}] => ERROR: \"{moe.Error}\" ({moe.Help})");
                    throw new QueryException(moe.Error, we)
                          {
                              HelpLink = moe.Help
                          };
                }
                Debug.Print($"[{DateTime.UtcNow}] => UNHANDLED ERROR RESPONSE ({response.ContentType}): <{response.ContentLength} byte(s)>");
            }
            catch (QueryException) { throw; }
            catch (Exception e) {
                Debug.Print($"[{DateTime.UtcNow}] => FAILED TO PROCESS ERROR RESPONSE: [{e.GetType()}] {e.Message}");
                /* keep calm and fall through */
            }
        }
Exemplo n.º 10
0
 public ActionResult Throw()
 {
     var innerException = new WebException("Error", WebExceptionStatus.ServerProtocolViolation);
     throw new InvalidOperationException("error!", innerException);
 }
Exemplo n.º 11
0
        public override void Abort()
        {
            if (Interlocked.CompareExchange(ref _aborted, 1, 0) == 1)
                return;

            if (_haveResponse && FinishedReading)
                return;

            _haveResponse = true;
            if (_abortHandler != null)
            {
                try
                {
                    _abortHandler(this, EventArgs.Empty);
                }
                catch (Exception)
                { }
                _abortHandler = null;
            }

            if (_asyncWrite != null)
            {
                var r = _asyncWrite;
                if (!r.Task.IsCompleted)
                {
                    try
                    {
                        var wexc = new WebException("Aborted.", WebExceptionStatus.RequestCanceled);
                        r.TrySetException(wexc);
                    }
                    catch
                    { }
                }

                _asyncWrite = null;
            }

            if (_asyncRead != null)
            {
                var r = _asyncRead;
                if (!r.Task.IsCompleted)
                {
                    try
                    {
                        var wexc = new WebException("Aborted.", WebExceptionStatus.RequestCanceled);
                        r.TrySetException(wexc);
                    }
                    catch
                    { }
                }

                _asyncRead = null;
            }

            if (_writeStream != null)
            {
                try
                {
                    _writeStream.Close();
                    _writeStream = null;
                }
                catch
                { }
            }

            if (_webResponse != null)
            {
                try
                {
                    _webResponse.Close();
                    _webResponse = null;
                }
                catch
                { }
            }
        }
Exemplo n.º 12
0
        // Returns true if redirected
        async Task<bool> CheckFinalStatusAsync()
        {
            //if (result.IsFaulted)
            //{
            //    _bodyBuffer = null;

            //    throw result.Exception;
            //}

            Exception throwMe = null;

            //var resp = result.Result;
            var protoError = WebExceptionStatus.ProtocolError;
            HttpStatusCode code = 0;
            if (throwMe == null && _webResponse != null)
            {
                code = _webResponse.StatusCode;
                if ((!_authState.IsCompleted && code == HttpStatusCode.Unauthorized && _credentials != null) ||
                    (ProxyQuery && !_proxyAuthState.IsCompleted && code == HttpStatusCode.ProxyAuthenticationRequired))
                {
                    if (!_usedPreAuth && CheckAuthorization(_webResponse, code))
                    {
                        // Keep the written body, so it can be rewritten in the retry
                        if (InternalAllowBuffering)
                        {
                            // NTLM: This is to avoid sending data in the 'challenge' request
                            // We save it in the first request (first 401), don't send anything
                            // in the challenge request and send it in the response request along
                            // with the buffers kept form the first request.
                            if (_authState.NtlmAuthState == NtlmAuthState.Challenge || _proxyAuthState.NtlmAuthState == NtlmAuthState.Challenge)
                            {
                                _bodyBuffer = _writeStream.WriteBuffer;
                                _bodyBufferLength = _writeStream.WriteBufferLength;
                            }
                            return true;
                        }
                        if (_method != "PUT" && _method != "POST")
                        {
                            _bodyBuffer = null;
                            return true;
                        }

                        if (!ThrowOnError)
                            return false;

                        _writeStream.InternalClose();
                        _writeStream = null;
                        _webResponse.Close();
                        _webResponse = null;
                        _bodyBuffer = null;

                        throw new WebException("This request requires buffering " +
                                               "of data for authentication or " +
                                               "redirection to be sucessful.");
                    }
                }

                _bodyBuffer = null;
                if ((int)code >= 400)
                {
                    var err = String.Format("The remote server returned an error: ({0}) {1}.",
                        (int)code, _webResponse.StatusDescription);
                    throwMe = new WebException(err, null, protoError, _webResponse);
                    await _webResponse.ReadAllAsync().ConfigureAwait(false);
                }
                else if ((int)code == 304 && AllowAutoRedirect)
                {
                    var err = String.Format("The remote server returned an error: ({0}) {1}.",
                        (int)code, _webResponse.StatusDescription);
                    throwMe = new WebException(err, null, protoError, _webResponse);
                }
                else if ((int)code >= 300 && AllowAutoRedirect && _redirects >= _maxAutoRedirect)
                {
                    throwMe = new WebException("Max. redirections exceeded.", null,
                        protoError, _webResponse);
                    await _webResponse.ReadAllAsync().ConfigureAwait(false);
                }
            }

            _bodyBuffer = null;
            if (throwMe == null)
            {
                var b = false;
                var c = (int)code;
                if (AllowAutoRedirect && c >= 300)
                {
                    b = Redirect(code);
                    if (InternalAllowBuffering && _writeStream.WriteBufferLength > 0)
                    {
                        _bodyBuffer = _writeStream.WriteBuffer;
                        _bodyBufferLength = _writeStream.WriteBufferLength;
                    }
                    if (b && !UnsafeAuthenticatedConnectionSharing)
                    {
                        _authState.Reset();
                        _proxyAuthState.Reset();
                    }
                }

                if (_previousWebResponse != null && c >= 300 && c != 304)
                    await _previousWebResponse.ReadAllAsync().ConfigureAwait(false);

                return b;
            }

            if (!ThrowOnError)
                return false;

            if (_writeStream != null)
            {
                _writeStream.InternalClose();
                _writeStream = null;
            }

            _webResponse = null;

            throw throwMe;
        }
Exemplo n.º 13
0
        internal async Task SetResponseDataAsync(WebConnectionData data)
        {
            var isLocked = false;
            var closeStream = default(Stream);

            try
            {
                Monitor.Enter(_locker, ref isLocked);

                if (Aborted)
                {
                    closeStream = data.Stream;

                    return;
                }

                _previousWebResponse = _webResponse;

                WebException wexc = null;
                try
                {
                    var createTask = HttpWebResponse.CreateAsync(Address, _method, data, CookieContainer);

                    if (createTask.IsCompleted)
                        _webResponse = createTask.Result;
                    else
                    {
                        try
                        {
                            Monitor.Exit(_locker);
                            isLocked = false;

                            _webResponse = await createTask.ConfigureAwait(false);
                        }
                        finally
                        {
                            Monitor.Enter(_locker, ref isLocked);
                        }
                    }
                }
                catch (Exception e)
                {
                    wexc = new WebException(e.Message, e, WebExceptionStatus.ProtocolError, null);
                    closeStream = data.Stream;
                }

                if (wexc == null && (_method == "POST" || _method == "PUT"))
                {
                    await CheckSendErrorAsync(data).ConfigureAwait(false);
                    if (_savedExc != null)
                        wexc = (WebException)_savedExc;
                }
            }
            finally
            {
                if (isLocked)
                    Monitor.Exit(_locker);

                if (null != closeStream)
                    closeStream.Close();
            }

            isLocked = false;
            try
            {
                Monitor.TryEnter(_locker, ref isLocked);

                if (Aborted)
                {
                    if (data.Stream != null)
                        data.Stream.Close();

                    return;
                }

                WebException wexc = null;

                var r = _asyncRead;

                var forced = false;
                if (r == null && _webResponse != null)
                {
                    // This is a forced completion (302, 204)...
                    forced = true;
                    r = new TaskCompletionSource<HttpWebResponse>();
                    r.TrySetResult(_webResponse);
                }

                if (r != null)
                {
                    if (wexc != null)
                    {
                        _haveResponse = true;
                        if (!r.Task.IsCompleted)
                            r.TrySetException(wexc);
                        return;
                    }

                    var isProxy = ProxyQuery && !_proxy.IsBypassed(Address);

                    try
                    {
                        var redirected = await CheckFinalStatusAsync().ConfigureAwait(false);

                        if (!redirected)
                        {
                            if ((isProxy ? _proxyAuthState.IsNtlmAuthenticated : _authState.IsNtlmAuthenticated) &&
                                _webResponse != null && (int)_webResponse.StatusCode < 400)
                            {
                                var wce = _webResponse.GetResponseStream() as WebConnectionStream;
                                if (wce != null)
                                {
                                    var cnc = wce.Connection;
                                    cnc.NtlmAuthenticated = true;
                                }
                            }

                            // clear internal buffer so that it does not
                            // hold possible big buffer (bug #397627)
                            if (_writeStream != null)
                                _writeStream.KillBuffer();

                            _haveResponse = true;
                            r.TrySetResult(_webResponse);
                        }
                        else
                        {
                            if (_webResponse != null)
                            {
                                if (await HandleNtlmAuthAsync(r.Task).ConfigureAwait(false))
                                    return;
                                _webResponse.Close();
                            }
                            FinishedReading = false;
                            _haveResponse = false;
                            _webResponse = null;
                            //r.Reset();
                            _servicePoint = GetServicePoint();
                            _abortHandler = _servicePoint.SendRequest(this, _connectionGroup);
                        }
                    }
                    catch (WebException wexc2)
                    {
                        if (forced)
                        {
                            _savedExc = wexc2;
                            _haveResponse = true;
                        }
                        r.TrySetException(wexc2);
                    }
                    catch (Exception ex)
                    {
                        wexc = new WebException(ex.Message, ex, WebExceptionStatus.ProtocolError, null);
                        if (forced)
                        {
                            _savedExc = wexc;
                            _haveResponse = true;
                        }
                        r.TrySetException(wexc);
                    }
                }
            }
            finally
            {
                if (isLocked)
                    Monitor.Exit(_locker);
            }
        }
Exemplo n.º 14
0
 private void OnValidateBackendServerCacheCompleted(object extraData)
 {
     base.CallThreadEntranceMethod(delegate
     {
         IAsyncResult asyncResult        = extraData as IAsyncResult;
         HttpWebResponse httpWebResponse = null;
         Exception ex = null;
         try
         {
             this.Logger.LogCurrentTime("H-OnResponseReady");
             httpWebResponse = (HttpWebResponse)this.headRequest.EndGetResponse(asyncResult);
             this.ThrowWebExceptionForRetryOnErrorTest(httpWebResponse, new int[]
             {
                 0,
                 1,
                 2
             });
         }
         catch (WebException ex2)
         {
             ex = ex2;
         }
         catch (HttpException ex3)
         {
             ex = ex3;
         }
         catch (IOException ex4)
         {
             ex = ex4;
         }
         catch (SocketException ex5)
         {
             ex = ex5;
         }
         finally
         {
             this.Logger.LogCurrentTime("H-EndGetResponse");
             if (httpWebResponse != null)
             {
                 httpWebResponse.Close();
             }
             this.headRequest = null;
         }
         if (ex != null)
         {
             ExTraceGlobals.VerboseTracer.TraceError <Exception, int, ProxyRequestHandler.ProxyState>((long)this.GetHashCode(), "[ProxyRequestHandler::OnValidateBackendServerCacheCompleted]: Head response error: {0}; Context {1}; State {2}", ex, this.TraceContext, this.State);
         }
         WebException ex6 = ex as WebException;
         bool flag        = true;
         if (ex6 != null)
         {
             this.LogWebException(ex6);
             if (this.HandleWebExceptionConnectivityError(ex6))
             {
                 flag = false;
             }
         }
         if (flag && this.ShouldRecalculateBackendOnHead(ex6) && this.RecalculateTargetBackend())
         {
             flag = false;
         }
         if (flag)
         {
             this.BeginProxyRequestOrRecalculate();
         }
     });
 }
        bool handleHttpWebErrorResponse(AsyncResult asyncResult, WebException we)
        {
            asyncResult.Metrics.AddProperty(RequestMetrics.Metric.Exception, we);

            HttpStatusCode         statusCode;
            AmazonServiceException errorResponseException = null;

            using (HttpWebResponse httpErrorResponse = we.Response as HttpWebResponse)
            {
                if (httpErrorResponse == null)
                {
                    // Abort the unsuccessful request
                    asyncResult.RequestState.WebRequest.Abort();

                    // If it is a keep alive error or name resolution error then attempt a retry
                    if (we != null && asyncResult.RetriesAttempt < config.MaxErrorRetry && (we.Status == WebExceptionStatus.KeepAliveFailure || we.Status == WebExceptionStatus.NameResolutionFailure))
                    {
                        pauseExponentially(asyncResult);
                        return(true);
                    }
                    throw new AmazonServiceException(we);
                }
                statusCode = httpErrorResponse.StatusCode;
                asyncResult.Metrics.AddProperty(RequestMetrics.Metric.StatusCode, statusCode);
                string redirectedLocation = httpErrorResponse.Headers["location"];
                asyncResult.Metrics.AddProperty(RequestMetrics.Metric.RedirectLocation, redirectedLocation);

                using (httpErrorResponse)
                {
                    var unmarshaller = asyncResult.Unmarshaller;
                    UnmarshallerContext errorContext = unmarshaller.CreateContext(httpErrorResponse, config.LogResponse || config.ReadEntireResponse || AWSConfigs.ResponseLogging != ResponseLoggingOption.Never, asyncResult);
                    errorResponseException = unmarshaller.UnmarshallException(errorContext, we, statusCode);
                    if (config.LogResponse || AWSConfigs.ResponseLogging != ResponseLoggingOption.Never)
                    {
                        logger.Error(errorResponseException, "Received error response: [{0}]", errorContext.ResponseBody);
                    }
                    asyncResult.Metrics.AddProperty(RequestMetrics.Metric.AWSRequestID, errorResponseException.RequestId);
                    asyncResult.Metrics.AddProperty(RequestMetrics.Metric.AWSErrorCode, errorResponseException.ErrorCode);
                }
                asyncResult.RequestState.WebRequest.Abort();

                if (isTemporaryRedirect(statusCode, redirectedLocation))
                {
                    asyncResult.Request.Endpoint = new Uri(redirectedLocation);
                    return(true);
                }
                else if (ShouldRetry(statusCode, this.config, errorResponseException, asyncResult.RetriesAttempt))
                {
                    pauseExponentially(asyncResult);
                    return(true);
                }
            }

            if (errorResponseException != null)
            {
                logger.Error(errorResponseException, "Error making request {0}.", asyncResult.RequestName);
                throw errorResponseException;
            }

            AmazonServiceException excep = new AmazonServiceException("Unable to make request", we, statusCode);

            logger.Error(excep, "Error making request {0}.", asyncResult.RequestName);
            asyncResult.Metrics.AddProperty(RequestMetrics.Metric.Exception, excep);
            throw excep;
        }
Exemplo n.º 16
0
        /// <summary>
        /// Queries the source with the given list of installed packages to get any updates that are available.
        /// </summary>
        /// <param name="installedPackages">The list of currently installed packages.</param>
        /// <param name="includePrerelease">True to include prerelease packages (alpha, beta, etc).</param>
        /// <param name="includeAllVersions">True to include older versions that are not the latest version.</param>
        /// <param name="targetFrameworks">The specific frameworks to target?</param>
        /// <param name="versionContraints">The version constraints?</param>
        /// <returns>A list of all updates available.</returns>
        public List <NugetPackage> GetUpdates(IEnumerable <NugetPackage> installedPackages, bool includePrerelease = false, bool includeAllVersions = false, string targetFrameworks = "", string versionContraints = "")
        {
            if (IsLocalPath)
            {
                return(GetLocalUpdates(installedPackages, includePrerelease, includeAllVersions));
            }

            List <NugetPackage> updates = new List <NugetPackage>();

            // check for updates in groups of 10 instead of all of them, since that causes servers to throw errors for queries that are too long
            for (int i = 0; i < installedPackages.Count(); i += 10)
            {
                var packageGroup = installedPackages.Skip(i).Take(10);

                string packageIds = string.Empty;
                string versions   = string.Empty;

                foreach (var package in packageGroup)
                {
                    if (string.IsNullOrEmpty(packageIds))
                    {
                        packageIds += package.Id;
                    }
                    else
                    {
                        packageIds += "|" + package.Id;
                    }

                    if (string.IsNullOrEmpty(versions))
                    {
                        versions += package.Version;
                    }
                    else
                    {
                        versions += "|" + package.Version;
                    }
                }

                string url = string.Format("{0}GetUpdates()?packageIds='{1}'&versions='{2}'&includePrerelease={3}&includeAllVersions={4}&targetFrameworks='{5}'&versionConstraints='{6}'", ExpandedPath, packageIds, versions, includePrerelease.ToString().ToLower(), includeAllVersions.ToString().ToLower(), targetFrameworks, versionContraints);

                try
                {
                    var newPackages = GetPackagesFromUrl(url, UserName, ExpandedPassword);
                    updates.AddRange(newPackages);
                }
                catch (System.Exception e)
                {
                    WebException    webException = e as WebException;
                    HttpWebResponse webResponse  = webException != null ? webException.Response as HttpWebResponse : null;
                    if (webResponse != null && webResponse.StatusCode == HttpStatusCode.NotFound)
                    {
                        // Some web services, such as VSTS don't support the GetUpdates API. Attempt to retrieve updates via FindPackagesById.
                        NugetHelper.LogVerbose("{0} not found. Falling back to FindPackagesById.", url);
                        return(GetUpdatesFallback(installedPackages, includePrerelease, includeAllVersions, targetFrameworks, versionContraints));
                    }

                    Debug.LogErrorFormat("Unable to retrieve package list from {0}\n{1}", url, e.ToString());
                }
            }

            // sort alphabetically
            updates.Sort(delegate(NugetPackage x, NugetPackage y)
            {
                if (x.Id == null && y.Id == null)
                {
                    return(0);
                }
                else if (x.Id == null)
                {
                    return(-1);
                }
                else if (y.Id == null)
                {
                    return(1);
                }
                else if (x.Id == y.Id)
                {
                    return(x.Version.CompareTo(y.Version));
                }
                else
                {
                    return(x.Id.CompareTo(y.Id));
                }
            });

#if TEST_GET_UPDATES_FALLBACK
            // Enable this define in order to test that GetUpdatesFallback is working as intended. This tests that it returns the same set of packages
            // that are returned by the GetUpdates API. Since GetUpdates isn't available when using a Visual Studio Team Services feed, the intention
            // is that this test would be conducted by using nuget.org's feed where both paths can be compared.
            List <NugetPackage> updatesReplacement = GetUpdatesFallback(installedPackages, includePrerelease, includeAllVersions, targetFrameworks, versionContraints);
            ComparePackageLists(updates, updatesReplacement, "GetUpdatesFallback doesn't match GetUpdates API");
#endif

            return(updates);
        }
Exemplo n.º 17
0
 public ChefExceptionBuilder(string username, WebException exception)
 {
     Build(username, exception);
 }
Exemplo n.º 18
0
 public ResponseError(WebException e)
     : base("Server responded with error")
 {
     this.error = e;
 }
Exemplo n.º 19
0
 private static bool IsServerDown(WebException e)
 {
     return(e.InnerException is SocketException);
 }
Exemplo n.º 20
0
    public void TestPokitDokException_StringExceptionRaised()
    {
        var raised = false;
        WebException innerException = new WebException("Alternative Test Message");
        try
        {
            throw new PokitDokException("Test Message.", innerException);
        }
        catch(PokitDokException e)
        {
            Assert.AreEqual("Test Message.", e.Message);
            Assert.AreEqual(e.InnerException.Message, innerException.Message);
            Assert.AreEqual(e.InnerException, innerException);
            raised = true;
        }

        Assert.IsTrue(raised);
    }
 /// <summary>
 /// Initializes a new instance of the <see cref="WebRequestException"/> class.
 /// </summary>
 /// <param name="inner">The inner exception.</param>
 private WebRequestException(WebException inner)
     : base("Unexpected WebException encountered", inner)
 {
 }
Exemplo n.º 22
0
 public void WebExceptionConstructorEmptyTest()
 {
     WebException target = new WebException();
     Assert.IsNotNull(target);
 }
Exemplo n.º 23
0
 public TUSException(OperationCanceledException ex)
     : base(ex.Message, ex, WebExceptionStatus.RequestCanceled, null)
 {
     this.OriginalException = null;
 }
Exemplo n.º 24
0
        internal void SetWriteStreamError(WebExceptionStatus status, Exception exc)
        {
            if (Aborted)
                return;

            if (null != _asyncWrite || null != _asyncRead)
            {
                string msg;
                WebException wex;
                if (exc == null)
                {
                    msg = "Error: " + status;
                    wex = new WebException(msg, status);
                }
                else
                {
                    msg = String.Format("Error: {0} ({1})", status, exc.Message);
                    wex = new WebException(msg, exc, status);
                }

                if (null != _asyncWrite)
                    _asyncWrite.TrySetException(wex);
                if (null != _asyncRead)
                    _asyncRead.TrySetException(wex);
            }
        }
Exemplo n.º 25
0
        private static DataServiceTransportException ConvertToDataServiceWebException(WebException webException)
        {
            HttpWebResponseMessage errorResponseMessage = null;

            if (webException.Response != null)
            {
                var httpResponse = (HttpWebResponse)webException.Response;
#if ASTORIA_LIGHT
                errorResponseMessage = new HttpWebResponseMessage(httpResponse.GetUnderlyingHttpResponse());
#else
                errorResponseMessage = new HttpWebResponseMessage(httpResponse);
#endif
            }

            return(new DataServiceTransportException(errorResponseMessage, webException));
        }
Exemplo n.º 26
0
 public RestApiException(string message, WebException inner)
     : base(message, inner)
 {
 }
Exemplo n.º 27
0
        private bool Write(AuditRecord record)
        {
            Exception exception = null;
            bool      retry     = false;

            try
            {
                GrayException.MapAndReportGrayExceptions(delegate()
                {
                    try
                    {
                        record.Visit(this.visitor);
                        exception = null;
                        retry     = false;
                    }
                    catch (ServerInMMException exception2)
                    {
                        exception = exception2;
                        retry     = true;
                    }
                    catch (TenantAccessBlockedException exception3)
                    {
                        exception = exception3;
                        retry     = false;
                    }
                    catch (DataSourceOperationException exception4)
                    {
                        exception = exception4;
                        retry     = true;
                    }
                    catch (ADTransientException exception5)
                    {
                        exception = exception5;
                        retry     = true;
                    }
                    catch (AuditException exception6)
                    {
                        exception = exception6;
                        retry     = false;
                    }
                    catch (AuditLogServiceException ex)
                    {
                        exception = ex;
                        retry     = false;
                        ResponseCodeType responseCodeType;
                        Enum.TryParse <ResponseCodeType>(ex.Code, true, out responseCodeType);
                        ResponseCodeType responseCodeType2 = responseCodeType;
                        if (responseCodeType2 <= ResponseCodeType.ErrorNotEnoughMemory)
                        {
                            if (responseCodeType2 <= ResponseCodeType.ErrorClientDisconnected)
                            {
                                if (responseCodeType2 != ResponseCodeType.ErrorADUnavailable && responseCodeType2 != ResponseCodeType.ErrorBatchProcessingStopped && responseCodeType2 != ResponseCodeType.ErrorClientDisconnected)
                                {
                                    goto IL_185;
                                }
                            }
                            else if (responseCodeType2 <= ResponseCodeType.ErrorInternalServerTransientError)
                            {
                                if (responseCodeType2 != ResponseCodeType.ErrorConnectionFailed)
                                {
                                    switch (responseCodeType2)
                                    {
                                    case ResponseCodeType.ErrorInsufficientResources:
                                    case ResponseCodeType.ErrorInternalServerTransientError:
                                        break;

                                    case ResponseCodeType.ErrorInternalServerError:
                                        goto IL_185;

                                    default:
                                        goto IL_185;
                                    }
                                }
                            }
                            else
                            {
                                switch (responseCodeType2)
                                {
                                case ResponseCodeType.ErrorMailboxMoveInProgress:
                                case ResponseCodeType.ErrorMailboxStoreUnavailable:
                                    break;

                                default:
                                    if (responseCodeType2 != ResponseCodeType.ErrorNotEnoughMemory)
                                    {
                                        goto IL_185;
                                    }
                                    break;
                                }
                            }
                        }
                        else if (responseCodeType2 <= ResponseCodeType.ErrorTimeoutExpired)
                        {
                            if (responseCodeType2 != ResponseCodeType.ErrorRequestAborted && responseCodeType2 != ResponseCodeType.ErrorServerBusy && responseCodeType2 != ResponseCodeType.ErrorTimeoutExpired)
                            {
                                goto IL_185;
                            }
                        }
                        else if (responseCodeType2 <= ResponseCodeType.ErrorMailboxFailover)
                        {
                            if (responseCodeType2 != ResponseCodeType.ErrorTooManyObjectsOpened && responseCodeType2 != ResponseCodeType.ErrorMailboxFailover)
                            {
                                goto IL_185;
                            }
                        }
                        else if (responseCodeType2 != ResponseCodeType.ErrorUMServerUnavailable && responseCodeType2 != ResponseCodeType.ErrorLocationServicesRequestTimedOut)
                        {
                            goto IL_185;
                        }
                        retry = true;
                        IL_185:;
                    }
                    catch (AuditLogException ex2)
                    {
                        exception        = ex2;
                        WebException ex3 = ex2.InnerException as WebException;
                        if (ex3 != null)
                        {
                            WebExceptionStatus status = ex3.Status;
                            if (status == WebExceptionStatus.Timeout)
                            {
                                retry = true;
                            }
                            else
                            {
                                retry = false;
                            }
                        }
                        else
                        {
                            retry = false;
                        }
                    }
                    if (exception != null && this.Tracer.IsTraceEnabled(TraceType.DebugTrace))
                    {
                        this.Tracer.TraceDebug((long)this.GetHashCode(), "AuditDatabaseWriter.Write. Failed while processing audit record: Id={0}, OrgId={1}, Operation={2}, Type={3}. Error: {4}", new object[]
                        {
                            record.Id,
                            record.OrganizationId,
                            record.Operation,
                            record.RecordType,
                            exception
                        });
                    }
                });
            }
            catch (GrayException exception)
            {
                GrayException exception7;
                exception = exception7;
                retry     = false;
                if (exception != null && this.Tracer.IsTraceEnabled(TraceType.ErrorTrace))
                {
                    this.Tracer.TraceError((long)this.GetHashCode(), "AuditDatabaseWriter.Write. Failed while processing audit record: Id={0}, OrgId={1}, Operation={2}, Type={3}. Error: {4}", new object[]
                    {
                        record.Id,
                        record.OrganizationId,
                        record.Operation,
                        record.RecordType,
                        exception
                    });
                }
            }
            finally
            {
                if (exception != null)
                {
                    AuditHealthInfo.Instance.AddException(exception);
                    AuditDatabaseWriterHealth instance = Singleton <AuditDatabaseWriterHealth> .Instance;
                    instance.Add(new RecordProcessingResult(record, exception, retry));
                    if (!retry)
                    {
                        instance.FailedBatchCount++;
                    }
                }
            }
            return(!retry);
        }
 public PhotobucketWebException(WebException webEx)
     : base(webEx.Message, webEx)
 {
 }
Exemplo n.º 29
0
        private RavenJToken HandleErrors(WebException e)
        {
            var httpWebResponse = e.Response as HttpWebResponse;

            if (httpWebResponse == null ||
                httpWebResponse.StatusCode == HttpStatusCode.Unauthorized ||
                httpWebResponse.StatusCode == HttpStatusCode.NotFound ||
                httpWebResponse.StatusCode == HttpStatusCode.Conflict)
            {
                int httpResult = -1;
                if (httpWebResponse != null)
                {
                    httpResult = (int)httpWebResponse.StatusCode;
                }

                factory.InvokeLogRequest(owner, () => new RequestResultArgs
                {
                    DurationMilliseconds = CalculateDuration(),
                    Method     = webRequest.Method,
                    HttpResult = httpResult,
                    Status     = RequestStatus.ErrorOnServer,
                    Result     = e.Message,
                    Url        = webRequest.RequestUri.PathAndQuery,
                    PostedData = postedData
                });

                return(null);               //throws
            }

            if (httpWebResponse.StatusCode == HttpStatusCode.NotModified &&
                CachedRequestDetails != null)
            {
                factory.UpdateCacheTime(this);
                var result = factory.GetCachedResponse(this);

                factory.InvokeLogRequest(owner, () => new RequestResultArgs
                {
                    DurationMilliseconds = CalculateDuration(),
                    Method     = webRequest.Method,
                    HttpResult = (int)httpWebResponse.StatusCode,
                    Status     = RequestStatus.Cached,
                    Result     = result.ToString(),
                    Url        = webRequest.RequestUri.PathAndQuery,
                    PostedData = postedData
                });

                return(result);
            }

            using (var sr = new StreamReader(e.Response.GetResponseStreamWithHttpDecompression()))
            {
                var readToEnd = sr.ReadToEnd();

                factory.InvokeLogRequest(owner, () => new RequestResultArgs
                {
                    DurationMilliseconds = CalculateDuration(),
                    Method     = webRequest.Method,
                    HttpResult = (int)httpWebResponse.StatusCode,
                    Status     = RequestStatus.Cached,
                    Result     = readToEnd,
                    Url        = webRequest.RequestUri.PathAndQuery,
                    PostedData = postedData
                });

                if (string.IsNullOrWhiteSpace(readToEnd))
                {
                    return(null);                   // throws
                }
                RavenJObject ravenJObject;
                try
                {
                    ravenJObject = RavenJObject.Parse(readToEnd);
                }
                catch (Exception)
                {
                    throw new InvalidOperationException(readToEnd, e);
                }

                if (ravenJObject.ContainsKey("Error"))
                {
                    var sb = new StringBuilder();
                    foreach (var prop in ravenJObject)
                    {
                        if (prop.Key == "Error")
                        {
                            continue;
                        }

                        sb.Append(prop.Key).Append(": ").AppendLine(prop.Value.ToString(Formatting.Indented));
                    }

                    sb.AppendLine()
                    .AppendLine(ravenJObject.Value <string>("Error"));

                    throw new InvalidOperationException(sb.ToString(), e);
                }
                throw new InvalidOperationException(readToEnd, e);
            }
        }
        /// <summary>
        /// http://fabric/app/service/#/partitionkey/any|primary|secondary/endpoint-name/api-path
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        protected override async Task <HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
        {
            //this class will process the request if this this a "fabric" targeted request otherwise it will delegate it for the base class
            if (request.RequestUri.Host.Equals("fabric"))
            {
                ServicePartitionResolver resolver   = ServicePartitionResolver.GetDefault();
                ResolvedServicePartition partition  = null;
                HttpServiceUriBuilder    uriBuilder = new HttpServiceUriBuilder(request.RequestUri);

                int  retries        = MaxRetries;
                int  retryDelay     = InitialRetryDelayMs;
                bool resolveAddress = true;

                HttpResponseMessage lastResponse  = null;
                Exception           lastException = null;

                while (retries-- > 0)
                {
                    lastResponse = null;
                    cancellationToken.ThrowIfCancellationRequested();

                    if (resolveAddress)
                    {
                        partition = partition != null
                            ? await resolver.ResolveAsync(partition, cancellationToken)
                            : await resolver.ResolveAsync(uriBuilder.ServiceName, uriBuilder.PartitionKey, cancellationToken);

                        string serviceEndpointJson;

                        switch (uriBuilder.Target)
                        {
                        case HttpServiceUriTarget.Default:
                        case HttpServiceUriTarget.Primary:
                            serviceEndpointJson = partition.GetEndpoint().Address;
                            break;

                        case HttpServiceUriTarget.Secondary:
                            serviceEndpointJson = partition.Endpoints.ElementAt(this.random.Next(1, partition.Endpoints.Count)).Address;
                            break;

                        case HttpServiceUriTarget.Any:
                        default:
                            serviceEndpointJson = partition.Endpoints.ElementAt(this.random.Next(0, partition.Endpoints.Count)).Address;
                            break;
                        }

                        string endpointUrl = JObject.Parse(serviceEndpointJson)["Endpoints"][uriBuilder.EndpointName].Value <string>();

                        request.RequestUri = new Uri($"{endpointUrl.TrimEnd('/')}/{uriBuilder.ServicePathAndQuery.TrimStart('/')}", UriKind.Absolute);
                    }

                    try
                    {
                        lastResponse = await base.SendAsync(request, cancellationToken);

                        if (lastResponse.StatusCode == HttpStatusCode.NotFound ||
                            lastResponse.StatusCode == HttpStatusCode.ServiceUnavailable)
                        {
                            resolveAddress = true;
                        }
                        else
                        {
                            return(lastResponse);
                        }
                    }
                    catch (TimeoutException te)
                    {
                        lastException  = te;
                        resolveAddress = true;
                    }
                    catch (SocketException se)
                    {
                        lastException  = se;
                        resolveAddress = true;
                    }
                    catch (HttpRequestException hre)
                    {
                        lastException  = hre;
                        resolveAddress = true;
                    }
                    catch (Exception ex)
                    {
                        lastException = ex;
                        WebException we = ex as WebException;

                        if (we == null)
                        {
                            we = ex.InnerException as WebException;
                        }

                        if (we != null)
                        {
                            HttpWebResponse errorResponse = we.Response as HttpWebResponse;

                            // the following assumes port sharing
                            // where a port is shared by multiple replicas within a host process using a single web host (e.g., http.sys).
                            if (we.Status == WebExceptionStatus.ProtocolError)
                            {
                                if (errorResponse.StatusCode == HttpStatusCode.NotFound ||
                                    errorResponse.StatusCode == HttpStatusCode.ServiceUnavailable)
                                {
                                    // This could either mean we requested an endpoint that does not exist in the service API (a user error)
                                    // or the address that was resolved by fabric client is stale (transient runtime error) in which we should re-resolve.
                                    resolveAddress = true;
                                }

                                // On any other HTTP status codes, re-throw the exception to the caller.
                                throw;
                            }

                            if (we.Status == WebExceptionStatus.Timeout ||
                                we.Status == WebExceptionStatus.RequestCanceled ||
                                we.Status == WebExceptionStatus.ConnectionClosed ||
                                we.Status == WebExceptionStatus.ConnectFailure)
                            {
                                resolveAddress = true;
                            }
                        }
                        else
                        {
                            throw;
                        }
                    }

                    await Task.Delay(retryDelay);

                    retryDelay += retryDelay;
                }

                if (lastResponse != null)
                {
                    return(lastResponse);
                }
                else
                {
                    throw lastException;
                }
            }
            else
            {
                return(await base.SendAsync(request, cancellationToken));
            }
        }
Exemplo n.º 31
0
        /// <summary>
        /// Parses the Response and throws appropriate exceptions.
        /// </summary>
        /// <param name="webException">Web Exception.</param>
        /// <param name="isIps">Specifies whether the exception is generated by an IPS call.</param>
        /// <returns>Ids Exception.</returns>
        internal IdsException ParseResponseAndThrowException(WebException webException, bool isIps = false)
        {
            IdsException idsException = null;

            // Checks whether the webException is null or not.
            if (webException != null)
            {
                // If not null then check the response property of the webException object.
                if (webException.Response != null)
                {
                    // There is a response from the Ids server. Cast it to HttpWebResponse.
                    HttpWebResponse errorResponse = (HttpWebResponse)webException.Response;

                    // Get the status code description of the error response.
                    string statusCodeDescription = errorResponse.StatusCode.ToString();

                    // Get the status code of the error response.
                    int    statusCode  = (int)errorResponse.StatusCode;
                    string errorString = string.Empty;

                    ICompressor responseCompressor = CoreHelper.GetCompressor(this.context, false);
                    if (!string.IsNullOrWhiteSpace(errorResponse.ContentEncoding) && responseCompressor != null)
                    {
                        using (var responseStream = errorResponse.GetResponseStream())
                        {
                            using (var decompressedStream = responseCompressor.Decompress(responseStream))
                            {
                                // Get the response stream.
                                StreamReader reader = new StreamReader(decompressedStream);

                                // Read the Stream
                                errorString = reader.ReadToEnd();
                                // Close reader
                                reader.Close();
                            }
                        }
                    }
                    else
                    {
                        using (Stream responseStream = errorResponse.GetResponseStream())
                        {
                            // Get the response stream.
                            StreamReader reader = new StreamReader(responseStream);

                            // Read the Stream
                            errorString = reader.ReadToEnd();
                            // Close reader
                            reader.Close();
                        }
                    }
                    string response_intuit_tid_header = "";
                    //get intuit_tid header
                    for (int i = 0; i < errorResponse.Headers.Count; ++i)
                    {
                        if (errorResponse.Headers.Keys[i] == "intuit_tid")
                        {
                            response_intuit_tid_header = errorResponse.Headers[i];
                        }
                    }

                    // Log the error string to disk.
                    CoreHelper.GetRequestLogging(this.context).LogPlatformRequests(" Response Intuit_Tid header: " + response_intuit_tid_header + ", Response Payload: " + errorString, false);

                    if (isIps)
                    {
                        IdsException exception = new IdsException(errorString, statusCode.ToString(CultureInfo.InvariantCulture), webException.Source);
                        this.context.IppConfiguration.Logger.CustomLogger.Log(TraceLevel.Error, exception.ToString());
                        return(exception);
                    }

                    // Use the above idsException to set to innerException of the specific exception which will be created below.

                    // Ids will set the following error codes. Depending on that we will be throwing specific exceptions.
                    switch (errorResponse.StatusCode)
                    {
                    // Bad Request: 400
                    case HttpStatusCode.BadRequest:
                        // Parse the error response and create the aggregate exception.
                        idsException = this.ParseErrorResponseAndPrepareException(errorString);
                        idsException = new IdsException(statusCodeDescription, statusCode.ToString(CultureInfo.InvariantCulture), webException.Source, idsException);
                        break;

                    // Unauthorized: 401
                    case HttpStatusCode.Unauthorized:
                        // Create Invalid Token Exception.
                        idsException = this.ParseErrorResponseAndPrepareException(errorString);
                        InvalidTokenException invalidTokenException = new InvalidTokenException(string.Format(CultureInfo.InvariantCulture, "{0}-{1}", statusCodeDescription, statusCode), idsException);
                        idsException = invalidTokenException;
                        break;

                    // ServiceUnavailable: 503
                    case HttpStatusCode.ServiceUnavailable:
                    // InternalServerError: 500
                    case HttpStatusCode.InternalServerError:
                    // Forbidden: 403
                    case HttpStatusCode.Forbidden:
                    // NotFound: 404
                    case HttpStatusCode.NotFound:
                        idsException = new IdsException(statusCodeDescription, statusCode.ToString(CultureInfo.InvariantCulture), webException.Source, new EndpointNotFoundException());
                        break;

                    // Throttle Exceeded: 429
                    case (HttpStatusCode)429:
                        idsException = new IdsException(statusCodeDescription, statusCode.ToString(CultureInfo.InvariantCulture), webException.Source, new ThrottleExceededException());
                        break;

                    // Default. Throw generic exception i.e. IdsException.
                    default:
                        // Parse the error response and create the aggregate exception.
                        // TODO: Do we need to give error string in exception also. If so then uncomemnt the below line.
                        // idsException = new IdsException(errorString, statusCode.ToString(CultureInfo.InvariantCulture), webException.Source);
                        idsException = new IdsException(statusCodeDescription, statusCode.ToString(CultureInfo.InvariantCulture), webException.Source);
                        break;
                    }
                }
            }

            // Return the Ids Exception.
            return(idsException);
        }
Exemplo n.º 32
0
        private bool ShouldRecalculateBackendOnHead(WebException webException)
        {
            bool flag;

            return(webException != null && webException.Response != null && ((base.AuthBehavior.AuthState != AuthState.BackEndFullAuth && base.IsAuthenticationChallengeFromBackend(webException) && base.TryFindKerberosChallenge(webException.Response.Headers[Constants.AuthenticationHeader], out flag)) || base.HandleRoutingError((HttpWebResponse)webException.Response)));
        }
Exemplo n.º 33
0
 internal WebExceptionBase(string message, WebException webException)
     : base(message, webException, webException.Status, webException.Response)
 {
 }
Exemplo n.º 34
0
 public static bool HasStatus(this WebException webEx, HttpStatusCode statusCode)
 {
     return(GetStatus(webEx) == statusCode);
 }
Exemplo n.º 35
0
        public void Status_IsNotTransient(WebExceptionStatus status)
        {
            var ex = new WebException(status.ToString(), status);

            Assert.IsFalse(_retryStrategy.IsTransient(ex));
        }
Exemplo n.º 36
0
        void responseCb(IAsyncResult ar)
        {
            try
            {
                Request request = (Request)ar.AsyncState;
                if (request.was_aborted)
                {
                    FunDebug.Log("Dedicated Server - Response callback. Request aborted.");
                    return;
                }

                request.web_response = (HttpWebResponse)request.web_request.EndGetResponse(ar);
                request.web_request  = null;

                if (request.web_response.StatusCode == HttpStatusCode.OK)
                {
                    byte[]   header     = request.web_response.Headers.ToByteArray();
                    string   str_header = System.Text.Encoding.ASCII.GetString(header, 0, header.Length);
                    string[] lines      = str_header.Replace("\r", "").Split('\n');

                    int length = 0;

                    foreach (string n in lines)
                    {
                        if (n.Length > 0)
                        {
                            string[] tuple = n.Split(kHeaderSeparator, StringSplitOptions.RemoveEmptyEntries);
                            string   key   = tuple[0].ToLower();
                            if (key == "content-length" && tuple.Length >= 2)
                            {
                                length = Convert.ToInt32(tuple[1]);
                                break;
                            }
                        }
                    }

                    byte[] buffer = new byte[length];
                    request.body = new ArraySegment <byte>(buffer);

                    request.read_stream = request.web_response.GetResponseStream();
                    request.read_stream.BeginRead(buffer, 0, length, new AsyncCallback(readCb), request);
                }
                else
                {
                    FunDebug.LogError("Host manager response failed. status:{0}",
                                      request.web_response.StatusDescription);
                }
            }
            catch (Exception e)
            {
                WebException we = e as WebException;
                if (we != null && we.Status == WebExceptionStatus.ConnectFailure)
                {
                    onRequestFailed((Request)ar.AsyncState);
                }
                else if ((we != null && we.Status == WebExceptionStatus.RequestCanceled) ||
                         (e is ObjectDisposedException || e is NullReferenceException))
                {
                    // When Stop is called HttpWebRequest.EndGetResponse may return a Exception
                    FunDebug.LogDebug("Dedicated server request operation has been cancelled.");
                }
            }
        }
Exemplo n.º 37
0
        public static HttpStatusCode?GetHttpStatusCode(WebException wex)
        {
            var errorResponse = wex.Response as HttpWebResponse;

            return(errorResponse?.StatusCode);
        }
 private static void ProcessWebException(WebException e)
 {
     Console.WriteLine("{0}", e.ToString());
     // Obtain detailed error information
     string strResponse = string.Empty;
     using (HttpWebResponse response = (HttpWebResponse)e.Response)
     {
         using (Stream responseStream = response.GetResponseStream())
         {
             using (StreamReader sr = new StreamReader(responseStream, System.Text.Encoding.ASCII))
             {
                 strResponse = sr.ReadToEnd();
             }
         }
     }
     Console.WriteLine("Http status code={0}, error message={1}", e.Status, strResponse);
 }
Exemplo n.º 39
0
        public string DownloadProgramme(string progExtId, string episodeExtId, ProgrammeInfo progInfo, EpisodeInfo epInfo, string finalName)
        {
            XmlDocument         rss           = this.LoadFeedXml(new Uri(progExtId));
            XmlNamespaceManager namespaceMgr  = this.CreateNamespaceMgr(rss);
            XmlNode             itemNode      = this.ItemNodeFromEpisodeID(rss, episodeExtId);
            XmlNode             enclosureNode = itemNode.SelectSingleNode("./enclosure");
            XmlAttribute        urlAttrib     = enclosureNode.Attributes["url"];
            Uri downloadUrl = new Uri(urlAttrib.Value);

            int    fileNamePos  = finalName.LastIndexOf("\\", StringComparison.Ordinal);
            int    extensionPos = downloadUrl.AbsolutePath.LastIndexOf(".", StringComparison.Ordinal);
            string extension    = "mp3";

            if (extensionPos > -1)
            {
                extension = downloadUrl.AbsolutePath.Substring(extensionPos + 1);
            }

            using (TempFile downloadFile = new TempFile(extension))
            {
                finalName += "." + extension;

                this.doDownload = new DownloadWrapper(downloadUrl, downloadFile.FilePath);
                this.doDownload.DownloadProgress += this.DoDownload_DownloadProgress;
                this.doDownload.Download();

                while ((!this.doDownload.Complete) && this.doDownload.Error == null)
                {
                    Thread.Sleep(500);
                }

                if (this.doDownload.Error != null)
                {
                    if (this.doDownload.Error is WebException)
                    {
                        WebException webExp = (WebException)this.doDownload.Error;

                        if (webExp.Status == WebExceptionStatus.NameResolutionFailure)
                        {
                            throw new DownloadException(ErrorType.NetworkProblem, "Unable to resolve " + downloadUrl.Host + " to download this episode from.  Check your internet connection or try again later.");
                        }
                        else if (webExp.Response is HttpWebResponse)
                        {
                            HttpWebResponse webErrorResponse = (HttpWebResponse)webExp.Response;

                            switch (webErrorResponse.StatusCode)
                            {
                            case HttpStatusCode.Forbidden:
                                throw new DownloadException(ErrorType.RemoteProblem, downloadUrl.Host + " returned a status 403 (Forbidden) in response to the request for this episode.  You may need to contact the podcast publisher if this problem persists.");

                            case HttpStatusCode.NotFound:
                                throw new DownloadException(ErrorType.NotAvailable, "This episode appears to be no longer available.  You can either try again later, or cancel the download to remove it from the list and clear the error.");
                            }
                        }
                    }

                    throw this.doDownload.Error;
                }

                if (this.Progress != null)
                {
                    this.Progress(100, ProgressType.Processing);
                }

                File.Move(downloadFile.FilePath, finalName);
            }

            return(extension);
        }
Exemplo n.º 40
0
    public void TestPokitDokException_StringExceptionConstructor()
    {
        WebException innerException = new WebException();

        PokitDokException exception = new PokitDokException("Test Message.", innerException);
        Assert.AreEqual("Test Message.", exception.Message);
    }
Exemplo n.º 41
0
        /// <summary>
        /// Determines whether this WebException represents a partial upload
        /// success or not.
        /// </summary>
        /// <param name="e">The web exception.</param>
        /// <returns>true, if this exception represents a successful partial
        /// upload, false otherwise.</returns>
        private bool IsPartialUploadSuccessResponse(WebException e)
        {
            HttpWebResponse response = e.Response as HttpWebResponse;

            return(response != null && (int)response.StatusCode == 308);
        }
Exemplo n.º 42
0
    public void TestPokitDokException_VerifyInnerException()
    {
        WebException innerException = new WebException("Alternative Test Message");

        PokitDokException exception = new PokitDokException("Test Message.", innerException);
        Assert.AreEqual("Test Message.", exception.Message);
        Assert.AreEqual(exception.InnerException, innerException);
    }
 protected abstract void AssertWebException(WebException e);
Exemplo n.º 44
0
        public void WebExceptionGetObjectDataTest()
        {
            WebException source, target;
            source = new WebException();

            //FUTUREDEV: Set any custom data properties and verify values after deserialization

            using (Stream formatStream = new MemoryStream()) {
                BinaryFormatter formatter = new BinaryFormatter();
                formatter.Serialize(formatStream, source);
                formatStream.Position = 0; //NOTE:  Reset stream

                target = (WebException)formatter.Deserialize(formatStream); //NOTE:  This will cause a call to GetObjectData
            }

            Assert.IsNotNull(target);
        }
Exemplo n.º 45
0
        /// <summary>
        /// Handles WebException. Determines whether it is due to cancelled operation, remoteexception from server or some other webexception
        /// </summary>
        /// <param name="e">WebException instance</param>
        /// <param name="resp">OperationResponse</param>
        /// <param name="path">Path</param>
        /// <param name="requestId">Request Id</param>
        /// <param name="token">Auth token</param>
        /// <param name="webReq">Http Web request</param>
        /// <param name="timeoutCancelToken">Cancel Token for timeout</param>
        /// <param name="actualCancelToken">Cancel Token sent by user</param>
        private static void HandleWebException(WebException e, OperationResponse resp, string path, string requestId, string token, HttpWebRequest webReq, CancellationToken timeoutCancelToken, CancellationToken actualCancelToken = default(CancellationToken))
        {
            // The status property will be set to RequestCanceled after Abort.
            if (timeoutCancelToken.IsCancellationRequested)
            {
                // Type should not be of operationcancelledexception otherwise this wont be retried
                resp.Ex = new Exception("Operation timed out");
            }
            else if (actualCancelToken.IsCancellationRequested)
            {
                resp.Ex = new OperationCanceledException(actualCancelToken);
            }
            //This the case where some exception occured in server but server returned a response
            else if (e.Status == WebExceptionStatus.ProtocolError)
            {
                try
                {
                    using (var errorResponse = (HttpWebResponse)e.Response)
                    {
                        PostPowershellLogDetails(webReq, errorResponse);
                        resp.HttpStatus = errorResponse.StatusCode;
                        resp.RequestId  = errorResponse.Headers["x-ms-request-id"];
                        if (resp.HttpStatus == HttpStatusCode.Unauthorized && TokenLog.IsDebugEnabled)
                        {
                            string tokenLogLine =
                                $"HTTPRequest,HTTP401,cReqId:{requestId},sReqId:{resp.RequestId},path:{path},token:{token}";
                            TokenLog.Debug(tokenLogLine);
                        }
                        resp.HttpMessage = errorResponse.StatusDescription;
                        ByteBuffer errorResponseData = default(ByteBuffer);
                        if (!InitializeResponseData(errorResponse, ref errorResponseData, true))
                        {
                            throw new ArgumentException("ContentLength of error response stream is not set");
                        }
                        using (Stream errorStream = errorResponse.GetResponseStream())
                        {
                            // Reading the data from the error response into a byte array is necessary to show the actual error data as a part of the
                            // error message in case JSON parsing does not work. We read the bytes and then pass it back to JsonTextReader using a memorystream
                            int noBytes;
                            int totalLengthToRead = errorResponseData.Count;
                            do
                            {
                                noBytes = errorStream.Read(errorResponseData.Data, errorResponseData.Offset, totalLengthToRead);
                                errorResponseData.Offset += noBytes;
                                totalLengthToRead        -= noBytes;
                            } while (noBytes > 0 && totalLengthToRead > 0);

                            ParseRemoteError(errorResponseData.Data, errorResponseData.Count, resp, errorResponse.Headers["Content-Type"]);
                        }
                    }
                }
                catch (Exception ex)
                {
                    resp.Ex = ex;
                }
            }
            else//No response stream is returned, Dont know what to do, so just store the exception
            {
                switch (e.Status)
                {
                case WebExceptionStatus.NameResolutionFailure:
                case WebExceptionStatus.ServerProtocolViolation:
                case WebExceptionStatus.ConnectFailure:
                case WebExceptionStatus.ConnectionClosed:
                case WebExceptionStatus.KeepAliveFailure:
                case WebExceptionStatus.ReceiveFailure:
                case WebExceptionStatus.SendFailure:
                case WebExceptionStatus.Timeout:
                case WebExceptionStatus.UnknownError:
                    resp.ConnectionFailure = true;
                    break;
                }
                resp.Ex = e;
            }
        }
Exemplo n.º 46
0
        public void WebExceptionConstructorMessageInnerExceptionTest()
        {
            string expectedMessage = "Exception Message";
            Exception expectedInnerException = new NotImplementedException();
            WebException target = new WebException(expectedMessage, expectedInnerException);
            Assert.IsNotNull(target);

            string actualMessage = target.Message;
            Assert.AreEqual(expectedMessage, actualMessage);

            Exception actualInnerException = target.InnerException;
            Assert.AreEqual(expectedInnerException, actualInnerException);
        }
Exemplo n.º 47
0
        private async void btnRun_Click(object sender, EventArgs e)
        {
            if (btnRun.Enabled == false)
            {
                return;
            }

            btnRun.Enabled = false;

            if (CheckWebsiteIsRunning("http://www.fish-ajax-cors.com/") == false ||                     // CrosClientWebSite1
                CheckWebsiteIsRunning("http://www.fish-mvc-demo.com/") == false)                        // DemoWebSite1

            {
                btnRun.Enabled = true;
                return;
            }


            _httpLog.Clear();
            txtHttpLog.Text    = string.Empty;
            txtTestResult.Text = string.Empty;
            StringBuilder sb    = new StringBuilder();
            int           count = 0;

            foreach (ListViewItem item in this.listView1.Items)
            {
                item.ImageIndex = 0;
            }

            foreach (ListViewItem item in this.listView1.Items)
            {
                if (item.Checked)
                {
                    count++;

                    MethodInfo m = (MethodInfo)item.Tag;
                    item.ImageIndex = 1;

                    try {
                        object instance = Activator.CreateInstance(m.DeclaringType);
                        Task   task     = m.Invoke(instance, null) as Task;
                        if (task != null)
                        {
                            await task;
                        }

                        //Task.Run(() => m.Invoke(instance, null)).Wait();

                        item.ImageIndex = 2;
                    }
                    catch (Exception ex) {
                        item.ImageIndex = 3;

                        string       errorMessage = null;
                        WebException webException = ex as WebException;
                        if (webException != null)
                        {
                            HttpClient client = new HttpClient();
                            string     html   = client.TryReadResponseException(webException);
                            errorMessage = GetHtmlTitle(html);
                        }


                        sb.AppendLine("Method: " + m.DeclaringType.FullName + "." + m.Name)
                        .AppendLine("---------------------------------------------------")
                        .Append(errorMessage ?? ex.GetBaseException().Message)
                        .AppendLine("\r\n---------------------------------------------------\r\n\r\n");
                    }
                }
            }


            btnRun.Enabled = true;

            if (count == 0)
            {
                txtTestResult.Text = "没有需要执行的测试用例。";
            }
            else
            {
                if (sb.Length == 0)
                {
                    txtTestResult.Text = "全部测试用例已通过。";
                }
                else
                {
                    txtTestResult.Text = sb.ToString();
                }
            }

            txtHttpLog.Text = _httpLog.ToString();
            _httpLog.Clear();
        }
Exemplo n.º 48
0
		private static Exception ThrowConcurrencyException(WebException e)
		{
			using (var sr = new StreamReader(e.Response.GetResponseStreamWithHttpDecompression()))
			{
				var text = sr.ReadToEnd();
				var errorResults = JsonConvert.DeserializeAnonymousType(text, new
				{
					url = (string)null,
					actualETag = Guid.Empty,
					expectedETag = Guid.Empty,
					error = (string)null
				});
				return new ConcurrencyException(errorResults.error)
				{
					ActualETag = errorResults.actualETag,
					ExpectedETag = errorResults.expectedETag
				};
			}
		}
Exemplo n.º 49
0
        bool Redirect(HttpStatusCode code)
        {
            _redirects++;
            Exception e = null;
            string uriString = null;
            switch (code)
            {
                case HttpStatusCode.Ambiguous: // 300
                    e = new WebException("Ambiguous redirect.");
                    break;
                case HttpStatusCode.MovedPermanently: // 301
                case HttpStatusCode.Redirect: // 302
                    if (_method == "POST")
                        _method = "GET";
                    break;
                case HttpStatusCode.TemporaryRedirect: // 307
                    break;
                case HttpStatusCode.SeeOther: //303
                    _method = "GET";
                    break;
                case HttpStatusCode.NotModified: // 304
                    return false;
                case HttpStatusCode.UseProxy: // 305
                    e = new NotImplementedException("Proxy support not available.");
                    break;
                case HttpStatusCode.Unused: // 306
                default:
                    e = new ProtocolViolationException("Invalid status code: " + (int)code);
                    break;
            }

            if (e != null)
                throw e;

            _contentLength = -1;
            //bodyBufferLength = 0;
            //bodyBuffer = null;
            uriString = _webResponse.Headers["Location"];

            if (uriString == null)
            {
                throw new WebException("No Location header found for " + (int)code,
                    WebExceptionStatus.ProtocolError);
            }

            var prev = Address;
            try
            {
                Address = new Uri(Address, uriString);
            }
            catch (Exception)
            {
                throw new WebException(String.Format("Invalid URL ({0}) for {1}",
                    uriString, (int)code),
                    WebExceptionStatus.ProtocolError);
            }

            _hostChanged = (Address.Scheme != prev.Scheme || Host != prev.Authority);

            return true;
        }