本帖最后由 .zZ 于 2014-2-19 09:39 编辑
[C#] 纯文本查看 复制代码 public string cs;
//开始
public void Start()
{
string username = user.Text.Trim();
string pwd = FormsAuthentication.HashPasswordForStoringInConfigFile(pass.Text.Trim(), "MD5").ToLower();
//登录
HttpResult hrLogin = GetHtml("https://mp.weixin.qq.com/cgi-bin/login?lang=zh_CN", "username=" + username + "&pwd=" + pwd + "&imgcode=&f=json", "");
//获取cookies
cs = hrLogin.Cookie;
//获取的cookies跟URL传入下一步
HttpResult hrIndex = GetHtml("https://mp.weixin.qq.com" + GetUrl(hrLogin.Html), cs);
MessageBox.Show(hrIndex.Html);
}
//第一步POST提交
public HttpResult GetHtml(string url, string datas, string cookies)
{
HttpItem item = new HttpItem()
{
URL = url,
Encoding = Encoding.UTF8,
Method = "POST",
Postdata = datas,
UserAgent = "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; InfoPath.2; .NET CLR 2.0.50727; .NET4.0C; .NET4.0E; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)",
Referer = "mp.weixin.qq.com/",
ContentType = "application/x-www-form-urlencoded; charset=UTF-8",
Accept = "application/json, text/javascript, */*; q=0.01",//Cookie = cookies
};
HttpHelper http = new HttpHelper();
HttpResult hr = http.GetHtml(item);
return hr;
}
//第二步Get提交
public HttpResult GetHtml(string url, string cookies)
{
HttpWebRequest hw = (HttpWebRequest)WebRequest.Create(url);
hw.AllowAutoRedirect = true;
HttpItem item = new HttpItem();
item.URL = url;
item.Encoding = Encoding.UTF8;
item.Method = "GET";
item.Cookie = cookies;
HttpHelper http = new HttpHelper();
HttpResult hr = http.GetHtml(item);
return hr;
}
/// <summary>
/// 将json数据反序列化为Dictionary
/// </summary>
/// <param name="jsonData">json数据</param>
/// <returns></returns>
private Dictionary<string, object> JsonToDictionary(string jsonData)
{
//实例化JavaScriptSerializer类的新实例
JavaScriptSerializer jss = new JavaScriptSerializer();
try
{
//将指定的 JSON 字符串转换为 Dictionary<string, object> 类型的对象
return jss.Deserialize<Dictionary<string, object>>(jsonData);
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}
private string GetUrl(string html)
{
Dictionary<string, object> dic2 = JsonToDictionary(html);
//Dictionary<string, object> data2 = (Dictionary<string, object>)dic2["ErrMsg"];
return dic2["ErrMsg"].ToString();
} |