Click here to Skip to main content
16,004,828 members
Home / Discussions / .NET (Core and Framework)
   

.NET (Core and Framework)

 
GeneralRe: IE automation question Pin
swheat9-Nov-07 17:43
swheat9-Nov-07 17:43 
AnswerRe: IE automation question Pin
Rajasekharan Vengalil9-Nov-07 0:54
Rajasekharan Vengalil9-Nov-07 0:54 
GeneralRe: IE automation question Pin
swheat9-Nov-07 6:24
swheat9-Nov-07 6:24 
GeneralRe: IE automation question Pin
Rajasekharan Vengalil9-Nov-07 6:42
Rajasekharan Vengalil9-Nov-07 6:42 
GeneralRe: IE automation question Pin
swheat9-Nov-07 6:52
swheat9-Nov-07 6:52 
GeneralRe: IE automation question Pin
Rajasekharan Vengalil9-Nov-07 20:10
Rajasekharan Vengalil9-Nov-07 20:10 
GeneralRe: IE automation question Pin
swheat10-Nov-07 6:46
swheat10-Nov-07 6:46 
GeneralRe: IE automation question Pin
swheat11-Nov-07 5:37
swheat11-Nov-07 5:37 
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.
<code>
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();
                }

                //
                // 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;
            }

            //
            // if that didn't work; try to get it from Content-Type
            //
            if (filename == null)
            {
                filename = response.Headers["Content-Type"];
                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;
                }
            }

            //
            // if there's still no file name then make one up
            //
            if (filename == null)
                filename = "ding.dat";

            return "c:\\" +  filename;
        }

        private static void CopyStream(Stream src, Stream dest)
        {
            byte[] buffer = new byte[1024];
            int read = -1;

            while (read != 0)
            {
                read = src.Read(buffer, 0, buffer.Length);
                if (read != 0)
                    dest.Write(buffer, 0, read);
            }
        }

        public string ParseDocument(IHTMLDocument2 doc)
        {
            string postdata = "_qf__mainform=";

            foreach (mshtml.IHTMLElement pageElement in doc.all)
            {
                string id = String.IsNullOrEmpty(pageElement.id) ? " " : pageElement.id;

                if (id == "selected_vintage_dates")
                {
                    string[] x;
                    x = pageElement.innerText.Trim().Split(new Char[] {' '});
                  
                    for (int i = 0; i < x.Length; i++)
                    {
                        postdata += "&selected_vintage_dates%5B%5D=" + x[i];
                    }
                    break;
                }
           }
           postdata += "&entered_vintage_dates=&file_type=1&file_format=xls&download_data=Download+Data";
           return postdata;
        }


    }
</code>

Questiondifference between .net framework 1.1 and 2.0 Pin
Krushna Sahu7-Nov-07 20:23
Krushna Sahu7-Nov-07 20:23 
AnswerRe: difference between .net framework 1.1 and 2.0 Pin
N a v a n e e t h7-Nov-07 21:55
N a v a n e e t h7-Nov-07 21:55 
GeneralRe: difference between .net framework 1.1 and 2.0 Pin
Krushna Sahu13-Nov-07 20:19
Krushna Sahu13-Nov-07 20:19 
AnswerRe: difference between .net framework 1.1 and 2.0 Pin
#realJSOP8-Nov-07 8:33
professional#realJSOP8-Nov-07 8:33 
GeneralRe: difference between .net framework 1.1 and 2.0 Pin
Pete O'Hanlon8-Nov-07 9:13
mvePete O'Hanlon8-Nov-07 9:13 
GeneralRe: difference between .net framework 1.1 and 2.0 Pin
Luc Pattyn8-Nov-07 9:23
sitebuilderLuc Pattyn8-Nov-07 9:23 
GeneralRe: difference between .net framework 1.1 and 2.0 Pin
Pete O'Hanlon8-Nov-07 9:59
mvePete O'Hanlon8-Nov-07 9:59 
JokeRe: difference between .net framework 1.1 and 2.0 Pin
Vasudevan Deepak Kumar9-Nov-07 1:05
Vasudevan Deepak Kumar9-Nov-07 1:05 
GeneralRe: difference between .net framework 1.1 and 2.0 Pin
Luc Pattyn9-Nov-07 3:13
sitebuilderLuc Pattyn9-Nov-07 3:13 
GeneralRe: difference between .net framework 1.1 and 2.0 Pin
led mike8-Nov-07 9:57
led mike8-Nov-07 9:57 
GeneralRe: difference between .net framework 1.1 and 2.0 Pin
Pete O'Hanlon8-Nov-07 10:00
mvePete O'Hanlon8-Nov-07 10:00 
JokeRe: difference between .net framework 1.1 and 2.0 Pin
Vasudevan Deepak Kumar8-Nov-07 23:26
Vasudevan Deepak Kumar8-Nov-07 23:26 
GeneralRe: difference between .net framework 1.1 and 2.0 Pin
Paul Conrad10-Nov-07 5:19
professionalPaul Conrad10-Nov-07 5:19 
AnswerRe: difference between .net framework 1.1 and 2.0 Pin
Vasudevan Deepak Kumar9-Nov-07 1:13
Vasudevan Deepak Kumar9-Nov-07 1:13 
GeneralRe: difference between .net framework 1.1 and 2.0 Pin
Pete O'Hanlon9-Nov-07 10:39
mvePete O'Hanlon9-Nov-07 10:39 
QuestionURGENT SHAREPOINT JOB Pin
SGUI7-Nov-07 8:59
SGUI7-Nov-07 8:59 
AnswerRe: URGENT SHAREPOINT JOB Pin
Pete O'Hanlon7-Nov-07 10:22
mvePete O'Hanlon7-Nov-07 10:22 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.