[C#] 纯文本查看 复制代码
using System;
using System.Collections.Generic;
using DotNet.Utilities;
using System.Text;
using System.Threading;
using System.Text.RegularExpressions;
namespace yunso
{
class baiduYun
{
/// <summary>
/// 查找s1和s2之间的字符串
/// </summary>
/// <param name="s"></param>
/// <param name="s1"></param>
/// <param name="s2"></param>
/// <returns>string</returns>
public string Search_string(string s, string s1, string s2)
{
int n1, n2;
n1 = s.IndexOf(s1, 0) + s1.Length; //开始位置 .
n2 = s.IndexOf(s2, n1); //结束位置
if (n2 - n1 > 0)
return s.Substring(n1, n2 - n1); //取搜索的条数,用结束的位置-开始的位置,并返回
else
return "";
}
//将字节大小转换成B K M G形式的字符串
public string sizeToB(ulong si)
{
string ss = "";
if (si < 1024)
ss = si.ToString() + "B";
else if (si / 1024 < 1024)
{
decimal r = (decimal)si / 1024; // (decimal)x/ y 表示把 x 转换成decimal再做除法运算,int 除 int 是会丢失小数点的。
ss = Math.Round(r, 2, MidpointRounding.AwayFromZero).ToString() + "K";//45.37
}
else if (si / 1024 / 1024 < 1024)
{
decimal r = (decimal)si / (1024 * 1024);
int len = 2;
if (r >= 10) len = 1;
ss = Math.Round(r, len, MidpointRounding.AwayFromZero).ToString() + "M";//45.37
}
else
{
decimal r = (decimal)si / (1024 * 1024 * 1024);
int len = 2;
if (r >= 10) len = 1;
ss = Math.Round(r, len, MidpointRounding.AwayFromZero).ToString() + "G";//45.37
}
return ss;
}
/// <summary>
/// 时间戳转为C#格式时间 13位
/// </summary>
/// <param name=”timeStamp”></param>
/// <returns></returns>
private DateTime ConvertStringToDateTime(string timeStamp)
{
DateTime dtStart = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));
long lTime = long.Parse(timeStamp + "0000");
TimeSpan toNow = new TimeSpan(lTime);
return dtStart.Add(toNow);
}
public class BDRes
{
public bool success;//获取直链是否成功
public string reason;//失败原因
public string name;//文件名
public string size;//大小
public string dlink;//直链地址
}
public BDRes jiexi(string url)
{
//string bdcookie = "BAIDUID=46D45EBC40584E57D3DAAE3CB227749A:FG=1";
BDRes bdr = new BDRes();
bdr.success = false;
string murl = url;
HttpHelper http = new HttpHelper();
HttpItem item = new HttpItem()
{
URL = murl,//URL 必需项
Cookie = "BAIDUID=897A635A1750FD37B5D90FAC1DC9A76F:FG=1",//字符串Cookie 可选项
//UserAgent = "Mozilla/5.0 (Linux; U; Android 4.0.2; en-us; Galaxy Nexus Build/ICL53F) AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0 Mobile Safari/534.30",//用户的浏览器类型,版本,操作系统 可选项有默认值
ContentType = "text/html",//返回类型 可选项有默认值
ResultType = ResultType.String,
Timeout=5000
};
HttpResult result = http.GetHtml(item);
string html = result.Html;
//string cookie = result.Cookie;
string cookie = "BAIDUID=897A635A1750FD37B5D90FAC1DC9A76F:FG=1";
if (html.Contains("网盘-链接不存在"))
{
bdr.reason = "此链接分享内容可能因为涉及侵权、色情、反动、低俗等信息,无法访问!";
}
else if (html.Contains("fs_id\":"))
{
//string jsons = Search_string(html, "list=JSON.parse(", ";</script>");
string isdir = Search_string(html, "\"isdir\":", ",");
if (isdir == "1")
{
bdr.reason = "目前暂不支持对文件夹的解析!";
return bdr;
}
string fs_id = Search_string(html, "fs_id\":", ",");
string uk = Search_string(html, "yunData.SHARE_UK = \"", "\"");
string shareid = Search_string(html, "\"shareid\":", ",");
string sign = Search_string(html, "sign\":\"", "\"");
string timestamp = Search_string(html, "\"timestamp\":", ",");
string durl = "http://pan.baidu.com/api/sharedownload?sign=" + sign + "×tamp=" + timestamp + "&bdstoken=&channel=chunlei&clienttype=0&web=1&app_id=250528";
string postdata = "encrypt=0&fid_list=" + System.Web.HttpUtility.UrlEncode("[" + fs_id + "]") + "&primaryid=" + shareid + "&product=share&uk=" + uk;
item = new HttpItem()
{
URL = durl,
Method = "post",
Postdata = postdata,
Referer = murl,//一定要有Referer
ContentType = "application/x-www-form-urlencoded; charset=UTF-8",
Accept = "*/*",
Cookie = cookie,
Timeout=5000
};
item.Header.Add("X-Requested-With", "XMLHttpRequest");
string jsons = http.GetHtml(item).Html;//error=-20说明该链接需要验证码了
if (jsons.Contains("{\"errno\":0"))//成功的
{
bdr.success = true;
string name = Regex.Unescape(Search_string(jsons, "server_filename\":\"", "\""));
string size = Search_string(jsons, "\"size\":", ",");
size = sizeToB(Convert.ToUInt64(size));
string dlink = Search_string(jsons, "\"dlink\":\"", "\"").Replace("\\/", "/");
item.URL = dlink;
//item.Allowautoredirect = true;
item.Referer = url;
item.Cookie = "pcsett=1446202103-546d49b7c4a9feba01341b8eddcc7379";
item.Timeout = 3000;//3秒
HttpResult res = http.GetHtml(item);
string hh = res.Html;
if (hh.Contains("{\"error_code\":302"))
{
dlink = res.Header["Location"];
}
bdr.name = name;
bdr.size = size;
bdr.dlink = dlink;
}
else
{
bdr.reason = jsons;
}
}
else
{
bdr.reason = html;
}
return bdr;
}
}
}