locked
WebClient and Post help RRS feed

  • Question

  • I'm trying to write something that will allow me to get data from a site that is protected by a logon form in asp.net.  I can put in the values for the username and password, then i tell the form to submit and it comes back with a refreshed / postback page with a new viewstate as if it looses the previous viewstate that was given the authentication information.  It would be better if i can just go through using a cookie, but the site developer has a problem with reading cookies... it seems to have the capabilities to do so, but it's dropping the data somewhere.  I am trying to go to the site and populate the required fields then download the html table that is created in the asp.net site.  The table is actually just a datagridviewer object, but it's coming through as a html table.  I am going on medical leave next week and i need to have this completed before i leave so that someone else can take over the program for the duration that i'm out.  So far, this is what i have been able to write, but it doesn't work of course.  It uses the WebBrowser obejct because I couldn't get anything else to work close to what i have here.  I've been lead down the track to using the WebClient class but I can't seem to figure out how to retain the viewstate when doing so.  I don't have an example of that because it got scrapped when i moved to the webbrowser object.

     

    [code]

    public partial class Form1 : Form

    {

    WebBrowser myBrowser;

    int internetCalls = 0;

    public Form1()

    {

    InitializeComponent();

    }

    private void Form1_Load(object sender, EventArgs e)

    {

    myBrowser = new WebBrowser();

    myBrowser.Parent = this;

    myBrowser.Dock = DockStyle.Fill;

    myBrowser.Navigate("<site full path>");

    myBrowser.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(myBrowser_DocumentCompleted);

    }

    void myBrowser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)

    {

    if (internetCalls > 2)

    return;

    internetCalls++;

    if (myBrowser.Url.ToString().Contains("<site login page url>"))

    {

    LoginToSite("<usre name>", "<password>");

    }

    }

    private void GetData(string begDate, string endDate)

    {

    HtmlElement dElement = myBrowser.Document.All["TextBox2"];

    dElement.InnerText = begDate;

    dElement = myBrowser.Document.All["TextBox1"];

    dElement.InnerText = endDate;

    dElement = myBrowser.Document.All["Find"];

    dElement.Focus();

    dElement.InvokeMember("post");

    }

    private string ExtractViewState(string s)

    {

    string viewStateNameDelimiter = "__VIEWSTATE";

    string valueDelimiter = "value=";

    int viewStateNamePosition = s.IndexOf(viewStateNameDelimiter);

    int viewStateValuePosition = s.IndexOf(valueDelimiter);

    int viewStateStartPosition = viewStateValuePosition + 6;

    int viewStateEndPosition = viewStateNamePosition - 6;

    return HttpUtility.UrlEncodeUnicode(

    s.Substring(

    viewStateStartPosition,

    viewStateEndPosition - viewStateStartPosition

    )

    );

    }

    private void LoginToSite(string sUserID, string sPWD)

    {

    HtmlElement curElement = myBrowser.Document.All["txtUser"];

    curElement.InnerText = sUserID;

    curElement = myBrowser.Document.All["txtPassword"];

    curElement.InnerText = sPWD;

    curElement = myBrowser.Document.All["cmdLogin"];

    curElement = myBrowser.Document.All["__VIEWSTATE"];

    System.Diagnostics.Debug.WriteLine("View State that was grabbed is \n{0}",

    ExtractViewState(curElement.OuterHtml));

    [/code]

    Wednesday, April 4, 2007 5:11 PM