Esempio n. 1
0
 private FormObjectCollection BuildFormsCollection()
 {
     FormObjectCollection objects = new FormObjectCollection();
     this.EnsureHtmlParser();
     foreach (IHTMLFormElement element in this._parsedHtml.forms)
     {
         string elementId = this.GetElementId(element as IHTMLElement);
         if (elementId == null)
         {
             elementId = element.name;
         }
         FormObject item = new FormObject(elementId, element.method, element.action);
         foreach (IHTMLElement element2 in element)
         {
             IHTMLInputElement element3 = element2 as IHTMLInputElement;
             if (element3 != null)
             {
                 elementId = this.GetElementId(element3 as IHTMLElement);
                 if (elementId == null)
                 {
                     elementId = element3.name;
                 }
                 item.AddField(elementId, element3.value);
             }
         }
         objects.Add(item);
     }
     return objects;
 }
Esempio n. 2
0
        private FormObjectCollection BuildFormsCollection()
        {
            FormObjectCollection forms = new FormObjectCollection();

            EnsureHtmlParser();
            foreach (IHTMLFormElement form in _parsedHtml.forms)
            {
                string id = GetElementId(form as IHTMLElement);
                if (null == id)
                {
                    id = form.name;
                }

                FormObject f = new FormObject(id, form.method, form.action);
                foreach (IHTMLElement element in form)
                {
                    IHTMLInputElement input = element as IHTMLInputElement;
                    if (null != input)
                    {
                        id = GetElementId(input as IHTMLElement);
                        if (null == id)
                        {
                            id = input.name;
                        }

                        f.AddField(id, input.value);
                    }

                    System.Runtime.InteropServices.Marshal.ReleaseComObject(element);
                }

                forms.Add(f);

                System.Runtime.InteropServices.Marshal.ReleaseComObject(form);
            }

            return(forms);
        }
        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);
                }
            }
        }
Esempio n. 4
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;
            }
        }
        private FormObjectCollection BuildFormsCollection()
        {
            FormObjectCollection forms = new FormObjectCollection();

            EnsureHtmlParser();
            foreach (IHTMLFormElement form in _parsedHtml.forms)
            {
                string id = GetElementId(form as IHTMLElement);
                if (null == id)
                {
                    id = form.name;
                }

                FormObject f = new FormObject(id, form.method, form.action);
                foreach (IHTMLElement element in form)
                {
                    IHTMLInputElement input = element as IHTMLInputElement;
                    if (null != input)
                    {
                        id = GetElementId(input as IHTMLElement);
                        if (null == id)
                        {
                            id = input.name;
                        }

                        f.AddField(id, input.value);
                    }

                    System.Runtime.InteropServices.Marshal.ReleaseComObject(element);
                }

                forms.Add(f);

                System.Runtime.InteropServices.Marshal.ReleaseComObject(form);
            }

            return (forms);
        }
Esempio n. 6
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;
     }
 }