using System;
using System.Collections.Specialized;
using System.IO;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;
public class HttpHelper
{
public string HttpPost(string Url, string postDataStr, ref CookieContainer cookie)
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);
if (cookie.Count == 0)
{
request.CookieContainer = new CookieContainer();
cookie = request.CookieContainer;
}
else
{
request.CookieContainer = cookie;
}
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = postDataStr.Length;
Stream myRequestStream = request.GetRequestStream();
StreamWriter myStreamWriter = new StreamWriter(myRequestStream, Encoding.GetEncoding("gb2312"));
myStreamWriter.Write(postDataStr);
myStreamWriter.Close();
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream myResponseStream = response.GetResponseStream();
StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8"));
string retString = myStreamReader.ReadToEnd();
myStreamReader.Close();
myResponseStream.Close();
return retString;
}
public string HttpGet(string Url, ref CookieContainer cookie, string encodingString = "utf-8")
{
int counter = 0;
rept:
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);
request.Timeout = 100000;
if (cookie == null || cookie.Count == 0)
{
request.CookieContainer = new CookieContainer();
cookie = request.CookieContainer;
}
else
{
request.CookieContainer = cookie;
}
request.Method = "GET";
string retString = string.Empty;
//request.ContentType = "text/html;charset=UTF-8";
//基础连接已经关闭: 连接被意外关闭。
request.UserAgent = "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.104 Safari/537.36 Core/1.53.4620.400 QQBrowser/9.7.13014.400";
request.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8";
request.AutomaticDecompression = System.Net.DecompressionMethods.GZip | System.Net.DecompressionMethods.Deflate;////!Very Important.!!!
request.Headers["Accept-Language"] = "zh-CN,zh;q=0.9";//也非常重要
request.Method = "GET";
try
{
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream myResponseStream = response.GetResponseStream();
StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding(encodingString));
retString = myStreamReader.ReadToEnd();
myStreamReader.Close();
myResponseStream.Close();
}
catch (Exception ex)
{
if (counter <= 10)
{
counter++;
string msg = $"网络返回错误,正在重试 第{counter}次";
goto rept;
}
else
{
}
}
return retString;
}
public string HttpGet(string Url, string referUrl, ref CookieContainer cookie, string encodingString = "utf-8")
{
int counter = 0;
rept:
string result = string.Empty;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);
if (cookie == null || cookie.Count == 0)
{
// System.Windows.Forms.MessageBox.Show("先登陆再采集");
//request.CookieContainer = new CookieContainer();
//cookie = request.CookieContainer;
}
else
{
request.CookieContainer = cookie;
}
//设置不建立持久性连接连接
request.KeepAlive = false;
// request.ProtocolVersion = HttpVersion.Version10; //http的版本有2个,一个是1.0,一个是1.1 具体更具实际情况测试替换
//基础连接已经关闭: 连接被意外关闭。
request.UserAgent = "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.104 Safari/537.36 Core/1.53.4620.400 QQBrowser/9.7.13014.400";
request.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8";
request.AutomaticDecompression = System.Net.DecompressionMethods.GZip | System.Net.DecompressionMethods.Deflate;////!Very Important.!!!
request.Referer = referUrl;//防盗
request.Headers["Accept-Language"] = "zh-CN,zh;q=0.9";//也非常重要
request.Method = "GET";
//Accept-Language: zh-CN,zh;q=0.9
// request.ContentType = "text/html;charset=UTF-8";
Stream myResponseStream = null;
StreamReader myStreamReader = null;
try
{
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
myResponseStream = response.GetResponseStream();
myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding(encodingString));
result = myStreamReader.ReadToEnd();
}
catch (Exception)
{
if (counter <= 10)
{
counter++;
string msg = $"网络返回错误,正在重试 第{counter}次";
goto rept;
}
}
finally
{
if (myStreamReader != null)
myStreamReader.Dispose();
if (myResponseStream != null)
myResponseStream.Dispose();
}
return result;
}
public string HttpGet(string Url, string referUrl, string encodingString)
{
int counter = 0;
rept:
string result = string.Empty;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);
//基础连接已经关闭: 连接被意外关闭。
request.UserAgent = "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.104 Safari/537.36 Core/1.53.4620.400 QQBrowser/9.7.13014.400";
request.AutomaticDecompression = System.Net.DecompressionMethods.GZip | System.Net.DecompressionMethods.Deflate;////!Very Important.!!!
request.Referer = referUrl;//防盗
request.AllowAutoRedirect = true;
request.Method = "GET";
Stream myResponseStream = null;
StreamReader myStreamReader = null;
try
{
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
myResponseStream = response.GetResponseStream();
myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding(encodingString));
result = myStreamReader.ReadToEnd();
}
catch (Exception)
{
if (counter <= 10)
{
counter++;
string msg = $"网络返回错误,正在重试 第{counter}次";
goto rept;
}
}
finally
{
if (myStreamReader != null)
myStreamReader.Dispose();
if (myResponseStream != null)
myResponseStream.Dispose();
}
return result;
}
public string HttpUploadFile(string url, string file, string paramName, string contentType, NameValueCollection nvc)
{
string result = string.Empty;
string boundary = "---------------------------" + DateTime.Now.Ticks.ToString("x");
byte[] boundarybytes = System.Text.Encoding.ASCII.GetBytes("\r\n--" + boundary + "\r\n");
HttpWebRequest wr = (HttpWebRequest)WebRequest.Create(url);
wr.ContentType = "multipart/form-data; boundary=" + boundary;
wr.Method = "POST";
wr.KeepAlive = true;
wr.Credentials = System.Net.CredentialCache.DefaultCredentials;
Stream rs = wr.GetRequestStream();
string formdataTemplate = "Content-Disposition: form-data; name=\"{0}\"\r\n\r\n{1}";
foreach (string key in nvc.Keys)
{
rs.Write(boundarybytes, 0, boundarybytes.Length);
string formitem = string.Format(formdataTemplate, key, nvc[key]);
byte[] formitembytes = System.Text.Encoding.UTF8.GetBytes(formitem);
rs.Write(formitembytes, 0, formitembytes.Length);
}
rs.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, file, contentType);
byte[] headerbytes = System.Text.Encoding.UTF8.GetBytes(header);
rs.Write(headerbytes, 0, headerbytes.Length);
FileStream fileStream = new FileStream(file, FileMode.Open, FileAccess.Read);
byte[] buffer = new byte[4096];
int bytesRead = 0;
while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) != 0)
{
rs.Write(buffer, 0, bytesRead);
}
fileStream.Close();
byte[] trailer = System.Text.Encoding.ASCII.GetBytes("\r\n--" + boundary + "--\r\n");
rs.Write(trailer, 0, trailer.Length);
rs.Close();
WebResponse wresp = null;
try
{
wresp = wr.GetResponse();
Stream stream2 = wresp.GetResponseStream();
StreamReader reader2 = new StreamReader(stream2);
result = reader2.ReadToEnd();
}
catch (Exception ex)
{
if (wresp != null)
{
wresp.Close();
wresp = null;
}
}
finally
{
wr = null;
}
return result;
}
///
/// Http方式下载文件
///
/// http地址
/// 本地文件
///
public bool HttpDownload(string url, string referUrl, string postDataStr, ref CookieContainer cookie, out string filePath)
{
bool flag = false;
long startPosition = 0; // 上次下载的文件起始位置
FileStream fileStream = null; // 写入本地文件流对象
if (!Directory.Exists(Path.Combine(Environment.CurrentDirectory, "DownFiles")))
{
Directory.CreateDirectory(Path.Combine(Environment.CurrentDirectory, "DownFiles"));
}
try
{
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);// 打开网络连接
if (cookie.Count == 0)
{
request.CookieContainer = new CookieContainer();
cookie = request.CookieContainer;
}
else
{
request.CookieContainer = cookie;
}
// Set credentials to use for this request.
request.Credentials = CredentialCache.DefaultCredentials;
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.Referer = referUrl;
request.ContentLength = postDataStr.Length;
if (startPosition > 0)
request.AddRange((int)startPosition);// 设置Range值,定义远程文件读取位置
//向请求中写入PostStr
Stream myRequestStream = request.GetRequestStream();
StreamWriter myStreamWriter = new StreamWriter(myRequestStream, Encoding.GetEncoding("gb2312"));
myStreamWriter.Write(postDataStr);
myStreamWriter.Close();
HttpWebResponse response = (HttpWebResponse)request.GetResponse();// 向服务器请求,获得服务器的回应数据流
Stream myResponseStream = response.GetResponseStream();
var headers = response.Headers;
//创建本地文件写入流
filePath = Regex.Match(Uri.UnescapeDataString(headers["Content-Disposition"]), "filename=(.+)").Groups[1].Value;
string fileFullPath = Path.Combine(Environment.CurrentDirectory, "DownFiles", filePath);
fileStream = new FileStream(fileFullPath, FileMode.Create);
byte[] btArray = new byte[512];// 定义一个字节数据,用来向readStream读取内容和向writeStream写入内容
int contentSize = myResponseStream.Read(btArray, 0, btArray.Length);// 向远程文件读第一次
while (contentSize > 0)// 如果读取长度大于零则继续读
{
fileStream.Write(btArray, 0, contentSize);// 写入本地文件
contentSize = myResponseStream.Read(btArray, 0, btArray.Length);// 继续向远程文件读取
}
//关闭流
fileStream.Close();
myRequestStream.Close();
flag = true; //返回true下载成功
}
catch (Exception ex)
{
filePath = "";
fileStream.Close();
flag = false; //返回false下载失败
}
return flag;
}
///
/// 获得13位时间戳
///
///
public static long GetLongTime()
{
System.DateTime time = DateTime.Now;
System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1, 0, 0, 0, 0));
long t = (time.Ticks - startTime.Ticks) / 10000; //除10000调整为13位
return t;
}
///
/// 从网络上获得北京时间
///
///
public static DateTime GetNetChineseDateTime()
{
//document.write('2019年6月25日')
DateTime res = DateTime.MinValue;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://www.time.ac.cn/stime.asp");
request.Timeout = 100000;
request.Method = "GET";
string retString = string.Empty;
//request.ContentType = "text/html;charset=UTF-8";
//基础连接已经关闭: 连接被意外关闭。
request.UserAgent = "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.104 Safari/537.36 Core/1.53.4620.400 QQBrowser/9.7.13014.400";
request.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8";
request.AutomaticDecompression = System.Net.DecompressionMethods.GZip | System.Net.DecompressionMethods.Deflate;////!Very Important.!!!
request.Headers["Accept-Language"] = "zh-CN,zh;q=0.9";//也非常重要
request.Method = "GET";
try
{
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream myResponseStream = response.GetResponseStream();
StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("gb2312"));
retString = myStreamReader.ReadToEnd();
myStreamReader.Close();
myResponseStream.Close();
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
var pattern = "document.write\\(\'(\\d{4})年(\\d{1,2})月(\\d{1,2})日";
var options = RegexOptions.None;
var regex = new Regex(pattern, options, TimeSpan.FromMilliseconds(1000));
var groupNames = regex.GetGroupNames();
var match = regex.Match(retString);
int year=int.Parse( match.Groups[1].Value);
int month=int.Parse( match.Groups[2].Value);
int day =int.Parse( match.Groups[3].Value);
return new DateTime(year, month, day);
}
}