static void Main(string[] args)
{
#region
//HttpWebRequest request = null;
//string url = “https://www.amazon.com/ap/signin?encoding=UTF8&openid.assoc_handle=usflex&openid.claimed_id=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0%2Fidentifier_select&openid.identity=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0%2Fidentifier_select&openid.mode=checkid_setup&openid.ns=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0&openid.ns.pape=http%3A%2F%2Fspecs.openid.net%2Fextensions%2Fpape%2F1.0&openid.pape.max_auth_age=0&openid.return_to=https%3A%2F%2Fwww.amazon.com%2Fgp%2Fyourstore%2Fhome%3Fie%3DUTF8%26action%3Dsign-out%26path%3D%252Fgp%252Fyourstore%252Fhome%26ref%3Dnav_youraccount_signout%26signIn%3D1%26useRedirectOnSuccess%3D1”;
//request = (HttpWebRequest)WebRequest.Create(url);
//request.Method = “POST”;
//request.Accept = “text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,/;q=0.8;”;
//request.UserAgent = “Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Mobile Safari/537.36”;
//request.ContentType = “application/json; charset=utf-8”;
//request.AllowAutoRedirect = true;
//request.KeepAlive = true;
//string postData = string.Format("txtUsr_id=账号&txtPassword=密码");
//byte[] postdatabyte = Encoding.GetEncoding("gbk").GetBytes(postData);
//request.ContentLength = postdatabyte.Length;
//using (Stream stream = request.GetRequestStream())
//{
// stream.Write(postdatabyte, 0, postdatabyte.Length);
//}
//HttpWebResponse response = (HttpWebResponse)request.GetResponse();
//string strWebData = string.Empty;
//using (StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding("gbk")))
//{
// strWebData = reader.ReadToEnd();
//}
//string test = strWebData;
//#endregion
// CookieContainer cookies = new CookieContainer();
// //登录
//string url = "https://www.amazon.com/ap/signin?_encoding=UTF8&openid.assoc_handle=usflex&openid.claimed_id=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0%2Fidentifier_select&openid.identity=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0%2Fidentifier_select&openid.mode=checkid_setup&openid.ns=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0&openid.ns.pape=http%3A%2F%2Fspecs.openid.net%2Fextensions%2Fpape%2F1.0&openid.pape.max_auth_age=0&openid.return_to=https%3A%2F%2Fwww.amazon.com%2Fgp%2Fyourstore%2Fhome%3Fie%3DUTF8%26action%3Dsign-out%26path%3D%252Fgp%252Fyourstore%252Fhome%26ref_%3Dnav_youraccount_signout%26signIn%3D1%26useRedirectOnSuccess%3D1";
//HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
//request.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8";
// request.Method = "POST";
// request.UserAgent = "Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Mobile Safari/537.36";
// request.CookieContainer = cookies;
// string postdata = "email=账号&password=密码"; //注意这里的用户名密码需要还原成你的
// byte[] postdataByte = Encoding.UTF8.GetBytes(postdata);
//request.ContentType = "application/json; charset=utf-8";
// request.ContentLength = postdataByte.Length;
// Stream st;
//st = request.GetRequestStream();
// st.Write(postdataByte, 0, postdataByte.Length);
// st.Close();
// var response = (HttpWebResponse)request.GetResponse();
//StreamReader readd = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
//string context = readd.ReadToEnd();
//response.Close();
// //在进入订单页
// url = "https://www.amazon.com/gp/css/order-history?ref_=nav_orders_first/";
// request = (HttpWebRequest)WebRequest.Create(url);
// request.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8";
// request.Method = "POST";
// request.UserAgent = "Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Mobile Safari/537.36";
// request.CookieContainer = cookies;
// request.Headers.Set("Accept-Encoding", "gzip, deflate, br");
// request.Headers.Set("Accept-Language", "zh,en-US;q=0.9,en;q=0.8,zh-CN;q=0.7");
// postdataByte = Encoding.UTF8.GetBytes(postdata);
// request.ContentType = "application/json; charset=utf-8";
// request.ContentLength = postdataByte.Length;
// st = request.GetRequestStream();
// st.Write(postdataByte, 0, postdataByte.Length);
// st.Close();
// response = (HttpWebResponse)request.GetResponse();
// if (response.StatusCode == HttpStatusCode.OK)
// {
// Stream pInputStream = response.GetResponseStream();
// using (MemoryStream ms = new MemoryStream())
// {
// byte[] pBuffer = new byte[1024 * 8];
// for (;;)
// {
// int bytesRead = pInputStream.Read(pBuffer, 0, pBuffer.Length);
// if (bytesRead == 0)
// {
// break;
// }
// ms.Write(pBuffer, 0, bytesRead);
// }
// pBuffer = ms.ToArray();
// if (pBuffer != null)
// {
// string pHeadValue = response.Headers.Get("Content-Encoding");
// if (pHeadValue != null)
// {
// pBuffer = UnGZip(pBuffer);
// }
// string pValue = Encoding.UTF8.GetString(pBuffer);
// if (pValue != null)
// {
// }
// }
// }
// }
// readd = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
// context = readd.ReadToEnd();
// response.Close();
// Console.WriteLine(context);
// Console.ReadKey();
#endregion
//测试下面的信息在F12里都有
HttpHeader header = new HttpHeader();
header.accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8";
header.contentType = "application/x-www-form-urlencoded";//这个F12里没有,这个一定要,这个是请求头的内容类型,不然参数无法传过去。
header.method = "POST";
header.userAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/36.0.1985.125 Safari/537.36"; header.maxTry = 300;
string postString = "LoginName=账号&LoginPwd=密码";//这里即为传递的参数,可以用工具抓包分析,也可以自己分析,主要是form里面每一个name都要加进来
string html = HTMLHelper.GetHtml("https://www.amazon.com/", HTMLHelper.GetCooKie("https://www.amazon.com/ap/signin?_encoding=UTF8&openid.assoc_handle=usflex&openid.claimed_id=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0%2Fidentifier_select&openid.identity=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0%2Fidentifier_select&openid.mode=checkid_setup&openid.ns=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0&openid.ns.pape=http%3A%2F%2Fspecs.openid.net%2Fextensions%2Fpape%2F1.0&openid.pape.max_auth_age=0&openid.return_to=https%3A%2F%2Fwww.amazon.com%2Fgp%2Fyourstore%2Fhome%3Fie%3DUTF8%26action%3Dsign-out%26path%3D%252Fgp%252Fyourstore%252Fhome%26ref_%3Dnav_youraccount_signout%26signIn%3D1%26useRedirectOnSuccess%3D1", postString, header), header);
Console.WriteLine(html);
Console.ReadKey();
}
public class HTMLHelper
{
/// <summary>
/// 获取CooKie
/// </summary>
/// <param name="loginUrl"></param>
/// <param name="postdata"></param>
/// <param name="header"></param>
/// <returns></returns>
public static CookieContainer GetCooKie(string loginUrl, string postdata, HttpHeader header)
{
HttpWebRequest request = null;
HttpWebResponse response = null;
try
{
CookieContainer cc = new CookieContainer();
request = (HttpWebRequest)WebRequest.Create(loginUrl);
request.Method = header.method;
request.ContentType = header.contentType;
//byte[] postdatabyte = Encoding.UTF8.GetBytes(postdata);
//request.ContentLength = postdatabyte.Length;
request.AllowAutoRedirect = false;
request.CookieContainer = cc;
WebResponse r = request.GetResponse();
request.KeepAlive = true;
//提交请求
//Stream stream;
//stream = request.GetRequestStream();
//stream.Write(postdatabyte, 0, postdatabyte.Length);
//stream.Close();
//接收响应
response = (HttpWebResponse)request.GetResponse();
response.Cookies = request.CookieContainer.GetCookies(request.RequestUri);
CookieCollection cook = response.Cookies;
//Cookie字符串格式
string strcrook = request.CookieContainer.GetCookieHeader(request.RequestUri);
return cc;
}
catch (Exception ex)
{
throw ex;
}
}
/// <summary>
/// 获取html
/// </summary>
/// <param name="getUrl"></param>
/// <param name="cookieContainer"></param>
/// <param name="header"></param>
/// <returns></returns>
public static string GetHtml(string getUrl, CookieContainer cookieContainer, HttpHeader header)
{
Thread.Sleep(1000);
HttpWebRequest httpWebRequest = null;
HttpWebResponse httpWebResponse = null;
try
{
httpWebRequest = (HttpWebRequest)HttpWebRequest.Create(getUrl);
httpWebRequest.CookieContainer = cookieContainer;
httpWebRequest.ContentType = header.contentType;
httpWebRequest.ServicePoint.ConnectionLimit = header.maxTry;
httpWebRequest.Referer = getUrl;
httpWebRequest.Accept = header.accept;
httpWebRequest.UserAgent = header.userAgent;
httpWebRequest.Method = "GET";
httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();
Stream responseStream = httpWebResponse.GetResponseStream();
StreamReader streamReader = new StreamReader(responseStream, Encoding.UTF8);
string html = streamReader.ReadToEnd();
streamReader.Close();
responseStream.Close();
httpWebRequest.Abort();
httpWebResponse.Close();
return html;
}
catch (Exception e)
{
if (httpWebRequest != null) httpWebRequest.Abort();
if (httpWebResponse != null) httpWebResponse.Close();
return string.Empty;
}
}
}
public class HttpHeader
{
public string contentType { get; set; }
public string accept { get; set; }
public string userAgent { get; set; }
public string method { get; set; }
public int maxTry { get; set; }
}