I have a wpf application in that i have one text box in which i enter the url and i am fetching that web page in web browser control.Think general for example if i am opening any web page in web browser control in a wpf application i want fetch all the text from that web browser control and diplay it in a text box. from that text box i can export it into any file. i now need information on how to fetch all data from web browser control and then put it into a multi line text box.
-
Why not just use wdsl.exe? msdn.microsoft.com/en-us/library/7h3ystb6.aspxpaparazzo– paparazzo2012-06-01 12:06:20 +00:00Commented Jun 1, 2012 at 12:06
-
No No.ok lets make this as general if i want to fetch any other web page data how to do that. i used web browser control . to open the web page but i am not able fetch the data from that control.user1379584– user13795842012-06-01 12:26:59 +00:00Commented Jun 1, 2012 at 12:26
-
Where does "I need to fetch the data from that web service url which is shown on the browser and create a .wsdl file" fit in.paparazzo– paparazzo2012-06-01 12:56:25 +00:00Commented Jun 1, 2012 at 12:56
-
IHTMLDocument2 htmlDocument = webBrowser1.Document.DomDocument as IHTMLDocument2; IHTMLSelectionObject currentSelection= htmlDocument.selection; if (currentSelection!=null) { IHTMLTxtRange range= currentSelection.createRange() as IHTMLTxtRange; if (range != null) { MessageBox.Show(range.text); } } i found this the problem is without selecting the data is not fetched . i dont want to select any thing i just want to fetch the data on its ownuser1379584– user13795842012-06-01 13:18:04 +00:00Commented Jun 1, 2012 at 13:18
-
i actually want to fetch the data which is shown on web service url so i told that. please help me in doing that. i found a link stackoverflow.com/questions/5099792/… but it is not so help full so i am asking a questionuser1379584– user13795842012-06-01 13:23:08 +00:00Commented Jun 1, 2012 at 13:23
|
Show 2 more comments
1 Answer
You can use the HttpWebRequest and HttpWebResponse objects from System.Net to communicate with a webserver.
e.g.
string GetWebPage(string address)
{
string responseText;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(address);
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
{
using (StreamReader responseStream = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding("utf-8")))
{
responseText = responseStream.ReadToEnd();
}
}
return responseText;
}
You can then set the text of your textbox using:
myTextBox.Text = GetWebPage(address);
To make things nicer for your users, you should make the web requests asynchronous, so you don't lock up the UI while the data is downloading. You could use a BackgroundWorkerThread to do this.
2 Comments
user1379584
This is the error message i am getting The remote server returned an error: (401) Unauthorized.
roomaroo
The 401 error is because the website requires authentication - e.g. a username and password. If you visit the website in a normal browser does it prompt you to login?