[英]Fetch the data from web browser page to a wpf application
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. 我有一个wpf应用程序,因为我有一个文本框,我在其中输入url,然后在Web浏览器控件中获取该网页。例如,如果我要在wpf应用程序中的Web浏览器控件中打开任何网页,我一般想要从该Web浏览器控件中获取所有文本并将其显示在文本框中。 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. 我现在需要有关如何从Web浏览器控件中获取所有数据,然后将其放入多行文本框中的信息。
You can use the HttpWebRequest and HttpWebResponse objects from System.Net to communicate with a webserver. 您可以使用System.Net中的HttpWebRequest和HttpWebResponse对象与Web服务器进行通信。
eg 例如
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. 为了使您的用户感觉更好,您应该使Web请求异步,以便在下载数据时不要锁定UI。 You could use a BackgroundWorkerThread
to do this. 您可以使用BackgroundWorkerThread
进行此操作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.