Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. .NET (Core and Framework)
  4. IE automation question

IE automation question

Scheduled Pinned Locked Moved .NET (Core and Framework)
helpquestioncsharpsysadmintesting
12 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S swheat

    I need to access a website and download several hundred spreadsheet files. The problem is that I must use POST to initiate the download. I cannot use webclient.downloadfile becuase I dont have a url to pass. I am able to get the download started using a bit of test code I wrote. Now I just need to handle the file download dialog that saves the file. If possible I would like to bypass the download dialog and complete the process in code. The following test code actually works (in its complete form) to get the download started: object missing = ""; InternetExplorerClass ie = new SHDocVw.InternetExplorerClass(); ie.Navigate("http://alfred.stlouisfed.org/series/downloaddata?seid=RSAFS&cid=6", ref missing, ref missing, ref missing, ref missing); System.Threading.Thread.Sleep(5000); mshtml.IHTMLDocument2 doc = (mshtml.IHTMLDocument2)ie.Document; foreach (mshtml.IHTMLElement pageElement in doc.all) { // click some elements } Is it possible to get the url that will be sent back to the server from the document AFTER I "click" the parameters? I have already seen The most complete C# Webbrowser control which is posted here on CodeProject. Either it is not designed to do what I described above or I dont understand it well enough to put it to good use. If there is some code there that illustrates what I am asking, kindly point me in the right direction. Thanks anyone for some help with this. I have searched every corner of the internet for an answer and I am truly stumped.

    R Offline
    R Offline
    Rajasekharan Vengalil
    wrote on last edited by
    #3

    Is there any particular reason why you wish to automate IE to achieve this instead of the HTTP classes from the base class library?

    -- gleat http://blogorama.nerdworks.in[^] --

    S 1 Reply Last reply
    0
    • P Pete OHanlon

      Why not use FTP? It's better suited to this type of operation. A simple example would look like this: using System; using System.Collections.Generic; using System.Text; using System.Net; using System.IO; namespace FtpLibrary { public class FtpClass { #region Members private string _ipAddress; private string _userId; private string _password; private int _bufferSize; #endregion /// /// Instantiate a new instance of /// /// The address of the remote server. /// The user Id to logon to the server. /// The password to use. public FtpClass(string ipAddress, string userId, string password) : this(ipAddress, userId, password, 4096) { } /// /// Instantiate a new instance of /// /// The address of the remote server. /// The user Id to logon to the server. /// The password to use. /// The size of the download buffer. public FtpClass(string ipAddress, string userId, string password, int bufferSize) { _ipAddress = ipAddress; _userId = userId; _password = password; if (bufferSize < 1024 || bufferSize > 8192) _bufferSize = 4096; _bufferSize = bufferSize; } /// /// Download the specified file from the remote host. This is a very /// simplistic version, where the remote file and the local file will /// have the same name, and the remote file is in the root directory. /// /// The name of the directory to store the file in. /// The name of the file. /// This is a very simplistic version, where the remote file /// and the local file will have the same name, and the remote file /// is in the root directory. /// public void Download(string filePath, string fileName) { FtpWebRequest ftpRequest; try { string path = Path.Combine(filePath, fileName); string uri = string.Format("ftp://{0}/{1}", _ipAddress, fileName); using (FileStream outputStream = new FileStream(path, FileMode.Create)) { ftpRequest = (Ft

      S Offline
      S Offline
      swheat
      wrote on last edited by
      #4

      Hi Pete I would love to use ftp it would solve so many problems. The question I asked really has nothing to do with ftp. Why is the code you posted relevant? Thanks

      P 1 Reply Last reply
      0
      • R Rajasekharan Vengalil

        Is there any particular reason why you wish to automate IE to achieve this instead of the HTTP classes from the base class library?

        -- gleat http://blogorama.nerdworks.in[^] --

        S Offline
        S Offline
        swheat
        wrote on last edited by
        #5

        Thanks for your suggestion - I will use any method that works. Simpler = better. I found some examples of using a stream object - I do not understand how to: a) convert the stream to a mshtml document b) post the document back to the server c) handle the subsequent file download.

        R 1 Reply Last reply
        0
        • S swheat

          Thanks for your suggestion - I will use any method that works. Simpler = better. I found some examples of using a stream object - I do not understand how to: a) convert the stream to a mshtml document b) post the document back to the server c) handle the subsequent file download.

          R Offline
          R Offline
          Rajasekharan Vengalil
          wrote on last edited by
          #6

          How does the server return the Excel document in response to the POST? Does it send it back as a file download, i.e., does the browser show up the file download dialog? Or does it show a link that you can click?

          -- gleat http://blogorama.nerdworks.in[^] --

          S 1 Reply Last reply
          0
          • R Rajasekharan Vengalil

            How does the server return the Excel document in response to the POST? Does it send it back as a file download, i.e., does the browser show up the file download dialog? Or does it show a link that you can click?

            -- gleat http://blogorama.nerdworks.in[^] --

            S Offline
            S Offline
            swheat
            wrote on last edited by
            #7

            The browser shows the download dialog (save file as...)

            R 1 Reply Last reply
            0
            • S swheat

              Hi Pete I would love to use ftp it would solve so many problems. The question I asked really has nothing to do with ftp. Why is the code you posted relevant? Thanks

              P Offline
              P Offline
              Pete OHanlon
              wrote on last edited by
              #8

              If you don't want to use FTP, then ignore the code; but I put it there to show you how easy it is to use FTP, and to question why you would want to do something other than use FTP to do a task which FTP was designed for. I really can't see why you would want to do anything else here.

              Deja View - the feeling that you've seen this post before.

              S 1 Reply Last reply
              0
              • P Pete OHanlon

                If you don't want to use FTP, then ignore the code; but I put it there to show you how easy it is to use FTP, and to question why you would want to do something other than use FTP to do a task which FTP was designed for. I really can't see why you would want to do anything else here.

                Deja View - the feeling that you've seen this post before.

                S Offline
                S Offline
                swheat
                wrote on last edited by
                #9

                Pete, I appreciate the fact that you have taken the time to respond to my question. If you take a brief moment to read the post you responded to you would note that I am trying to download files from the website of Federal Reserve Bank of St. Louis. While they are a great bunch of folks, I highly doubt that they would put up an ftp server for no other reason than to resolve my little issue:laugh:. If you feel the code is useful, please post a specific example of how it might be used in the context I described. I am open to resolutions of any kind. I have done a fairly extensive search and no one seems to have a solution.

                1 Reply Last reply
                0
                • S swheat

                  The browser shows the download dialog (save file as...)

                  R Offline
                  R Offline
                  Rajasekharan Vengalil
                  wrote on last edited by
                  #10

                  Here's a sample program that does what you describe. It does a POST to a web page and then gets a file in response which it saves to the local disk. Hope this helps!

                  using System;
                  using System.IO;
                  using System.Net;
                  using System.Collections.Generic;
                  using System.Text;

                  namespace PostDownload
                  {
                  class Program
                  {
                  static void Main(string[] args)
                  {
                  try
                  {
                  string url = "http://nerdworks.in/webstuff/getfui.aspx";
                  string postdata = "fn=logo.gif&url=http%3A%2F%2Fwww.google.com%2Fintl%2Fen_ALL%2Fimages%2Flogo.gif";

                              //
                              // create a web request for our page
                              //
                              WebRequest request = WebRequest.Create(url);
                              request.ContentType = "application/x-www-form-urlencoded";
                              request.Method = "POST";
                              ((HttpWebRequest)request).Referer = url;
                  
                              //
                              // write the post data
                              //
                              using (Stream stream = request.GetRequestStream())
                              {
                                  stream.Write(Encoding.UTF8.GetBytes(postdata), 0, Encoding.UTF8.GetByteCount(postdata));
                                  stream.Flush();
                              }
                  
                              //
                              // get the response
                              //
                              WebResponse response = request.GetResponse();
                  
                              using (FileStream dest = new FileStream(GetFilename(response), FileMode.Create))
                              {
                                  using (Stream src = response.GetResponseStream())
                                      CopyStream(src, dest);
                              }
                          }
                          catch (Exception ex)
                          {
                              Console.WriteLine("ERROR: {0}", ex.ToString());
                          }
                      }
                  
                      private static string GetFilename(WebResponse response)
                      {
                          //
                          // try and get a file name from Content-Disposition
                          //
                          string filename = response.Headers\["Content-Disposition"\];
                          if (filename != null)
                          {
                              string\[\] tokens = filename.Split(';');
                              if (tokens.Length == 2)
                              {
                                  tokens = tokens\[1\].Split('=');
                                  if (tokens.Length == 2)
                                      filename = tokens\[1\];
                                  else
                                      filename = null;
                              }
                              else
                                  filename = null;
                          }
                  
                  S 2 Replies Last reply
                  0
                  • R Rajasekharan Vengalil

                    Here's a sample program that does what you describe. It does a POST to a web page and then gets a file in response which it saves to the local disk. Hope this helps!

                    using System;
                    using System.IO;
                    using System.Net;
                    using System.Collections.Generic;
                    using System.Text;

                    namespace PostDownload
                    {
                    class Program
                    {
                    static void Main(string[] args)
                    {
                    try
                    {
                    string url = "http://nerdworks.in/webstuff/getfui.aspx";
                    string postdata = "fn=logo.gif&url=http%3A%2F%2Fwww.google.com%2Fintl%2Fen_ALL%2Fimages%2Flogo.gif";

                                //
                                // create a web request for our page
                                //
                                WebRequest request = WebRequest.Create(url);
                                request.ContentType = "application/x-www-form-urlencoded";
                                request.Method = "POST";
                                ((HttpWebRequest)request).Referer = url;
                    
                                //
                                // write the post data
                                //
                                using (Stream stream = request.GetRequestStream())
                                {
                                    stream.Write(Encoding.UTF8.GetBytes(postdata), 0, Encoding.UTF8.GetByteCount(postdata));
                                    stream.Flush();
                                }
                    
                                //
                                // get the response
                                //
                                WebResponse response = request.GetResponse();
                    
                                using (FileStream dest = new FileStream(GetFilename(response), FileMode.Create))
                                {
                                    using (Stream src = response.GetResponseStream())
                                        CopyStream(src, dest);
                                }
                            }
                            catch (Exception ex)
                            {
                                Console.WriteLine("ERROR: {0}", ex.ToString());
                            }
                        }
                    
                        private static string GetFilename(WebResponse response)
                        {
                            //
                            // try and get a file name from Content-Disposition
                            //
                            string filename = response.Headers\["Content-Disposition"\];
                            if (filename != null)
                            {
                                string\[\] tokens = filename.Split(';');
                                if (tokens.Length == 2)
                                {
                                    tokens = tokens\[1\].Split('=');
                                    if (tokens.Length == 2)
                                        filename = tokens\[1\];
                                    else
                                        filename = null;
                                }
                                else
                                    filename = null;
                            }
                    
                    S Offline
                    S Offline
                    swheat
                    wrote on last edited by
                    #11

                    gleat you are amazing! Thank you very much! I am going to try to make this work. I am sure I will have some questions but I am going to work with this a while first.

                    1 Reply Last reply
                    0
                    • R Rajasekharan Vengalil

                      Here's a sample program that does what you describe. It does a POST to a web page and then gets a file in response which it saves to the local disk. Hope this helps!

                      using System;
                      using System.IO;
                      using System.Net;
                      using System.Collections.Generic;
                      using System.Text;

                      namespace PostDownload
                      {
                      class Program
                      {
                      static void Main(string[] args)
                      {
                      try
                      {
                      string url = "http://nerdworks.in/webstuff/getfui.aspx";
                      string postdata = "fn=logo.gif&url=http%3A%2F%2Fwww.google.com%2Fintl%2Fen_ALL%2Fimages%2Flogo.gif";

                                  //
                                  // create a web request for our page
                                  //
                                  WebRequest request = WebRequest.Create(url);
                                  request.ContentType = "application/x-www-form-urlencoded";
                                  request.Method = "POST";
                                  ((HttpWebRequest)request).Referer = url;
                      
                                  //
                                  // write the post data
                                  //
                                  using (Stream stream = request.GetRequestStream())
                                  {
                                      stream.Write(Encoding.UTF8.GetBytes(postdata), 0, Encoding.UTF8.GetByteCount(postdata));
                                      stream.Flush();
                                  }
                      
                                  //
                                  // get the response
                                  //
                                  WebResponse response = request.GetResponse();
                      
                                  using (FileStream dest = new FileStream(GetFilename(response), FileMode.Create))
                                  {
                                      using (Stream src = response.GetResponseStream())
                                          CopyStream(src, dest);
                                  }
                              }
                              catch (Exception ex)
                              {
                                  Console.WriteLine("ERROR: {0}", ex.ToString());
                              }
                          }
                      
                          private static string GetFilename(WebResponse response)
                          {
                              //
                              // try and get a file name from Content-Disposition
                              //
                              string filename = response.Headers\["Content-Disposition"\];
                              if (filename != null)
                              {
                                  string\[\] tokens = filename.Split(';');
                                  if (tokens.Length == 2)
                                  {
                                      tokens = tokens\[1\].Split('=');
                                      if (tokens.Length == 2)
                                          filename = tokens\[1\];
                                      else
                                          filename = null;
                                  }
                                  else
                                      filename = null;
                              }
                      
                      S Offline
                      S Offline
                      swheat
                      wrote on last edited by
                      #12

                      Gleat, thanks again for your program. The only thing that is different in the situation I described is that the postdata values are not known when the program is run. You gave me the info I needed to look that up so all is good. For the benefit of any one else who is trying to do this: Download a program called Fiddler from www.fiddler2.com. Open fiddler then use your web browser to click the elements on the page you are trying to automate. Observe the data posted back to the server. The ParseDocument function you write (see code below) will need to create a string that matches the postdata. Here is the code you can run to automate your page. Create a form with a textbox and a button. Paste the code below as the form class. The ParseDocument function is specific for my purpose. I included it to demonstrate the basic foreach loop and id test.

                      public partial class Form3 : Form
                          {
                              public Form3()
                              {
                                  InitializeComponent();
                              }
                      
                              private void Form3_Load(object sender, EventArgs e)
                              {
                      
                              }
                      
                              private void button1_Click(object sender, EventArgs e)
                              {
                                  try
                                  {
                                      // Create a web request for the initital query to get post values
                                      string url = textBox1.Text;
                                      IHTMLDocument2 doc = new HTMLDocumentClass();
                                      WebRequest request1 = WebRequest.Create(url);
                                      WebResponse response1 = request1.GetResponse();
                                      StreamReader reader = new StreamReader(response1.GetResponseStream());
                                      doc.write(reader.ReadToEnd());
                                      doc.close();
                                      response1.Close();
                                      // Loop through the html document elements to get postdata values
                                      string postdata = ParseDocument(doc);
                                      
                                      //
                                      // create a web request for our page
                                      //
                                      WebRequest request = WebRequest.Create(url);
                                      request.ContentType = "application/x-www-form-urlencoded";
                                      request.Method = "POST";
                                      ((HttpWebRequest)request).Referer = url;
                      
                                      //
                                      // write the post data
                                      //
                                      
                                      using (Stream stream = request.GetRequestStream())
                                      {
                                          stream.Write(Encoding.UTF8.GetBytes(postdata), 0, Encoding.UTF8.GetByteCount(postdata));
                                          stream.Flush();
                      
                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups