internal static WebResponseObject GetResponseObject(WebResponse response, Stream responseStream, ExecutionContext executionContext, bool useBasicParsing = false)
 {
     WebResponseObject output;
     if (WebResponseHelper.IsText(response))
     {
         if (!useBasicParsing)
         {
             output = new HtmlWebResponseObject(response, responseStream, executionContext);
         }
         else
         {
             output = new BasicHtmlWebResponseObject(response, responseStream);
         }
     }
     else
     {
         output = new WebResponseObject(response, responseStream);
     }
     return (output);
 }
Example #2
0
        internal static WebResponseObject GetResponseObject(WebResponse response, Stream responseStream, ExecutionContext executionContext, bool useBasicParsing = false)
        {
            WebResponseObject output;

            if (WebResponseHelper.IsText(response))
            {
                if (!useBasicParsing)
                {
                    output = new HtmlWebResponseObject(response, responseStream, executionContext);
                }
                else
                {
                    output = new BasicHtmlWebResponseObject(response, responseStream);
                }
            }
            else
            {
                output = new WebResponseObject(response, responseStream);
            }
            return(output);
        }
Example #3
0
        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;
            }
        }
Example #4
0
 internal virtual void FillRequestStream(WebRequest request)
 {
     if (request == null)
     {
         throw new ArgumentNullException("request");
     }
     if (this.ContentType != null)
     {
         request.ContentType = this.ContentType;
     }
     else if (this.Method == WebRequestMethod.Post)
     {
         request.ContentType = "application/x-www-form-urlencoded";
     }
     if (this.Body != null)
     {
         object   body = this.Body;
         PSObject obj3 = this.Body as PSObject;
         if (obj3 != null)
         {
             body = obj3.BaseObject;
         }
         if (!(body is HtmlWebResponseObject))
         {
             if (body is FormObject)
             {
                 FormObject obj5 = body as FormObject;
                 this.SetRequestContent(request, obj5.Fields);
             }
             else if ((body is IDictionary) && (request.Method != "GET"))
             {
                 IDictionary content = body as IDictionary;
                 this.SetRequestContent(request, content);
             }
             else if (body is XmlNode)
             {
                 XmlNode xmlNode = body as XmlNode;
                 this.SetRequestContent(request, xmlNode);
             }
             else if (body is Stream)
             {
                 Stream contentStream = body as Stream;
                 this.SetRequestContent(request, contentStream);
             }
             else if (body is byte[])
             {
                 byte[] buffer = body as byte[];
                 this.SetRequestContent(request, buffer);
             }
             else
             {
                 this.SetRequestContent(request, (string)LanguagePrimitives.ConvertTo(body, typeof(string), CultureInfo.InvariantCulture));
             }
         }
         else
         {
             HtmlWebResponseObject obj4 = body as HtmlWebResponseObject;
             if (obj4.Forms.Count == 1)
             {
                 this.SetRequestContent(request, obj4.Forms[0].Fields);
             }
         }
     }
     else
     {
         if (this.InFile != null)
         {
             try
             {
                 using (FileStream stream2 = new FileStream(this.InFile, FileMode.Open))
                 {
                     this.SetRequestContent(request, stream2);
                 }
                 return;
             }
             catch (UnauthorizedAccessException)
             {
                 throw new UnauthorizedAccessException(string.Format(CultureInfo.InvariantCulture, WebCmdletStrings.AccessDenied, new object[] { this._originalFilePath }));
             }
         }
         request.ContentLength = 0L;
     }
 }