WindowsFormsApp输入url获取html
时间: 2024-04-29 22:25:14 浏览: 79
sqlclr函数,根据url获取html
代码怎么写?
很高兴回答你的问题!可以使用 C# 写一个 WindowsFormsApp,通过 HttpWebRequest 和 HttpWebResponse 获取网页的 HTML 代码,具体代码如下:
```
private void btnGetHtml_Click(object sender, EventArgs e)
{
string url = txtUrl.Text;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "GET";
request.Timeout = 20000;
request.ContentType = "text/html;charset=UTF-8";
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream stream = response.GetResponseStream();
StreamReader reader = new StreamReader(stream, Encoding.UTF8);
string html = reader.ReadToEnd();
txtHtml.Text = html;
}
```
这段代码会根据输入的 URL 发送一个 GET 请求,获取网页的 HTML 代码,并在界面上显示出来。希望这可以帮助你!
阅读全文