using System;
using System.Collections.Generic; using System.Text; using System.Net; using System.IO; using System.Threading; namespace MyGather { public class HTTP { public HTTP() { // // TODO: 在此处添加构造函数逻辑 // } public string Encode = "GB2312";//对方面编码,一般为GB2312和UTF-8两种 public string ProxyAdd = ""; public string Get(string url) { string sContent = ""; HttpStatusCode hc = new HttpStatusCode(); HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); HttpWebResponse response; try { if (ProxyAdd != "")//设置HTTP代理 { WebProxy proxyObject = new WebProxy(ProxyAdd, true); request.Proxy = proxyObject; } request.Method = "GET"; request.Referer = url; request.ContentType = "text/html; charset=" + Encode; request.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)"; request.Accept = "*/*"; //这里可以设置要发送过去的COOKIE内容 //CookieContainer cookie = new CookieContainer(); //Cookie ck = new Cookie("Test", "Test Content"); //ck.Domain = "www.baidued.cn"; //cookie.Add(ck); //request.CookieContainer = cookie; request.Timeout = 30000; response = (HttpWebResponse)request.GetResponse(); hc = response.StatusCode; if (response.ContentLength < 1)//如果对方返回的HTTP数据包没有Content-Length字段话,就要使用这种方式. { int nreadsize; byte[] nbytes = new byte[512];//接收缓冲区 Stream ns = null; FileStream fs = new FileStream(Directory.GetCurrentDirectory() + "/HTTP.txt", FileMode.Create); //接收的起始位置及接收的长度 request.AddRange(0); ns = response.GetResponseStream();//获得接收流 nreadsize = ns.Read(nbytes, 0, 512); while (nreadsize > 0) { fs.Write(nbytes, 0, nreadsize); try { nreadsize = ns.Read(nbytes, 0, 512);//会出错,还没明白原因,正在想办法 } catch { break; } } fs.Flush(); fs.Close(); fs.Dispose(); ns.Close(); ns.Dispose(); Duxer dux = new Duxer(); sContent = dux.ReadTxtFile(Directory.GetCurrentDirectory() + "/HTTP.txt", Encode); dux.DeleteFile(Directory.GetCurrentDirectory() + "/HTTP.txt"); } else { StreamReader stream = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding(Encode)); sContent = stream.ReadToEnd(); stream.Close(); } response.Close(); if (hc == HttpStatusCode.OK)//成功 { return sContent; } else { return "Error:" + hc.ToString(); } } catch (Exception ex) { return "Error:" + ",错误描述:" + ex.ToString() + ",远程地址:" + url; } } } } 使用方法 HTTP http =new HTTP(); http.Encode="UTF-8"; http.ProxyAdd="127.0.0.1:808"; String HTMLCode=http.Get(" http://www.******.com"); |
c#的http类,用于采集
最新推荐文章于 2019-08-28 10:40:00 发布