.net如何获取网页内容

.net中如何获取网页内容,下面给出C#版本的函数,关键代码如下:

1、引入namespace

using System.Net; using System.IO; using System.Text;

2、函数代码:

/// <summary> /// 返回URL内容,带POST数据提交 /// </summary> /// <param name="url"></param> /// <param name="data"></param> /// <param name="method">GET/POST(默认)</param> /// <param name="dataEncoding">编码方式(默认utf-8)</param> /// <param name="timeout">超时时间(以毫秒为单位)</param> /// <returns></returns> public static string HttpRequest(string url, string data, string method, string dataEncoding, int timeout) { string res = string.Empty; if (string.IsNullOrEmpty(dataEncoding)) dataEncoding = "utf-8"; Encoding encoding = Encoding.GetEncoding(dataEncoding); //请求 WebRequest webRequest = null; Stream postStream = null; //响应 WebResponse webResponse = null; StreamReader streamReader = null; try { if (method == "GET") { url = url + '?' + data; } //请求 webRequest = WebRequest.Create(url); webRequest.Method = string.IsNullOrEmpty(method) ? "POST" : method; webRequest.Timeout = timeout; if (method == "POST") { byte[] postData = encoding.GetBytes(data); webRequest.ContentLength = postData.Length; postStream = webRequest.GetRequestStream(); postStream.Write(postData, 0, postData.Length); } //响应 webResponse = webRequest.GetResponse(); streamReader = new StreamReader(webResponse.GetResponseStream(), encoding); res = streamReader.ReadToEnd(); } catch (WebException ex) { using (HttpWebResponse response = (HttpWebResponse)ex.Response) { using (Stream responseStream = response.GetResponseStream()) { res = new StreamReader(responseStream).ReadToEnd(); } } } catch (Exception) { } finally { if (postStream != null) { postStream.Close(); } if (streamReader != null) { streamReader.Close(); } if (webResponse != null) { webResponse.Close(); } } return res; }
3、调用示例:

Get:HttpRequest("http://test.com/request.aspx","user=aa&pwd=bbb","GET",null,30)


关键类:Encoding,WebRequest,Stream,WebResponse,StreamReader

vb.net可以使用HttpWebRequest和HttpWebResponse类来获取网页内容。 首先,我们需要导入System.Net命名空间。 然后,可以使用HttpWebRequest类创建一个请求对象,并设置请求的URL地址和其他相关属性。例如: ```vb Dim request As HttpWebRequest = CType(WebRequest.Create("http://www.example.com"), HttpWebRequest) request.Method = "GET" request.UserAgent = "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3" ``` 在上面的代码中,我们创建了一个GET请求对象,并设置了请求的URL和User-Agent头部信息。 接下来,可以使用HttpWebResponse类发送请求,并获取服务器返回的响应。例如: ```vb Dim response As HttpWebResponse = CType(request.GetResponse(), HttpWebResponse) ``` 通过上面的代码,我们发送Get请求,并获得服务器的响应。 最后,我们可以通过响应对象的GetResponseStream方法获取到服务器返回的内容,并读取出来。例如: ```vb Dim reader As New StreamReader(response.GetResponseStream(), Encoding.[Default]) Dim content As String = reader.ReadToEnd() ``` 通过上面的代码,我们使用StreamReader类读取响应的内容,并保存到一个字符串中。 最终,我们可以使用content变量来获取网页的内容。 以上就是使用vb.net获取网页内容的基本步骤。当然,还可以根据具体需求对请求和响应进行更多的设置和操作。例如,可以设置请求的超时时间、添加请求头部信息等。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值