1

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.

7
  • Why not just use wdsl.exe? msdn.microsoft.com/en-us/library/7h3ystb6.aspx Commented 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. Commented 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. Commented 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 own Commented 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 question Commented Jun 1, 2012 at 13:23

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

This is the error message i am getting The remote server returned an error: (401) Unauthorized.
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?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.