6 Replies - 1089 Views - Last Post: 06 February 2012 - 12:49 PM Rate Topic: -----

#1 mohammedokasha  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 05-February 12

problem in my c# application

Posted 05 February 2012 - 04:11 AM

i develop a c# application that work with a site pages

for example that url = "http://www.myurl.net/info.php";

   private void Login()
        {
	         WebBrowser b = new WebBrowser();
	         b.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(b_DocumentCompleted);
	         b.Navigate(url);
            
	    }



       	private void b_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
	    {
	         WebBrowser b = sender as WebBrowser;
	         string response = b.DocumentText;
	 
	         
	         b.DocumentCompleted -= new WebBrowserDocumentCompletedEventHandler(b_DocumentCompleted);
	         b.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(b_DocumentCompleted2);
	 
	    
	      string postData = string.Format("username={0}&password={1}&submit={2}", username, password, Submit);
             
           
             
	         ASCIIEncoding enc = new ASCIIEncoding();

             b.Navigate(link.Text.ToString(), "", enc.GetBytes(postDatA), "Content-Type: application/x-www-form-urlencoded\r\n");

	    }

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

             if (response.Contains("Logout"))
             {
                 MessageBox.Show("Login Successful");
             }
             if (response.Contains("Login"))
             {
                 MessageBox.Show("Login not Successful");
             }
	    }




if the response contain that text :

<h1 class="uname" id="username">mohammed</h1>
<p id="age">23</p>

<form id="frm" action="change.php" method="post">
<input type="text" name="username"/>
<input type="text" name="age"/>
<button type="submit" value="submit" id="sbt"/>
</form>



1 - how i can get the information from that response

for example :

get user name and age (mohammed,23) and using it in my c# application


2 - how i can submit the form from my c# application

???

Is This A Good Question/Topic? 0
  • +

Replies To: problem in my c# application

#2 RexGrammer  Icon User is offline

  • Coding Dynamo
  • member icon

Reputation: 181
  • View blog
  • Posts: 777
  • Joined: 27-October 11

Re: problem in my c# application

Posted 05 February 2012 - 04:42 AM

No need to double post. I take it that this is the not-complete version of the question and that the other one is correct?
Was This Post Helpful? 0
  • +
  • -

#3 RexGrammer  Icon User is offline

  • Coding Dynamo
  • member icon

Reputation: 181
  • View blog
  • Posts: 777
  • Joined: 27-October 11

Re: problem in my c# application

Posted 05 February 2012 - 04:45 AM

I just want to clarify the question for everyone:

The OP wants to extract the data from the response of the page.


Oh and a question: What do you mean by

Quote

how i can submit the form from my c# application
? You want to send data?
Was This Post Helpful? 0
  • +
  • -

#4 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6058
  • View blog
  • Posts: 23,495
  • Joined: 23-August 08

Re: problem in my c# application

Posted 05 February 2012 - 05:27 AM

Merged duplicate topics.
Was This Post Helpful? 0
  • +
  • -

#5 Sergio Tapia  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1252
  • View blog
  • Posts: 4,168
  • Joined: 27-January 10

Re: problem in my c# application

Posted 05 February 2012 - 07:29 AM

You can use HtmlAgilityPack to parse HTML in .NET applications.

<h1 class="uname" id="username">mohammed</h1>
<p id="age">23</p>

HtmlDocument htmlDoc;
HtmlWeb web = new HtmlWeb();
try
{
    htmlDoc = web.Load(url);
}
catch (Exception e)
{
    throw;
}

var h1sWithClassOfName = htmlDoc.DocumentNode
                         .Descendants("h1")
                         .Where(x => x.GetAttributeValue("class", "").Contains("uname")
                         .ToList();

foreach(var foo in h1sWithClassOfName) {
    Console.WriteLine(foo.InnerText);
}




That will fetch all "h1" elements with class of "uname" and iterate through the resulting collection; printing out the .InnerText property.

Google for more docs if you get stuck.
Was This Post Helpful? 2
  • +
  • -

#6 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,960
  • Joined: 21-March 08

Re: problem in my c# application

Posted 06 February 2012 - 06:40 AM

As for submitting the data, take a look at this tutorial on how to programmatically send data to a web page.

http://www.dreaminco...ogrammatically/
Was This Post Helpful? 1
  • +
  • -

#7 mohammedokasha  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 05-February 12

Re: problem in my c# application

Posted 06 February 2012 - 12:49 PM

thanks for all
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1