Hello everyone!

I have one simple question. I have minor programming experience in C# and would like to write a program which: would open url in Firefox, enter password and login on the open page, and on the next page click a link.

I have not idea where to start. I would appreciate any suggestions.


Recommended Answers

All 13 Replies

Do you actually have to are it happen in firefox?
Are you really just after the content on the last page?

You are correct. The link on the second page actives application tunneling on my computer (kind of VPN). So I would like to activate it via C#.

More details: application tunneling shuts itself down after some hours, I would like it to stay on for 24 hours 7 days a week. So I just need to come up with the way to relaunching it automatically.

You can do this with a WebClient.
Set the credentials of the web client to the username and password using the URL of the SECOND page. Remember the first page is just a holder of the URL for the second page.

View source and look at the FORM that hold the username and password box and also the "action=" statement. Piece together that URL in a text editor using & to separate the values http ...blh=BLAH&blee=BLEE&user=youruserid&password=YourPassword
... and make sure it works.

Once you've done that, you can do a wc.OpenRead() and keep it open as long as you need the tunnel to stay alive.

Oh... correction. Depending on the page, you might need a different approach.
If the first page issues a session cookie, you will need to
1) Open the first page and get the cookie using the original URL as a reference
2) Pass the cookie to the login page and get the stream.
That will create the VPN tunnel.

Here are some code snippets I have used for a similar purpose:

Cookie cookie = LoadPageOneGetCookie(strRefUrl);

// ...................................................
      private static Cookie LoadPageOneGetCookie(string strRefUrl)
         HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(strRefUrl);
         req.Method = WebRequestMethods.Http.Get;
         HttpWebResponse resp = (HttpWebResponse)req.GetResponse();

         return GetCookie(resp, req);

// ...........................................
using System;
using System.Net;
namespace GetCookie
   class CGetCookie
      public CGetCookie()

      public static Cookie GetCookie(string strUrl)
         // Get a cookie from the compose page
         HttpWebRequest reqHttp = (HttpWebRequest)WebRequest.Create(strUrl);
         reqHttp.Method = "GET";
         // Get the cookie
         HttpWebResponse respHttp = (HttpWebResponse)reqHttp.GetResponse();
         string strCookie = respHttp.Headers["Set-Cookie"];

         string[] arr_strCookieParts = strCookie.Split('=');
         Cookie cookie = new Cookie(            //Transfer the cookie
            arr_strCookieParts[0],              //Cookie Name
            arr_strCookieParts[1].Split(';')[0],//Cookie Value 
            reqHttp.Address.LocalPath, reqHttp.Address.Host);

         return cookie;

Otther helper functions:

private static Stream LogInAndGetStream(Cookie cookie, string strLoginUrl)
         HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(strLoginUrl);
         TransferCookie(ref req, cookie); // CRITICAL!
         //req.Referer = strRefUrl + '?'; // not necessary
         req.Method = WebRequestMethods.Http.Post;
         return req.GetResponse().GetResponseStream();

// ..................................................

      private static void TransferCookie(ref HttpWebRequest req, Cookie cookie)
         if (null == cookie)

         req.CookieContainer = new CookieContainer();

Awesome, let me try it!

"View source and look at the FORM that hold the username and password box and also the "action=" statement. Piece together that URL in a text editor using & to separate the values http ...blh=BLAH&blee=BLEE&user=youruserid&password=YourPassword
... and make sure it works."

Thank you again, I need a little help with the above statement. Assume the second page address is www.secondpage.com. Also assume that I know the value of FORM and the ACTION variables. How do I exactly parse into URL. I have something like this in mind


Does the order matter?

I realized that I am asking too many basic questions, therefore if I you refer me to some literature, I would greatly appreciate.


Thank you! Hopefully the last question.

return GetCookie(resp, req);

on line 13 takes two parameters.

Later on method GetCookie is defined as static and take only one string as parameter. Are these two methods related?

I think I gave you the wrong GetCookie.

private static Cookie GetCookie(HttpWebResponse resp, HttpWebRequest req)
         string strCookie = resp.Headers["Set-Cookie"];
         if (string.IsNullOrEmpty(strCookie))
            return null;

         String[] arr_strCookieParts = strCookie.Split("=; ".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
         Cookie cookie = new Cookie(            //Transfer the cookie
               arr_strCookieParts[0],              //Cookie Name
               arr_strCookieParts[1],//Cookie Value 
         cookie.Secure = arr_strCookieParts[4].ToUpper().Equals("SECURE");
         return cookie;


If I implement your code, I will end up with Stream. But Steam is defined as abstract class, I have no idea how to work with. There is no way to create Stream object. Any suggestions?


pass it to a new StreamReader()

pass it to a new StreamReader()

Okay, I did as you suggested. I wonder if there is a way to check that my implementation worked correctly? (I am still confused how you can operate with Stream )

Anyway, assuming that everything works as intended, I need to click a link on the second page, which will active the VPN. I can see there is java script associated with this link. Do I use HTTPHttpWebRequest again, but with what parameters?

Thank you again for all your time!

What does clicking the link on the second page do?
If it takes you to another page, just make your code load that page with another Stream/StreamReader either with the WebClient or HTTPWebRequest again.

If the link in dynamically generated, you will need to make your code search for the link, then Stream to the page based on the found link.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.