|
近期帮朋友做个充值的插件,遇到登录的问题,调了几天也没解决,求飞哥看看。
请求地址: https://sx.ac.10086.cn/login
使用《苏飞开发助手》的Cookie自动提取器,网络tab页显示登录过程经过了:
https://sx.ac.10086.cn/Login
https://sx.ac.10086.cn/4login/backPage.jsp
http://service.sx.10086.cn/my/
http://service.sx.10086.cn/login/toLoginSso.action
最后访问http://service.sx.10086.cn/my/index.action页面,
四个页面跳转,其中https://sx.ac.10086.cn/Login和https://sx.ac.10086.cn/4login/backPage.jsp的HOST为:sx.ac.10086.cn,
http://service.sx.10086.cn/my/,http://service.sx.10086.cn/login/toLoginSso.action, http://service.sx.10086.cn/my/index.action的HOST为:service.sx.10086.cn
应该涉及到了跨域的问题。
[code=csharp]using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.IO;
using System.Net;
using mobile.Utils;
namespace mobile.Test
{
public partial class HttpClientTest : Form
{
public HttpClientTest()
{
InitializeComponent();
}
HttpItem item = null;
HttpHelper hh = null;
HttpResult httpResult = null;
string strAccept = "application/x-ms-application, image/jpeg, application/xaml+xml, image/gif, image/pjpeg, application/x-ms-xbap, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*";
string strUserAgent = "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; InfoPath.3; .NET4.0C; .NET4.0E; IEShow Toolbar; IEShow wangsuToolBar)";
private void HttpClientTest_Load(object sender, EventArgs e)
{
hh = new HttpHelper();
httpResult = new HttpResult();
showCode();
getLoginPage();
// this.textBoxNo.Text = "";
// this.textBoxFw.Text = "";
}
private void getLoginPage()
{
item = new HttpItem()
{
URL = @"https://sx.ac.10086.cn/login",
Accept = strAccept,
UserAgent = strUserAgent,
};
item.Header.Add("Accept-Encoding", "gzip, deflate");
item.Header.Add("Accept-Language", "zh-CN");
//进入登录页面
httpResult = hh.GetHtml(item);
}
private void showCode()
{
Random rad = new Random();//实例化随机数产生器rad;
String s = rad.Next(10000000, 99999999).ToString();
String strUrl = "https://sx.ac.10086.cn/common/image.jsp?l=" + s;
item = new HttpItem()
{
URL = strUrl,
ResultType = ResultType.Byte,
};
httpResult = hh.GetHtml(item);
PrintCookies(httpResult, item);
if (httpResult.StatusCode == System.Net.HttpStatusCode.OK)
{
this.pictureBox1.Image = byteArrayToImage(httpResult.ResultByte);
}
}
/// <summary>
/// 字节数组生成图片
/// </summary>
/// <param name="Bytes">字节数组</param>
/// <returns>图片</returns>
private Image byteArrayToImage(byte[] Bytes)
{
using (MemoryStream ms = new MemoryStream(Bytes))
{
Image outputImg = Image.FromStream(ms);
return outputImg;
}
}
private void button1_Click(object sender, EventArgs e)
{
string telNo = this.textBoxNo.Text;
string fwCode = this.textBoxFw.Text;
string code = this.textBoxYz.Text;
//------------------------------------------
string strPostDataF = "type=B&backurl=https%3A%2F%2Fsx.ac.10086.cn%2F4login%2FbackPage.jsp&errorurl=https%3A%2F%2Fsx.ac.10086.cn%2F4login%2FerrorPage.jsp&spid=8a4c9aa637e2567f0137e3c51d050001&RelayState=type%3DB%3Bbackurl%3Dhttp%3A%2F%2Fservice.sx.10086.cn%2Fmy%2F%3Bnl%3D3%3BloginFrom%3Dnull&webPassword=&mobileNum={0}&servicePassword={1}&validCode={2}";
string strPostData = String.Format(strPostDataF, telNo, fwCode, code);
//Cookie: CmProvid=sx; WT_FPC=id=241eb65d61fa852cf551373208538717:lv=1373470502215:ss=1373470455118; JSESSIONID=WGp7Rd1QmK64Jq6qzQGBsQWLWqKLp2f5y1n4dqTTDGQRTSL8CLwm!1052125353
item = new HttpItem()
{
URL = @"https://sx.ac.10086.cn/Login",
Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:22.0) Gecko/20100101 Firefox/22.0",
Postdata = strPostData,
Method = "post",
Referer=@"https://sx.ac.10086.cn/login",
Cookie = strCookie1 + httpResult.Cookie,//.Replace("path:/", ""),
//Host="sx.ac.10086.cn",
};
item.Header.Add("Accept-Language", "zh-cn,zh;q=0.8,en-us;q=0.5,en;q=0.3");
httpResult = hh.GetHtml(item);
//-----------------------------------------------------------------------
string strPostData0 = "displayPic=1&RelayState=type=B;backurl=http%3A%2F%2Fservice.sx.10086.cn%2Fmy%2F;nl=3;loginFrom=null&SAMLart=852c70d88d7749978407b4380df11d53";
//string strSession = httpResult.Cookie;
item = new HttpItem()
{
URL = @"https://sx.ac.10086.cn/4login/backPage.jsp",
Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:22.0) Gecko/20100101 Firefox/22.0",
Postdata = strPostData0,
Method = "post",
Referer = @"https://sx.ac.10086.cn/Login",
// Cookie = httpResult.Cookie,
};
item.Header.Add("Accept-Language", "zh-cn,zh;q=0.8,en-us;q=0.5,en;q=0.3");
httpResult = hh.GetHtml(item);
//-----------------------------------------------------------------------
// Console.WriteLine("Session1:" + httpResult.Cookie);
string strPostData1 = @"RelayState=type=B;backurl=http://service.sx.10086.cn/my/;nl=3;loginFrom=null&SAMLart=5aa148c79231482dbbd9930dfc0cbf25";
item = new HttpItem()
{
URL = @"http://service.sx.10086.cn/my/",
Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:22.0) Gecko/20100101 Firefox/22.0",
Postdata = strPostData1,
Method = "post",
//Cookie = httpResult.Cookie,
//Host="service.sx.10086.cn",
};
item.Header.Add("Accept-Language", "zh-cn,zh;q=0.8,en-us;q=0.5,en;q=0.3");
httpResult = hh.GetHtml(item);
//--------------------------------------------------------------------------
string strPostData2 = @"loginType=0&jumpMenu=01&phoneNo=" + telNo + "&loginPasswordType=0&returl=%252Fmy%252Findex.action";
item = new HttpItem()
{
URL = @"http://service.sx.10086.cn/login/toLoginSso.action",
Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:22.0) Gecko/20100101 Firefox/22.0",
Postdata = strPostData2,
Method = "post",
Cookie = httpResult.Cookie,
Referer = @"http://service.sx.10086.cn/my/",
//Host = "service.sx.10086.cn",
};
item.Header.Add("Accept-Language", "zh-cn,zh;q=0.8,en-us;q=0.5,en;q=0.3");
httpResult = hh.GetHtml(item);
//-----------------------------------------------------------------------------
item = new HttpItem()
{
URL = @"http://service.sx.10086.cn/my/index.action",
Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:22.0) Gecko/20100101 Firefox/22.0",
Postdata = strPostData,
Method = "get",
// Cookie = httpResult.Cookie,
Referer = @"http://service.sx.10086.cn/my/",
//Host = "service.sx.10086.cn",
};
item.Header.Add("Accept-Language", "zh-cn,zh;q=0.8,en-us;q=0.5,en;q=0.3");
httpResult = hh.GetHtml(item);
Console.WriteLine(httpResult.Html);
}
private string getHtml(HttpWebResponse httpWebResponse)
{
Stream responseStream = httpWebResponse.GetResponseStream();
StreamReader streamReader = new StreamReader(responseStream, Encoding.Default);
string html = streamReader.ReadToEnd();//注意这里是直接将所有的字节从头读到尾,也可以一行一行的控制,节省时间
streamReader.Close();
responseStream.Close();
return html;
}
}
}
[/code]
测试用户名密码,私信给版主~ 想把获取到的cookie也贴上来的,可超出了帖子的字数限制,请飞哥看看~ 最后是使用.net2.0调试~
|
|