精易论坛

标题: c#从0开始写采集软件 第二弹 创建界面 访问网页 [打印本页]

作者: 赵六    时间: 2019-3-29 09:57
标题: c#从0开始写采集软件 第二弹 创建界面 访问网页
我们选择窗体应用  .NET framework 我们选择4.5或以上

工具箱里 拖动 一个button 跟一个TextBox 双击按钮 自动生成 按钮点击事件 添加如下代码
  
private void but_http_Click (object sender, EventArgs e)
{
string url = "http://www.17k.com/";
string host = "www.17k.com";
string html=  this.Get (url,host);
text_msg.Text = html;
}

private string Get (string url,string host)
{
string html = string.Empty;
HttpWebRequest http = WebRequest.CreateHttp (url);
http.Method = "GET";
http.Host = host;
http.UserAgent = "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36";
http.ProtocolVersion = HttpVersion.Version10;
HttpWebResponse response = http.GetResponse () as HttpWebResponse;
Stream responseStream = response.GetResponseStream ();
using (StreamReader reader = new StreamReader (responseStream, Encoding.GetEncoding ("UTF-8")))
{
html = reader.ReadToEnd ();
}
responseStream.Close ();
response.Close ();
return html;
}

最后结果如图


完整代码下载 MainForm.zip (461.21 KB, 下载次数: 74)

作者: xueyuhong958    时间: 2019-4-13 15:51
老师写的代码能不能写上备注啊,新手有的看不明白啊
作者: 赵六    时间: 2019-4-13 16:18
xueyuhong958 发表于 2019-4-13 15:51
老师写的代码能不能写上备注啊,新手有的看不明白啊

好的
作者: qiugl123    时间: 2019-4-30 09:56
关注更新
作者: yiwei125    时间: 2019-10-12 13:19
持续关系楼主更新  不错的教程
作者: 宇宙浩瀚    时间: 2021-8-2 18:39
0开始写采集软件
作者: 宇宙浩瀚    时间: 2021-8-2 18:39
我来看看从0开始写采集软件
作者: 兰克特里斯    时间: 2023-2-4 10:51
mark666学习了
作者: Mouth    时间: 2023-6-28 16:02
感谢分享




欢迎光临 精易论坛 (https://125.confly.eu.org/) Powered by Discuz! X3.4