using System;
using System.Collections;
using System.Diagnostics;
using System.IO;
using System.Net;
using System.Net.Security;
using System.Net.Sockets;
using System.Security.Authentication;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
/************************************************************************/
/* Author:huliang
* Email:huliang@yahoo.cn
* QQ:12658501
* 说明:转载请注明出处
/************************************************************************/
namespace EnAliKeywordSearch
{
public class HttpArgs
{
public string Url { get; set; }
public string Host { get; set; }
public int Port { get; set; }
public string Accept { get; set; }
public string Referer { get; set; }
public string Cookie { get; set; }
public string Data { get; set; }
public string UA { get; set; }
}
public static class HttpHelper
{
///
/// 提交方法
///
enum HttpMethod
{
GET,
POST
}
#region HttpWebRequest & HttpWebResponse
///
/// Get方法
///
/// 请求地址
/// Cookies存储器
/// 请求返回的Stream
public static string Get(string url)
{
HttpArgs args = ParseURL(url);
string strhtml = InternalSocketHttp(args, HttpMethod.GET);
return strhtml;
}
///
/// Post方法
///
/// 请求地址
/// Post数据
/// Cllkies存储器
/// 请求返回的流
public static string Post(string url,
byte[] bytes,
CookieContainer cookies,
Encoding encoding)
{
return null;
}
///
/// 根据Url得到host
///
/// url字符串
/// host字符串
private static HttpArgs ParseURL(string strUrl)
{
HttpArgs args = new HttpArgs();
args.Host = "";
args.Port = 80;
args.Referer = "";
args.Cookie = "";
args.Url = "";
args.Accept = "text/html";//,application/xhtml+xml,application/xml,application/json;";
args.UA = "Mozilla/5.0+(Compatible;+Baiduspider/2.0;++http://www.baidu.com/search/spider.html)";
//http://www.alibaba.com/products/Egg_Laying_Block_Machine/1.html
int iIndex = strUrl.IndexOf(@"//");
if (iIndex <= 0)
return null;
//www.alibaba.com:80/products/Egg_Laying_Block_Machine/1.html
string nohttpurl = strUrl.Substring(iIndex + 2);
string address = nohttpurl;
iIndex = nohttpurl.IndexOf(@"/");
if (iIndex > 0)
{
//www.alibaba.com:80
address = nohttpurl.Substring(0, iIndex);
args.Url = nohttpurl.Substring(iIndex);
}
iIndex = nohttpurl.IndexOf(@":");
if (iIndex > 0)
{
string[] tempargs = nohttpurl.Trim().Split(char.Parse(":"));
args.Host = tempargs[0];
args.Port = int.Parse(tempargs[1]);
}
else
{
//www.alibaba.com:80
args.Host = address;
args.Port = 80;
}
return args;
}
#endregion
#region Socket
public static string Get(HttpArgs args)
{
return InternalSocketHttp(args, HttpMethod.GET);
}
public static string Post(IPEndPoint endpoint,
HttpArgs args)
{
return InternalSocketHttp(args, HttpMethod.POST);
}
static string InternalSocketHttp(HttpArgs args, HttpMethod method)
{
using (Socket sK = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp))
{
try
{
sK.Connect(args.Host, args.Port);
if (sK.Connected)
{
byte[] buff = ParseHttpArgs(method, args);
if (sK.Send(buff) > 0)
{
string html = ParseResponse(sK, args);
return html;
}
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
return null;
}
private static string ParseResponse(Socket sK, HttpArgs args)
{
string header = ReadHeaderProcess(sK);
if (header.StartsWith("HTTP/1.1 400 Bad Request"))
{
return string.Empty;
}
if (header.StartsWith("HTTP/1.1 302"))
{
int start = header
.ToUpper().IndexOf("LOCATION");
if (start > 0)
{
string temp = header.Substring(start, header.Length - start);
string[] sArry = Regex.Split(temp, "\r\n");
args.Url = sArry[0].Remove(0, 10);
return Get(args); //注意:302协议需要重定向
}
}
else if (header.StartsWith("HTTP/1.1 200")) //继续读取内容
{
int start = header.ToUpper().IndexOf("CONTENT-LENGTH");
int content_length = 0;
if (start > 0)
{
string temp = header.Substring(start, header.Length - start);
string[] sArry = Regex.Split(temp, "\r\n");
content_length = Convert.ToInt32(sArry[0].Split(':')[1]);
if (content_length > 0)
{
byte[] bytes = new byte[content_length];
if (sK.Receive(bytes) > 0)
{
return Encoding.Default.GetString(bytes);
}
}
}
else
{
//不存在Content-Length协议头
return ParseResponse(sK);
}
}
return string.Empty;
}
///
/// 读取协议头
///
///
///
static string ReadHeaderProcess(Socket sK)
{
StringBuilder bulider = new StringBuilder();
while (true)
{
byte[] buff = new byte[1];
int read = sK.Receive(buff, SocketFlags.None);
if (read > 0)
{
bulider.Append((char)buff[0]);
}
string temp = bulider.ToString();
if (temp.Contains("\r\n\r\n") || temp.Contains("