![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
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=(.*)(&);";
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=(.*)(&);"; 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值的页面
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
<!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&top_parameters=aWZyYW1lPTAmdHM9MTI3MzQyNDY5MjcyOCZ2aWV3X21vZGU9ZnVsbCZ2aWV3X3dpZHRoPTAmdmlzaXRvcl9pZD0xNzU5NzgyNzQmdmlzaXRvcl9uaWNrPXNhbmRib3hfY181&top_session=2827481fecfabd25be9a536f429a55701cc8a&top_sign=hUqxHxqx5IIxoTsquwXjRw==</PRE></BODY></HTML>