Click here to Skip to main content
15,892,298 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I'd like to write app in C# for retreiving WWW page from specified URL.
I've written simple app. It retrieves WWW page as a HTML source. But I've one big problem. I don't know how to make my simple program to pass thru authentication on ASP page.

Idea of this app is to connect to my account, and then to go to the specified place in the service and to chceck the status of specified documentation.
Posted

What is the authentication mechanism used for the web application that you are trying to connect to ?! most probably you will submit a user name and a password, this will be achieved if you do a SUBMIT with the user name and password (POST method) to that web page, but eventually you will need to know the names of the fields you are posting.

You could use an HttpClient to connect to that page and submit your data, taking in mind that you will have no UI in this case.
 
Share this answer
 
Since you haven't shown any code I'll assume you are using WebClient to access the remote site. In which case you need to use the Credentials property to supply the proper authentication credentials

http://msdn.microsoft.com/en-us/library/system.net.webclient.credentials.aspx[^]
 
Share this answer
 
I'm using WebClient to retreive page.

The page has session based authentication mechanism.
 
Share this answer
 
v2
If the authentication is session based - using redirects etc., you need to first post to the authentication form, get the authentication cookie and then post/get to the data page that you want to retrieve WITH the authentication cookie in your request.

This post would probably work:
http://couldbedone.blogspot.com/2007/08/webclient-handling-cookies.html[^]
 
Share this answer
 
Well I have partially did it. I wrote LogIn mechanism for ASP pages.
The code is below:

        private void button1_Click(object sender, EventArgs e)
        {

                textBox.Clear();

                HttpWebRequest webRequest = WebRequest.Create(loginURL.Text) as HttpWebRequest;
                StreamReader responseReader = new StreamReader(
                      webRequest.GetResponse().GetResponseStream()
                   );
                string responseData = responseReader.ReadToEnd();
                responseReader.Close();

                string viewState = ExtractViewState(responseData);
                string postData =
                      String.Format(
                         paramBox.Text,
                         viewState, userName.Text, userPasswd.Text
                      );

                CookieContainer cookies = new CookieContainer();

                webRequest = WebRequest.Create(loginURL.Text) as HttpWebRequest;
                webRequest.Method = "POST";
                webRequest.ContentType = "application/x-www-form-urlencoded";
                webRequest.CookieContainer = cookies;

                StreamWriter requestWriter = new StreamWriter(webRequest.GetRequestStream());
                requestWriter.Write(postData);
                requestWriter.Close();

                webRequest.GetResponse().Close();

                webRequest = WebRequest.Create(secretURL.Text) as HttpWebRequest;
                webRequest.CookieContainer = cookies;
                responseReader = new StreamReader(webRequest.GetResponse().GetResponseStream());

                responseData = responseReader.ReadToEnd();
                responseReader.Close();

                
                textBox.Text = responseData;
            

        }


private string ExtractViewState(string s)
{
    string viewStateNameDelimiter = "__VIEWSTATE";
    string valueDelimiter = "value=\"";
    int viewStateNamePosition = s.IndexOf(viewStateNameDelimiter);
    int viewStateValuePosition = s.IndexOf(
          valueDelimiter, viewStateNamePosition
       );
    int viewStateStartPosition = viewStateValuePosition +
                                 valueDelimiter.Length;
    int viewStateEndPosition = s.IndexOf("\"", viewStateStartPosition);
    return HttpUtility.UrlEncodeUnicode(
             s.Substring(
                viewStateStartPosition,
                viewStateEndPosition - viewStateStartPosition
             )
          );
}


The code works well. It's written based on examples from many posts & forums.

When I'm skipping __VIEWSTATE param and instead of this I'm putting login form data for PHP based webpages, I can connect to them too.

But I have another problem.
I thought that the site, I'm trying to connect and grab some data from is an ASP page. I was wrong :laugh: The page is based on JAVA servlets.
I've stucked again and I don't know how can I connect to this kind of page.

I'd like to know what have I to change in my code, to connect to the servlet based webpage?
 
Share this answer
 
v2
Oh. I've almost forgot. I've checked webpage, I'm trying to connect, with FIDDLER atnd I noticed, that cookie contains JSESSIONID string only.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900