|
private CookieCollection cookies = new CookieCollection(); //cookie存放
private string getResponeString(string url, string method = "GET",string Referer="",string postData="",bool allowautoredirect=false,bool getHTML=false,string accept="",string contentType="" )//访问封装
{
HttpHelper http = new HttpHelper();
HttpItem item = new HttpItem()
{
URL = url,//URL 必需项
Method = method,//URL 可选项 默认为Get
Accept =string.IsNullOrEmpty(accept)? "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8":accept,// 可选项有默认值
ContentType =string.IsNullOrEmpty(contentType)?"application/x-www-form-urlencoded":contentType,//返回类型 可选项有默认值
ResultType = ResultType.String,//返回数据类型,是Byte还是String
ResultCookieType= ResultCookieType.CookieCollection,
Referer=Referer,
CookieCollection=cookies,
Allowautoredirect = allowautoredirect,
Postdata=postData
};
string value = "";
try
{
HttpResult result = http.GetHtml(item);
if (getHTML == false)
{
value= result.Header[System.Net.HttpResponseHeader.Location];
cookies.Add(result.CookieCollection);
return value;
}
value = result.Html;
cookies.Add(result.CookieCollection);
return value;
}
catch
{
return value;
}
}
private void start() //开始执行
{
string url = "https://reserve-hk.apple.com/HK/zh_HK/reserve/iPhone?partNumber=MKUG2ZP/A&channel=1&returnURL=http://www.apple.com/hk/shop/buy-iphone/iphone6s&sourceID=&iPP=N&appleCare=N&carrier=&store=R428";//从此URL开始访问
string signinUrl = "";
string postUrl = "https://signin.apple.com/appleauth/auth/signin";
string accept = "application/json, text/javascript, */*; q=0.01";
string contentType = "application/json; charset=UTF-8";
string Postdata = "{\"accountName\":\"cqqiphone@163.com\",\"password\":\"Aa137144\",\"rememberMe\":false}";
string method = "POST";
bool allowautoredirect = true;
//A.第一步访问url链接获取Location URL;
url = getResponeString(url);
//B.通过A获取的URL继续访问获取Location URL
url = getResponeString(url);
signinUrl = url;
//C.通过B获取的signinUrl继续访问 获取HTML
string html = getResponeString(signinUrl, getHTML: true);
//D.登录 通过指定的POST URL提交账号密码、需跳转、JSON数据提交格式 获取HTML
html = getResponeString(url: postUrl, getHTML: true, method: method, postData: Postdata, accept: accept, contentType: contentType, allowautoredirect: allowautoredirect);
//E.登录后进一步POST 需要从signinUrl提取相关参数组成Postdata 获取Location URL
Postdata = "rememberMe=false&oAuthToken=&" + signinUrl.Substring(signinUrl.IndexOf("appIdKey=")) + "&language=&" + getPathUrl(signinUrl) + "&rv=3";//根据浏览器抓包创建POST数据
postUrl = "https://signin.apple.com/IDMSWebAuth/signin";
url = getResponeString(postUrl, method: method, Referer: signinUrl, postData: Postdata);
//F.通过E返回的Location URL 继续访问继续获取Location URL
url = getResponeString(url, Referer: signinUrl);
//G.本次问题所在苏飞大神帮忙解决 到这一部就不能获取到详细信息了
//我试过很多办法换COOKIE为字符串,修改accept、contentType、UserAgent都不行
//allowautoredirect也都试过还有//就算能获取到HTML信息也是跳转后的登录页面的HTMLStatusDescription为跳转
//正常不应该跳转的,浏览器抓包显示登录成功这个页面获取的HTML title是APPLE 而不是登录。
//所以这里完全就卡主了,到底出了什么问题 希望大神帮忙下,是你的Httphelper让我从新提起了兴趣!谢谢
url = getResponeString(url, Referer: signinUrl);
}
private string getPathUrl(string url) //字符串处理
{
string a = url.Split('?')[1];
return a.Substring(0, a.IndexOf("&p_time"));
}
|
|