如何获取WebView中页面的Title信息
1个回答
2015-06-19 · 知道合伙人数码行家
关注
展开全部
我发现没有办法从webView控件中直接提取内容,因此我写了一个HttpClient发送了一个Get请求,请求得到网页的内容,并在内容中找“<title>”和"</title>"通过这样找到之间的标题。
下面是我的代码:
public async void getContent()
{
Uri target = new Uri("ht tp:/ /msdn.micr osoft.c om/en-us/");
HttpClient httpClient = new HttpClient();
httpClient.MaxResponseContentBufferSize = 256000;
httpClient.DefaultRequestHeaders.Add("user-agent", "Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; WOW64; Trident/6.0)");
HttpResponseMessage message = await httpClient.GetAsync(target);
String str = await message.Content.ReadAsStringAsync();
int indexStart = str.IndexOf("<title>");
int indexEnd = str.IndexOf("</title>");
char[] charArray = str.ToCharArray(indexStart + 7, indexEnd - indexStart - 7);
this.text.Text = new String(charArray);
this.webview.Navigate(target);
}
下面是我的代码:
public async void getContent()
{
Uri target = new Uri("ht tp:/ /msdn.micr osoft.c om/en-us/");
HttpClient httpClient = new HttpClient();
httpClient.MaxResponseContentBufferSize = 256000;
httpClient.DefaultRequestHeaders.Add("user-agent", "Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; WOW64; Trident/6.0)");
HttpResponseMessage message = await httpClient.GetAsync(target);
String str = await message.Content.ReadAsStringAsync();
int indexStart = str.IndexOf("<title>");
int indexEnd = str.IndexOf("</title>");
char[] charArray = str.ToCharArray(indexStart + 7, indexEnd - indexStart - 7);
this.text.Text = new String(charArray);
this.webview.Navigate(target);
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询