2
Contributors
13
Replies
14
Views
5 Years
Discussion Span
Last Post by thines01
0

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

0

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.

0

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.

0

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();
         resp.Close();

         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"];
         respHttp.Close();

         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)
         {
            return;
         }

         req.CookieContainer = new CookieContainer();
         req.CookieContainer.Add(cookie);
      }
0

"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

www.secondpage.com&FORM=Formvalue&ACTION=ActionValue&user=youruserid&password=YourPassword

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.

Regards,

0

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?

0

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 
               req.Address.LocalPath,
               req.Address.Host);
         cookie.Secure = arr_strCookieParts[4].ToUpper().Equals("SECURE");
         return cookie;
      }
0

Okay,

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?

Regards,

0

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!

0

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.

Edited by thines01: clarity

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.