C# 登陆淘宝 (非webbrowser)
HttpWebRequest 代码:
/// <summary>
/// 通过POST方式发送数据
/// </summary>
/// <param name="Url">url</param>
/// <param name="postDataStr">Post数据</param>
/// <param name="cookie">Cookie容器</param>
/// <returns></returns>
publicstaticstringSendDataByPost(stringUrl,Encoding encoding,string postDataStr,refCookieContainer cookie,refstringAbsoluteUri)
{
string retString ="";
try
{
HttpWebRequest request =(HttpWebRequest)WebRequest.Create(Url);
request.Timeout=40000;
if(cookie.Count==0)
{
request.CookieContainer=newCookieContainer();
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 =newStreamWriter(myRequestStream, encoding);
myStreamWriter.Write(postDataStr);
myStreamWriter.Close();
HttpWebResponse response =(HttpWebResponse)request.GetResponse();
Stream myResponseStream = response.GetResponseStream();
StreamReader myStreamReader =newStreamReader(myResponseStream, encoding);
retString = myStreamReader.ReadToEnd();
AbsoluteUri= response.ResponseUri.AbsoluteUri;
myStreamReader.Close();
myResponseStream.Close();
}
catch(Exception ex)
{
thrownewException(ex.Message, ex);
}
return retString;
}
/// <summary>
/// 淘宝登陆
/// </summary>
/// <param name="Account"></param>
/// <param name="Password"></param>
/// <returns></returns>
privateboolLoginTaobao(stringAccount,stringPassword)
{
stringAbsoluteUri=string.Empty;
string result =WebHelper.SendDataByPost("http://login.taobao.com/member/login.jhtml",Encoding.GetEncoding("gb2312"),"TPL_username="+Utility.UrlEncode(Account)+
"&TPL_password="+Utility.UrlEncode(Password)+"&actionForStable=enable_post_user_action&action=Authenticator&event_submit_do_login=anything",ref cookieContainer,refAbsoluteUri);
if(Regex.IsMatch(result,"safe/check_ip.do"))
{
Platform.Common.StatusOutputEvent(newobject(),newPlatform.Common.StatusArgs(){Message="请在淘宝页面 [账号管理] 页面关闭 [登录保护] 功能或设置不进行二次验证地域."});
returnfalse;
}
if(result.IndexOf(aq_0)==-1&& result.IndexOf(aq_1)==-1)
{
returntrue;
}
else
{
Platform.Common.StatusOutputEvent(newobject(),newPlatform.Common.StatusArgs(){Message="["+this.Name+"] 登录失败."});
returnfalse;
}
}