13 Replies - 5935 Views - Last Post: 14 March 2012 - 07:38 AM Rate Topic: -----

#1 Guest_Andrew*


Reputation:

How to automate a login into a jsp site to download a file

Posted 14 October 2010 - 12:02 PM

I've been googling my brain out and cannot find a solution to my problem. I'm attempting to automate a process that would login to a website and then download a file by a given url. The only problem is that I can't figure out how to automate the login process to the jsp site. The site I'm tyring to login to is http://www.supplierlink.carrier.com. Any help in this area would be great. I've literally tried everything.

Is This A Good Question/Topic? 0

Replies To: How to automate a login into a jsp site to download a file

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6081
  • View blog
  • Posts: 23,557
  • Joined: 23-August 08

Re: How to automate a login into a jsp site to download a file

Posted 14 October 2010 - 12:11 PM

Well, you have to know the HTTP protocol for starters. You need to look at the source of the page and figure out all the form fields that need to be provided, and the page to which the form data is posted.
Was This Post Helpful? 1
  • +
  • -

#3 Guest_Andrew*


Reputation:

Re: How to automate a login into a jsp site to download a file

Posted 14 October 2010 - 12:20 PM

I do know the form fields to fill in. Here is a sample of code I came up with to automate this, but I couldn't get to work. I'm not even sure if this is the right way to go about it but it won't set the inner text of the elements when I try to assign it values.

        private void Web()
        {
            WebBrowser wb = new WebBrowser();
            wb.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(wb_DocumentCompleted);
            wb.Navigate("http://www.supplierlink.carrier.com");
        }

        private void wb_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            string username = "username";
            string password = "password";

            WebBrowser web = sender as WebBrowser;

            HtmlElement usernameElement = web.document.GetElementById("USER");
            usernameElement.InnerText = username;

            HtmlElement passwordElement = web.document.GetElementById("Password");
            passwordElement.InnerText = password;

            web.document.GetElementById("submit").InvokeMember("click");
        }

        private void wb_DocumentCompleted2(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            WebBrowser b = sender as WebBrowser;
            string response = b.DocumentText;

            StreamWriter sw = new StreamWriter(@"C:\Users\azwicker\Desktop\text.txt");

            sw.Write(response);
            sw.Close();

            if (response.Contains("Sign out"))
            {
                MessageBox.Show("Login Successful");
            }
        }


Was This Post Helpful? 1

#4 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4539
  • View blog
  • Posts: 7,917
  • Joined: 08-June 10

Re: How to automate a login into a jsp site to download a file

Posted 14 October 2010 - 12:26 PM

Let me show you a snippet from another project I did requiring that I pass values to a website:

public static Response UploadImage(Image image)
{
    ...
    using (WebClient wc = new WebClient())
    {
        var fields = GetFields(image);
        try
        {
            var response = wc.UploadValues(someUrlPath, fields);
            ...
        }
        catch (Exception exc)
        {
            res.Exception = exc;
        }
    }
    ...
}

private static NameValueCollection GetFields(Image image)
{
    NameValueCollection res = new NameValueCollection();
    res.Add("key", someApiKey);
    res.Add("image", image.ToBase64());
    res.Add("type", "base64");
    return res;
}


This will POST all values in the NameValueCollection to the provided URL.

Now, it may be that you have to maintain a state via a cookie or something like that. That will get more complicated.

This post has been edited by insertAlias: 14 October 2010 - 12:27 PM

Was This Post Helpful? 0
  • +
  • -

#5 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6081
  • View blog
  • Posts: 23,557
  • Joined: 23-August 08

Re: How to automate a login into a jsp site to download a file

Posted 14 October 2010 - 12:28 PM

Oh, you're using a WebBrowser component. I'll bow out of this then, as I'm not all that familiar with it.

I will say that when you say "it doesn't work", that doesn't give us a lot to go on. Be specific as to what is not working.
Was This Post Helpful? 1
  • +
  • -

#6 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4539
  • View blog
  • Posts: 7,917
  • Joined: 08-June 10

Re: How to automate a login into a jsp site to download a file

Posted 14 October 2010 - 12:32 PM

I'll also say I don't know anything about using a WebBrowser. That's a UI control, not really something for automation, like a WebClient or an HttpWebRequest/Response.
Was This Post Helpful? 1
  • +
  • -

#7 zwickz711  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 8
  • Joined: 14-October 10

Re: How to automate a login into a jsp site to download a file

Posted 14 October 2010 - 12:35 PM

Well I'm not exactly sure that the way I'm going about this is correct. I'm not familiar with navigating through websites programmatically... But it's not working the way I think it would simply by not being able to set the values of the inner text of the HTML elements that i need to set. The elements inner text just stay at the value of null.

InsertAlias, thanks for your post. I'll see what I can do with that.

This post has been edited by zwickz711: 14 October 2010 - 12:37 PM

Was This Post Helpful? 0
  • +
  • -

#8 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6081
  • View blog
  • Posts: 23,557
  • Joined: 23-August 08

Re: How to automate a login into a jsp site to download a file

Posted 14 October 2010 - 12:43 PM

I don't think you want to set the innertext. I think instead you should be getting those as an HtmlInputElement and setting the value.

