c#的http类,用于采集

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");
 
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值