淘宝api 登录验证

 

淘宝api登录验证代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Text.RegularExpressions;
namespace TaobaoForm
{
public partial class Login : Form
{
public Login()
{
InitializeComponent();
webBrowser1.Navigate(
"http://open.taobao.com/isv/authorize.php? appkey=12053297");
webBrowser2.Visible
= false;
}
public string SessionKey;
private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
string a= e.ToString();
string htmltext= webBrowser1.DocumentText.ToString();

String Authcode
=GetAuthCode(htmltext);
if (Authcode != null)
{
webBrowser2.Navigate(
@"http://container.api.tbsandbox.com/container?authcode="+Authcode);
webBrowser2.Visible
= true;
webBrowser1.Visible
= false;
}
}
public string GetAuthCode(string html)
{
string rxString = @"<input type=\""text\"" id=\""autoInput\"" value=""(.*)\"" ";

MatchCollection mc
= Regex.Matches(html, rxString);
if (mc.Count != 0)
{
foreach (Match m in mc)
{
string a = m.Groups[1].Value.ToString();
return a;
}
}
else
{
return null;
}
return null;
}
public string GetTopSession(string html)
{
string rxString = @"top_session=(.*)(&amp);";
MatchCollection mc
= Regex.Matches(html, rxString);
if (mc.Count != 0)
{
foreach (Match m in mc)
{
string a = m.Groups[1].Value.ToString();
return a;
}
}
return null;
}

private void webBrowser2_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
string htmltext2 = webBrowser2.DocumentText.ToString();
Form1 form1
= new Form1();
SessionKey
= GetTopSession(htmltext2);
form1.SessionKey
= GetTopSession(htmltext2);
webBrowser2.Visible
= false;
SessionKeyClass.SessionKey
= SessionKey;
this.Close();
}

}
}

 

 

 

public string GetTopSession(string html)
        {
            string rxString = @"top_session=(.*)(&amp);";
            MatchCollection mc = Regex.Matches(html, rxString);
            if (mc.Count != 0)
            {
                foreach (Match m in mc)
                {
                    string a = m.Groups[1].Value.ToString();
                    return a;
                }
            }
            return null;
        }

以上代码是匹配淘宝api 中的 获取 网页中的 session值,

以下为session值的页面

 

代码
<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\">\r\n<HTML><HEAD>\r\n<META content=\"text/html; charset=gb2312\" http-equiv=Content-Type></HEAD>\r\n<BODY><PRE>top_appkey=12053297&amp;top_parameters=aWZyYW1lPTAmdHM9MTI3MzQyNDY5MjcyOCZ2aWV3X21vZGU9ZnVsbCZ2aWV3X3dpZHRoPTAmdmlzaXRvcl9pZD0xNzU5NzgyNzQmdmlzaXRvcl9uaWNrPXNhbmRib3hfY181&amp;top_session=2827481fecfabd25be9a536f429a55701cc8a&amp;top_sign=hUqxHxqx5IIxoTsquwXjRw==</PRE></BODY></HTML>

 

 

 

posted @ 2010-06-05 16:12  川川兄  阅读(1706)  评论(0编辑  收藏  举报