/// <summary> /// the main execution method for cmdlets derived from WebRequestPSCmdlet. /// </summary> protected override void ProcessRecord() { try { // Set cmdlet context for write progress ValidateParameters(); PrepareSession(); // if the request contains an authorization header and PreserveAuthorizationOnRedirect is not set, // it needs to be stripped on the first redirect. bool stripAuthorization = null != WebSession && null != WebSession.Headers && !PreserveAuthorizationOnRedirect.IsPresent && WebSession.Headers.ContainsKey(HttpKnownHeaderNames.Authorization.ToString()); using (HttpClient client = GetHttpClient(stripAuthorization)) { int followedRelLink = 0; Uri uri = Uri; do { if (followedRelLink > 0) { string linkVerboseMsg = string.Format(CultureInfo.CurrentCulture, WebCmdletStrings.FollowingRelLinkVerboseMsg, uri.AbsoluteUri); WriteVerbose(linkVerboseMsg); } using (HttpRequestMessage request = GetRequest(uri, stripAuthorization: false)) { FillRequestStream(request); try { long requestContentLength = 0; if (request.Content != null) { requestContentLength = request.Content.Headers.ContentLength.Value; } string reqVerboseMsg = String.Format(CultureInfo.CurrentCulture, WebCmdletStrings.WebMethodInvocationVerboseMsg, request.Method, request.RequestUri, requestContentLength); WriteVerbose(reqVerboseMsg); HttpResponseMessage response = GetResponse(client, request, stripAuthorization); string contentType = ContentHelper.GetContentType(response); string respVerboseMsg = string.Format(CultureInfo.CurrentCulture, WebCmdletStrings.WebResponseVerboseMsg, response.Content.Headers.ContentLength, contentType); WriteVerbose(respVerboseMsg); if (!response.IsSuccessStatusCode) { string message = String.Format(CultureInfo.CurrentCulture, WebCmdletStrings.ResponseStatusCodeFailure, (int)response.StatusCode, response.ReasonPhrase); HttpResponseException httpEx = new HttpResponseException(message, response); ErrorRecord er = new ErrorRecord(httpEx, "WebCmdletWebResponseException", ErrorCategory.InvalidOperation, request); string detailMsg = ""; StreamReader reader = null; try { reader = new StreamReader(StreamHelper.GetResponseStream(response)); // remove HTML tags making it easier to read detailMsg = System.Text.RegularExpressions.Regex.Replace(reader.ReadToEnd(), "<[^>]*>", ""); } catch (Exception) { // catch all } finally { if (reader != null) { reader.Dispose(); } } if (!String.IsNullOrEmpty(detailMsg)) { er.ErrorDetails = new ErrorDetails(detailMsg); } ThrowTerminatingError(er); } if (_parseRelLink || _followRelLink) { ParseLinkHeader(response, uri); } ProcessResponse(response); UpdateSession(response); // If we hit our maximum redirection count, generate an error. // Errors with redirection counts of greater than 0 are handled automatically by .NET, but are // impossible to detect programmatically when we hit this limit. By handling this ourselves // (and still writing out the result), users can debug actual HTTP redirect problems. if (WebSession.MaximumRedirection == 0) // Indicate "HttpClientHandler.AllowAutoRedirect == false" { if (response.StatusCode == HttpStatusCode.Found || response.StatusCode == HttpStatusCode.Moved || response.StatusCode == HttpStatusCode.MovedPermanently) { ErrorRecord er = new ErrorRecord(new InvalidOperationException(), "MaximumRedirectExceeded", ErrorCategory.InvalidOperation, request); er.ErrorDetails = new ErrorDetails(WebCmdletStrings.MaximumRedirectionCountExceeded); WriteError(er); } } } catch (HttpRequestException ex) { ErrorRecord er = new ErrorRecord(ex, "WebCmdletWebResponseException", ErrorCategory.InvalidOperation, request); if (ex.InnerException != null) { er.ErrorDetails = new ErrorDetails(ex.InnerException.Message); } ThrowTerminatingError(er); } if (_followRelLink) { if (!_relationLink.ContainsKey("next")) { return; } uri = new Uri(_relationLink["next"]); followedRelLink++; } } }while (_followRelLink && (followedRelLink < _maximumFollowRelLink)); } } catch (CryptographicException ex) { ErrorRecord er = new ErrorRecord(ex, "WebCmdletCertificateException", ErrorCategory.SecurityError, null); ThrowTerminatingError(er); } catch (NotSupportedException ex) { ErrorRecord er = new ErrorRecord(ex, "WebCmdletIEDomNotSupportedException", ErrorCategory.NotImplemented, null); ThrowTerminatingError(er); } }
/// <summary> /// the main execution method for cmdlets derived from WebRequestPSCmdlet. /// </summary> protected override void ProcessRecord() { try { // Set cmdlet context for write progress ValidateParameters(); PrepareSession(); using (HttpClient client = GetHttpClient()) using (HttpRequestMessage request = GetRequest(Uri)) { FillRequestStream(request); try { long requestContentLength = 0; if (request.Content != null) { requestContentLength = request.Content.Headers.ContentLength.Value; } string reqVerboseMsg = String.Format(CultureInfo.CurrentCulture, "{0} {1} with {2}-byte payload", request.Method, request.RequestUri, requestContentLength); WriteVerbose(reqVerboseMsg); HttpResponseMessage response = GetResponse(client, request); string contentType = ContentHelper.GetContentType(response); string respVerboseMsg = string.Format(CultureInfo.CurrentCulture, "received {0}-byte response of content type {1}", response.Content.Headers.ContentLength, contentType); WriteVerbose(respVerboseMsg); if (!response.IsSuccessStatusCode) { string message = String.Format(CultureInfo.CurrentCulture, WebCmdletStrings.ResponseStatusCodeFailure, (int)response.StatusCode, response.ReasonPhrase); HttpResponseException httpEx = new HttpResponseException(message, response); ErrorRecord er = new ErrorRecord(httpEx, "WebCmdletWebResponseException", ErrorCategory.InvalidOperation, request); string detailMsg = ""; StreamReader reader = null; try { reader = new StreamReader(StreamHelper.GetResponseStream(response)); // remove HTML tags making it easier to read detailMsg = System.Text.RegularExpressions.Regex.Replace(reader.ReadToEnd(), "<[^>]*>", ""); } catch (Exception) { // catch all } finally { if (reader != null) { reader.Dispose(); } } if (!String.IsNullOrEmpty(detailMsg)) { er.ErrorDetails = new ErrorDetails(detailMsg); } ThrowTerminatingError(er); } ProcessResponse(response); UpdateSession(response); // If we hit our maximum redirection count, generate an error. // Errors with redirection counts of greater than 0 are handled automatically by .NET, but are // impossible to detect programmatically when we hit this limit. By handling this ourselves // (and still writing out the result), users can debug actual HTTP redirect problems. if (WebSession.MaximumRedirection == 0) // Indicate "HttpClientHandler.AllowAutoRedirect == false" { if (response.StatusCode == HttpStatusCode.Found || response.StatusCode == HttpStatusCode.Moved || response.StatusCode == HttpStatusCode.MovedPermanently) { ErrorRecord er = new ErrorRecord(new InvalidOperationException(), "MaximumRedirectExceeded", ErrorCategory.InvalidOperation, request); er.ErrorDetails = new ErrorDetails(WebCmdletStrings.MaximumRedirectionCountExceeded); WriteError(er); } } } catch (HttpRequestException ex) { ErrorRecord er = new ErrorRecord(ex, "WebCmdletWebResponseException", ErrorCategory.InvalidOperation, request); if (ex.InnerException != null) { er.ErrorDetails = new ErrorDetails(ex.InnerException.Message); } ThrowTerminatingError(er); } } } catch (CryptographicException ex) { ErrorRecord er = new ErrorRecord(ex, "WebCmdletCertificateException", ErrorCategory.SecurityError, null); ThrowTerminatingError(er); } catch (NotSupportedException ex) { ErrorRecord er = new ErrorRecord(ex, "WebCmdletIEDomNotSupportedException", ErrorCategory.NotImplemented, null); ThrowTerminatingError(er); } }