But that's strictly a wild-ass guess :)
Was This Post Helpful? 1
  • +
  • -

#9 zwickz711  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 8
  • Joined: 14-October 10

Re: How to automate a login into a jsp site to download a file

Posted 14 October 2010 - 01:18 PM

View PostJackOfAllTrades, on 14 October 2010 - 11:43 AM, said:

I don't think you want to set the innertext. I think instead you should be getting those as an HtmlInputElement and setting the value.

But that's strictly a wild-ass guess :)


lol. Ill try that too. Thanks for your help.
Was This Post Helpful? 0
  • +
  • -

#10 zwickz711  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 8
  • Joined: 14-October 10

Re: How to automate a login into a jsp site to download a file

Posted 18 October 2010 - 01:25 PM

So I got the login to work, with the HTMLInputElements as mentioned before. The only problem now is downloading the file automatically. When I try to navigate to the url of the file download a save/open dialog box appears asking me what I want to do. I'm wondering if theres a way to bypass that and just download it automatically by using the WebBrowser control?
Was This Post Helpful? 0
  • +
  • -

#11 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4539
  • View blog
  • Posts: 7,917
  • Joined: 08-June 10

Re: How to automate a login into a jsp site to download a file

Posted 18 October 2010 - 02:26 PM

According to some of the things I've read, you simply won't be able to do this. It's a security issue. Remember, you're basically using an instance of Internet Explorer when you're using the WebBrowser control.

Just one forum that mentions this:
http://forums.devx.c...ad.php?t=147646

Anyway, I'd suggest against using the WebBrowser and use the WebClient object. It's different, you'll have to re-figure it out, but you can download a file without any dialog box using it.

Also, you could try using HttpWebRequest. That works as well. More manual work, but more fine control.
Was This Post Helpful? 2
  • +
  • -

#12 zwickz711  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 8
  • Joined: 14-October 10

Re: How to automate a login into a jsp site to download a file

Posted 19 October 2010 - 05:20 AM

View PostinsertAlias, on 18 October 2010 - 01:26 PM, said:

According to some of the things I've read, you simply won't be able to do this. It's a security issue. Remember, you're basically using an instance of Internet Explorer when you're using the WebBrowser control.

Just one forum that mentions this:
http://forums.devx.c...ad.php?t=147646

Anyway, I'd suggest against using the WebBrowser and use the WebClient object. It's different, you'll have to re-figure it out, but you can download a file without any dialog box using it.

Also, you could try using HttpWebRequest. That works as well. More manual work, but more fine control.



That's exactly what I had found too but wanted to make sure. I've considered using the others but I'm unsure of how to manipulate the html of the page in order to login to the page to access the file download url. Any ideas there?

This post has been edited by zwickz711: 19 October 2010 - 05:21 AM

Was This Post Helpful? 0
  • +
  • -

#13 zwickz711  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 8
  • Joined: 14-October 10

Re: How to automate a login into a jsp site to download a file

Posted 21 October 2010 - 12:00 PM

I finally figured out a way to automate this process. There is a tool called watiN that will automate the process through IE. This was able to handle the open/save file dialog box. Here is the code I used through it to automate my process....

            IE ie = new IE("urltologinto");
            //ie.ShowWindow(WatiN.Core.Native.Windows.NativeMethods.WindowShowStyle.Hide);

            //html tags to find by name
            ie.TextField(Find.ByName("USER")).TypeText("username");
            ie.TextField(Find.ByName("Password")).TypeText("password");
            ie.Button(Find.ByName("Login")).Click();

           //file download handler to handler dialog box
            FileDownloadHandler handler = new FileDownloadHandler(@"FileLocationHere");
            ie.AddDialogHandler(handler);
            ie.WaitForComplete();

            ie.GoTo(@"urltodownload");

            handler.WaitUntilFileDownloadDialogIsHandled(5);
            handler.WaitUntilDownloadCompleted(20);


Was This Post Helpful? 1
  • +
  • -

#14 manramro  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 10-March 09

Re: How to automate a login into a jsp site to download a file

Posted 14 March 2012 - 07:38 AM

View Postzwickz711, on 21 October 2010 - 12:00 PM, said:

I finally figured out a way to automate this process. There is a tool called watiN that will automate the process through IE. This was able to handle the open/save file dialog box. Here is the code I used through it to automate my process....

            IE ie = new IE("urltologinto");
            //ie.ShowWindow(WatiN.Core.Native.Windows.NativeMethods.WindowShowStyle.Hide);

            //html tags to find by name
            ie.TextField(Find.ByName("USER")).TypeText("username");
            ie.TextField(Find.ByName("Password")).TypeText("password");
            ie.Button(Find.ByName("Login")).Click();

           //file download handler to handler dialog box
            FileDownloadHandler handler = new FileDownloadHandler(@"FileLocationHere");
            ie.AddDialogHandler(handler);
            ie.WaitForComplete();

            ie.GoTo(@"urltodownload");

            handler.WaitUntilFileDownloadDialogIsHandled(5);
            handler.WaitUntilDownloadCompleted(20);



Would be able to show me how did you implemet this in your code, I need to do the exact same thing in a webapplication but I need to get an instance of IE Which is already running, please help I have been trying to do this forever
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1