不废话,直接上代码,firebug分析的.
using System; using System.Collections.Generic; using System.Text; using System.Net; using HttpLib; using Chsword; using System.Collections.Specialized; using System.Text.RegularExpressions; using System.Web; namespace WeiYunLib { public class WeiYunAPI { CookieContainer WeiYunCookies = new CookieContainer(); public WeiYunAPI() { } /// <summary> /// 进行MD5加密 /// </summary> /// <param name="s"></param> /// <returns></returns> public static string EncyptMD5(string s) { System.Security.Cryptography.MD5 md5 = System.Security.Cryptography.MD5CryptoServiceProvider.Create(); byte[] bytes = System.Text.Encoding.ASCII.GetBytes(s); byte[] bytes1 = md5.ComputeHash(bytes); System.Text.StringBuilder sb = new StringBuilder(); foreach (var item in bytes1) { sb.Append(item.ToString("x").PadLeft(2, ‘0‘)); } return sb.ToString().ToUpper(); } public string GetToken(string skey) { int hash = 5381; string md5_key = "tencentQQVIP123443safde&!%^%1282"; return EncyptMD5((hash << 5).ToString() + md5_key); } public bool GetDirectUrl(string sourceUrl, StringBuilder decodeUrl) { try { string getUrl = sourceUrl; string referer = sourceUrl; HttpHelper http = new HttpHelper(); HttpItem item = new HttpItem() { URL = getUrl,//URL 必需项 Method = "get",//URL 可选项 默认为Get ResultType = ResultType.String, CookieContainer = WeiYunCookies }; HttpResult result = http.GetHtml(item); if (result.StatusCode == HttpStatusCode.Found) { string tmpUrl = result.Header["Location"] == null ? string.Empty : result.Header["Location"].ToString(); if (tmpUrl != string.Empty) sourceUrl = tmpUrl; else return false; } string share_key = sourceUrl.Substring(sourceUrl.LastIndexOf(‘/‘) + 1); //string jsonStr = string.Format("") StringBuilder sbJson = new StringBuilder(); sbJson.AppendLine("{"); sbJson.AppendLine("\"req_header\": {"); sbJson.AppendLine("\"cmd\": \"view_share\","); sbJson.AppendLine("\"main_v\": 11,"); sbJson.AppendLine("\"proto_ver\": 10006,"); sbJson.AppendLine("\"sub_v\": 1,"); sbJson.AppendLine("\"encrypt\": 0,"); sbJson.AppendLine("\"msg_seq\": 1,"); sbJson.AppendLine("\"source\": 30111,"); sbJson.AppendLine("\"appid\": 30111,"); sbJson.AppendLine("\"client_ip\": \"127.0.0.1\","); sbJson.AppendLine("\"token\": \"" + GetToken(string.Empty) + "\""); sbJson.AppendLine("},"); sbJson.AppendLine("\"req_body\": {"); sbJson.AppendLine("\"share_key\": \"" + share_key + "\""); sbJson.AppendLine("}"); sbJson.AppendLine("}"); dynamic json = new JDynamic(sbJson.ToString()); var values = new NameValueCollection(); values["_"] = "1387991017986"; values["callback"] = "Get_Info"; values["data"] = json.ToString(); values["g_tk"] = ""; getUrl = "http://web2.cgi.weiyun.com/wy_share_v2.fcg"; referer = sourceUrl; http = new HttpHelper(); item = new HttpItem() { URL = getUrl,//URL 必需项 Method = "get",//URL 可选项 默认为Get Referer = referer, Getdata = HttpHelper.GetDataToString(values), ResultType = ResultType.String, CookieContainer = WeiYunCookies }; result = http.GetHtml(item); if (result.StatusCode == System.Net.HttpStatusCode.OK) { var match = Regex.Match(result.Html, "(?<=(" + "try{Get_Info\\(" + "))[.\\s\\S]*?(?=(" + "\\)}catch\\(e\\){};" + "))"); if (match.Captures.Count > 0) { //return HttpUtility.UrlDecode(match.Value.ToString()); dynamic rjson = new JDynamic(HttpUtility.UrlDecode(match.Value.ToString())); values = new NameValueCollection(); values["browser"] = "mozilla"; values["dir"] = ""; values["err_callback"] = "http://www.weiyun.com/web/callback/iframe_share_down_fail.html"; values["fid"] = rjson.rsp_body.file_list[0].file_id; values["os_info"] = "windows"; values["pdir"] = rjson.rsp_body.pdir_key; values["sharekey"] = rjson.rsp_body.share_key; values["skey"] = ""; values["uin"] = rjson.rsp_body.uin.ToString(); values["ver"] = "12"; values["zn"] = rjson.rsp_body.file_list[0].file_name; //设置cookies Cookie cookie = new Cookie("dlskey", rjson.rsp_body.dlskey, "/", "web.cgi.weiyun.com"); WeiYunCookies.Add(cookie); ////设置header 防止返回乱码 //WebHeaderCollection header = new WebHeaderCollection(); //header.Add("Accept-Charset", "GBK,utf-8;q=0.7,*;q=0.3"); //header.Add("Accept-Encoding", "gzip,deflate,sdch"); //header.Add("Accept-Language", "en-US,en;q=0.8,zh-CN;q=0.6,zh;q=0.4"); getUrl = "http://web.cgi.weiyun.com/share_dl.fcg"; referer = sourceUrl; http = new HttpHelper(); item = new HttpItem() { URL = getUrl,//URL 必需项 Method = "post",//URL 可选项 默认为Get PostDataType = PostDataType.String, Referer = referer, Postdata = HttpHelper.PostDataToString(values), ResultType = ResultType.String, ContentType = "application/x-www-form-urlencoded", CookieContainer = WeiYunCookies, }; result = http.GetHtml(item); { if (result.StatusCode == HttpStatusCode.Found) { string failedURL = "http://www.weiyun.com/web/callback/iframe_share_down_fail.html"; string tmpUrl = result.Header["Location"] == null ? string.Empty : result.Header["Location"].ToString(); if (tmpUrl != string.Empty) { if (tmpUrl.IndexOf(failedURL) >= 0) return false; decodeUrl.Append(EncodeFilename(tmpUrl)); return true; } } } } } return false; } catch (System.Exception ex) { return false; } } public static string EncodeFilename(string filename) { byte[] arrb = Encoding.Unicode.GetBytes(filename); List<byte> lb = new List<byte>(); foreach (byte b in arrb) { if (b != 0) lb.Add(b); } return Encoding.UTF8.GetString(lb.ToArray()); } } }
调用代码
WeiYunLib.WeiYunAPI weiyun = new WeiYunLib.WeiYunAPI(); StringBuilder str = new StringBuilder(); if (weiyun.GetDirectUrl(textBox1.Text, str)) textBox2.Text = str.ToString();