private Uri PrepareUri(Uri uri) { uri = CheckProtocol(uri); // before creating the web request, // preprocess Body if content is a dictionary and method is GET (set as query) IDictionary bodyAsDictionary; LanguagePrimitives.TryConvertTo <IDictionary>(Body, out bodyAsDictionary); if ((null != bodyAsDictionary) && ((IsStandardMethodSet() && (Method == WebRequestMethod.Default || Method == WebRequestMethod.Get)) || (IsCustomMethodSet() && CustomMethod.ToUpperInvariant() == "GET"))) { UriBuilder uriBuilder = new UriBuilder(uri); if (uriBuilder.Query != null && uriBuilder.Query.Length > 1) { uriBuilder.Query = uriBuilder.Query.Substring(1) + "&" + FormatDictionary(bodyAsDictionary); } else { uriBuilder.Query = FormatDictionary(bodyAsDictionary); } uri = uriBuilder.Uri; // set body to null to prevent later FillRequestStream Body = null; } return(uri); }
internal virtual WebRequest GetRequest(Uri uri) { uri = PrepareUri(uri); // create the base WebRequest object WebRequest request = WebRequest.Create(uri); // pull in session data if (0 < WebSession.Headers.Count) { try { HttpWebRequest webRequest = request as HttpWebRequest; request.Headers.Clear(); foreach (string key in WebSession.Headers.Keys) { bool setHeaderViaProperty = TryMapHeaderToProperty(webRequest, key); if (!setHeaderViaProperty) { request.Headers[key] = WebSession.Headers[key]; } } } catch (NotImplementedException) { } } // set the credentials used by this request if (WebSession.UseDefaultCredentials) { // the UseDefaultCredentials flag overrides other supplied credentials request.UseDefaultCredentials = true; } else if (null != WebSession.Credentials) { request.Credentials = WebSession.Credentials; } if (null != WebSession.Proxy) { request.Proxy = WebSession.Proxy; } switch (ParameterSetName) { case "StandardMethod": if (WebRequestMethod.Default != Method) { // set the method if the parameter was provided request.Method = Method.ToString().ToUpperInvariant(); } break; case "CustomMethod": // set the method if the parameter was provided request.Method = CustomMethod.ToUpperInvariant(); break; } // pull in http specific properties HttpWebRequest httpRequest = request as HttpWebRequest; if (null != httpRequest) { httpRequest.CookieContainer = WebSession.Cookies; httpRequest.UserAgent = WebSession.UserAgent; if (null != WebSession.Certificates) { httpRequest.ClientCertificates = WebSession.Certificates; } if (-1 < WebSession.MaximumRedirection) { if (WebSession.MaximumRedirection == 0) { httpRequest.AllowAutoRedirect = false; } else { httpRequest.MaximumAutomaticRedirections = WebSession.MaximumRedirection; } } // check timeout setting (in seconds instead of milliseconds as in HttpWebRequest) if (TimeoutSec == 0) { // A zero timeout means infinite httpRequest.Timeout = Timeout.Infinite; } else if (TimeoutSec > 0) { // just to make sure if (TimeoutSec > Int32.MaxValue / 1000) { httpRequest.Timeout = Int32.MaxValue; } else { httpRequest.Timeout = TimeoutSec * 1000; } } // check keep-alive setting if (DisableKeepAlive) { // default value is true, so only need to set if false. httpRequest.KeepAlive = false; } if (null != TransferEncoding) { httpRequest.SendChunked = true; httpRequest.TransferEncoding = TransferEncoding; } } return(request); }
internal virtual void FillRequestStream(HttpRequestMessage request) { if (null == request) { throw new ArgumentNullException("request"); } // set the content type if (ContentType != null) { WebSession.ContentHeaders[HttpKnownHeaderNames.ContentType] = ContentType; //request } // ContentType == null else if (Method == WebRequestMethod.Post || (IsCustomMethodSet() && CustomMethod.ToUpperInvariant() == "POST")) { // Win8:545310 Invoke-WebRequest does not properly set MIME type for POST string contentType = null; WebSession.ContentHeaders.TryGetValue(HttpKnownHeaderNames.ContentType, out contentType); if (string.IsNullOrEmpty(contentType)) { WebSession.ContentHeaders[HttpKnownHeaderNames.ContentType] = "application/x-www-form-urlencoded"; } } // coerce body into a usable form if (Body != null) { object content = Body; // make sure we're using the base object of the body, not the PSObject wrapper PSObject psBody = Body as PSObject; if (psBody != null) { content = psBody.BaseObject; } /* TODO: This needs to be enable after the dependency on mshtml is resolved. * var html = content as HtmlWebResponseObject; * if (html != null) * { * // use the form if it's the only one present * if (html.Forms.Count == 1) * { * SetRequestContent(request, html.Forms[0].Fields); * } * } * else if (content is FormObject) */ if (content is FormObject) { FormObject form = content as FormObject; SetRequestContent(request, form.Fields); } else if (content is IDictionary && request.Method != HttpMethod.Get) { IDictionary dictionary = content as IDictionary; SetRequestContent(request, dictionary); } else if (content is XmlNode) { XmlNode xmlNode = content as XmlNode; SetRequestContent(request, xmlNode); } else if (content is Stream) { Stream stream = content as Stream; SetRequestContent(request, stream); } else if (content is byte[]) { byte[] bytes = content as byte[]; SetRequestContent(request, bytes); } else if (content is MultipartFormDataContent multipartFormDataContent) { WebSession.ContentHeaders.Clear(); SetRequestContent(request, multipartFormDataContent); } else { SetRequestContent(request, (string)LanguagePrimitives.ConvertTo(content, typeof(string), CultureInfo.InvariantCulture)); } } else if (InFile != null) // copy InFile data { try { // open the input file SetRequestContent(request, new FileStream(InFile, FileMode.Open)); } catch (UnauthorizedAccessException) { string msg = string.Format(CultureInfo.InvariantCulture, WebCmdletStrings.AccessDenied, _originalFilePath); throw new UnauthorizedAccessException(msg); } } // Add the content headers if (request.Content != null) { foreach (var entry in WebSession.ContentHeaders) { request.Content.Headers.Add(entry.Key, entry.Value); } } }
internal virtual void FillRequestStream(WebRequest request) { if (null == request) { throw new ArgumentNullException("request"); } // set the content type if (null != ContentType) { request.ContentType = ContentType; } // ContentType == null else if ((IsStandardMethodSet() && Method == WebRequestMethod.Post) || (IsCustomMethodSet() && CustomMethod.ToUpperInvariant() == "POST")) { // Win8:545310 Invoke-WebRequest does not properly set MIME type for POST if (String.IsNullOrEmpty(request.ContentType)) { request.ContentType = "application/x-www-form-urlencoded"; } } // coerce body into a usable form if (null != Body) { object content = Body; // make sure we're using the base object of the body, not the PSObject wrapper PSObject psBody = Body as PSObject; if (null != psBody) { content = psBody.BaseObject; } if (null != content as HtmlWebResponseObject) { HtmlWebResponseObject html = content as HtmlWebResponseObject; // use the form it's the only one present if (html.Forms.Count == 1) { SetRequestContent(request, html.Forms[0].Fields); } } else if (null != content as FormObject) { FormObject form = content as FormObject; SetRequestContent(request, form.Fields); } else if (null != content as IDictionary && request.Method != WebRequestMethods.Http.Get) { IDictionary dictionary = content as IDictionary; SetRequestContent(request, dictionary); } else if (null != content as XmlNode) { XmlNode xmlNode = content as XmlNode; SetRequestContent(request, xmlNode); } else if (null != content as Stream) { Stream stream = content as Stream; SetRequestContent(request, stream); } else if (null != content as byte[]) { byte[] bytes = content as byte[]; SetRequestContent(request, bytes); } else { SetRequestContent(request, (string)LanguagePrimitives.ConvertTo(content, typeof(string), CultureInfo.InvariantCulture)); } } else if (null != InFile) // copy InFile data { try { // open the input file using (FileStream fs = new FileStream(InFile, FileMode.Open)) { SetRequestContent(request, fs); } } catch (UnauthorizedAccessException) { string msg = string.Format(CultureInfo.InvariantCulture, WebCmdletStrings.AccessDenied, _originalFilePath); throw new UnauthorizedAccessException(msg); } } else { request.ContentLength = 0; } }