1
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Web;
namespace WebAPI.Controllers
{
public class HttpHelp
{
//CookieContainer是当前域的所有Cookie
public CookieContainer CookieContainer { get; set; }
//CookieCollection是该次请求相关的所有Cookie
public CookieCollection CookieCollection { get; set; }
public HttpHelp()
{
this.CookieCollection = new CookieCollection();
this.CookieContainer = new CookieContainer();
}
public static string GetHtml(string url, string Referer, Encoding encode)
{
return new HttpHelp().GetHtml(url, Referer, encode, false);
}
public static string PostHtml(string url, string Referer, string data,string cookies,string domain)
{
return new HttpHelp().PostHtml(url, Referer, data, cookies,true, domain);
}
public string GetHtml(string url, string Referer, Encoding encode, bool SaveCookie)
{
HttpWebRequest req = WebRequest.Create(url) as HttpWebRequest;
req.Method = "GET";
req.CookieContainer = this.CookieContainer;
req.Proxy = null;
if (!string.IsNullOrEmpty(Referer))
req.Referer = Referer;
using (HttpWebResponse hwr = req.GetResponse() as HttpWebResponse)
{
if (SaveCookie)
{
this.CookieCollection = hwr.Cookies;
this.CookieContainer.GetCookies(req.RequestUri);
}
using (StreamReader SR = new StreamReader(hwr.GetResponseStream(), encode))
{
return SR.ReadToEnd();
}
}
}
/// <summary>
/// //POST请求中一般要设置ContentType和UserAgent
/// </summary>
/// <param name="url"></param>
/// <param name="Referer"></param>
/// <param name="postData">Post请求需要传递的参数,格式:"UserName='张三'&age=25"/param>
/// <param name="cookie">要传递的Cookie</param>
/// <param name="SaveCookie">是否保存Cookie</param>
/// <param name="domain">域名,例如:"cpas.net.cn"</param>
/// <returns></returns>
public string PostHtml(string url, string Referer, string postData,string cookie, bool SaveCookie,string domain)
{
Encoding encode = Encoding.UTF8;
HttpWebRequest req = WebRequest.Create(url) as HttpWebRequest;
req.CookieContainer = this.CookieContainer;
req.ContentType = "application/x-www-form-urlencoded";
req.Method = "POST";
req.UserAgent = "Mozilla/5.0 (Windows NT 5.1; rv:30.0) Gecko/20100101 Firefox/30.0"; //建议使用火狐浏览器调试
req.Proxy = null;
req.ProtocolVersion = HttpVersion.Version10;
//将请求来源地址添加到请求报文中
if (!string.IsNullOrEmpty(Referer))
req.Referer = Referer;
//将请求参数添加到请求报文当中
byte[] mybyte=null;
if (!string.IsNullOrEmpty(postData) && postData.Length > 0)
{
mybyte = Encoding.Default.GetBytes(postData);
req.ContentLength = mybyte.Length;
}
//将Cookie添加到请求报文中
if (!string.IsNullOrEmpty(cookie))
{
if (cookie.IndexOf(';') > 0)
{
var arr = cookie.Split(';');
for (int i = 0; i < arr.Length; i++)
{
var ck = arr[i].Split('=');
this.CookieContainer.Add(new Cookie { Name = ck[0].Trim(), Value = HttpUtility.UrlEncode(ck[1].Trim()),Domain= domain});
}
}
else
{
var ck = cookie.Split('=');
this.CookieContainer.Add(new Cookie { Name = ck[0].Trim(), Value = HttpUtility.UrlEncode(ck[1].Trim()) });
}
}
using (Stream stream = req.GetRequestStream())
{
stream.Write(mybyte, 0, mybyte.Length);
}
using (HttpWebResponse hwr = req.GetResponse() as HttpWebResponse)
{
if (SaveCookie)
{
this.CookieCollection = hwr.Cookies;
this.CookieContainer.GetCookies(req.RequestUri);
}
using (StreamReader SR = new StreamReader(hwr.GetResponseStream(), encode))
{
return SR.ReadToEnd();
}
}
}
///
/// 上传文件
///
///上传地址
///文件路径
///原网页file控件name
///请求流中的contentType
///返回的encoding
///post参数字典
///
public static string PostFile(string url, string filepath, string paramName, string contentType, Encoding encode, Dictionary<string, string> dict)
{
HttpWebRequest hrq = WebRequest.Create(url) as HttpWebRequest;
string boundary = "---------------------------" + DateTime.Now.Ticks.ToString("x");
byte[] boundarybytes = System.Text.Encoding.Default.GetBytes("\r\n--" + boundary + "\r\n");
hrq.ContentType = "multipart/form-data; boundary=" + boundary;
hrq.Method = "POST";
using (Stream stream = hrq.GetRequestStream()) //请求流
{
//写入post参数
string formdataTemplete = "Content-Disposition: form-data; name=\"{0}\"\r\n\r\n{1}";
if (dict != null && dict.Count > 0)
{
foreach (KeyValuePair<string, string> pair in dict)
{
stream.Write(boundarybytes, 0, boundarybytes.Length);
string formitem = string.Format(formdataTemplete, pair.Key, pair.Value);
byte[] formitemBytes = Encoding.Default.GetBytes(formitem);
stream.Write(formitemBytes, 0, formitemBytes.Length);
}
}
stream.Write(boundarybytes, 0, boundarybytes.Length);
//写入头信息
string headerTemplate = "Content-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"\r\nContent-Type: {2}\r\n\r\n";
string header = string.Format(headerTemplate, paramName, Path.GetFileName(filepath), contentType);
byte[] headerBytes = Encoding.UTF8.GetBytes(header);
stream.Write(headerBytes, 0, headerBytes.Length);
//写入文件
byte[] fileBytes = File.ReadAllBytes(filepath);
stream.Write(fileBytes, 0, fileBytes.Length);
//写入尾部
byte[] footerBytes = Encoding.Default.GetBytes("\r\n--" + boundary + "--\r\n");
stream.Write(footerBytes, 0, footerBytes.Length);
using (HttpWebResponse hrp = hrq.GetResponse() as HttpWebResponse)//响应流
{
using (StreamReader SR = new StreamReader(hrp.GetResponseStream(), encode))
{
return SR.ReadToEnd();
}
}
}
}
}
}
调用帮助类
public class HomeController : Controller
{
public ActionResult Index()
{
return View();
}
public ActionResult PostData()
{
string cookies = @"yunId=e2f41d2e81bb3b6d8cabd2541758d2a6; Hm_lvt_e6fbc851f9770ec4fe06b7cd3fdd0bbf=1472720372,1472720646
,1472732049,1472739127; JSESSIONID=4E54939270E5EEB62FD046516DDA6C8E; SERVERID=a45784edf4cda39b69776f05908de799
|1472739332|1472739126; Hm_lpvt_e6fbc851f9770ec4fe06b7cd3fdd0bbf=1472739310; school=%7B%22suffix%22:null
,%22companyId%22:10434,%22delFlag%22:0,%22cusorder%22:null,%22createTime%22:1464575458000,%22updateTime
%22:1468461617000,%22creator%22:null,%22updator%22:13725,%22schoolName%22:%22%E7%94%A8%E5%8F%8B%E5%AE
%A1%E8%AE%A1%22,%22mark%22:null,%22overview%22:null,%22defaultFlag%22:1,%22schoolDesc%22:null,%22indexDomain
%22:null,%22xzqhCode%22:null,%22schoolNum%22:null,%22schoolType%22:null,%22id%22:10805,%22start%22:0
,%22limit%22:12,%22page%22:1,%22pageSize%22:12,%22firstIndex%22:0,%22totalPages%22:0,%22lastPageNo%22
:0,%22previousPageNo%22:1,%22nextPageNo%22:0,%22totalRecords%22:0%7D; schoolId=10805; schoolName=%E7
%94%A8%E5%8F%8B%E5%AE%A1%E8%AE%A1; account=18620997006";
var str = HttpHelp.PostHtml("http://cpas.net.cn/resolve/selTopic", "", "topicId=12379439&userExerciseId=22000&pager=1", cookies, "cpas.net.cn");
StringBuilder builder = new StringBuilder();
Match regex = Regex.Match(str, @"<div class=""que_answer_hd""><span>.+</span>");
builder.Append(regex.Value);
Match regex2 = Regex.Match(str, @"<div class=""que_question_number"">第\d题");
builder.Append(regex2.Value + "</div>");
Match regex3 = Regex.Match(str, @"<div class=""que_question_introduce"">(?s).*</div>");
builder.Append(regex3.Value);
//将使用正则匹配到的东西写入到timu.txt文件夹中
using (StreamWriter SW = System.IO.File.AppendText("d:/timu.txt"))
{
if (builder.Length > 0)
{
SW.Write(builder.ToString());
}
}
return View();
}
}