/// <summary> /// 上传文件 /// </summary> /// <param name="filePath"></param> public FileOper UpLoadFile(string remotePath, string localPath) { try { //上传文件url string url = "http://c.pcs.baidu.com/rest/2.0/pcs/file?"; Dictionary <string, string> data = new Dictionary <string, string>(); data.Add("method", "upload"); //方法 data.Add("path", remotePath); //上传到网盘的路径 data.Add("ondup", "overwrite"); //模式,覆盖 data.Add("app_id", SkyDrive_app_id.ToString()); StringBuilder sb = new StringBuilder(); foreach (KeyValuePair <string, string> kv in data) { sb.Append(HttpUtility.UrlEncode(kv.Key)); sb.Append("="); sb.Append(HttpUtility.UrlEncode(kv.Value)); sb.Append("&"); } string uploadUrl = url + sb.ToString(); string boundary = GetBoundary(); UrolsPage page = new UrolsPage(); page.RequestCookie = requestCookie; FileInfo fileInfo = new FileInfo(localPath); if (fileInfo.Exists) { using (FileStream fileStream = fileInfo.Open(FileMode.Open, FileAccess.Read)) { string contenttype = GetContextType(fileInfo); StringBuilder sbHeader = new StringBuilder(); sbHeader.Append("--"); sbHeader.Append(boundary); sbHeader.Append("\r\n"); sbHeader.Append("Content-Disposition: form-data;filename=\"" + Path.GetFileName(localPath) + "\""); sbHeader.Append("\r\n"); sbHeader.Append("Content-Type: "); sbHeader.Append(contenttype); sbHeader.Append("\r\n"); sbHeader.Append("\r\n"); string postHeader = sbHeader.ToString(); byte[] postHeaderBytes = Encoding.UTF8.GetBytes(postHeader); string result = page.POST(uploadUrl, boundary, postHeaderBytes, fileStream); } } else { return(FileOper.载文件不存在); } return(FileOper.文件成功); } catch (Exception ex) { return(FileOper.文件失败); } return(FileOper.文件成功); }
/// <summary> /// 获取BAIDUID和Token值 /// </summary> private void GetPara() { //获取BAIDUID UrolsPage page = new UrolsPage("http://www.baidu.com", Encoding.UTF8); requestCookie = page.RequestCookie; GetAllCookies(requestCookie); //获取Token,CodeString GetTokenAndCodeString();//获取 Token, CodeString }
/// <summary> /// 登陆 /// </summary> private void Login() { GetPara();//获取登陆所需的参数 string loginUrl = "https://passport.baidu.com/v2/api/?login"; UrolsPage page = new UrolsPage(); page.Url = loginUrl; page.RequestCookie = requestCookie; Dictionary <string, string> Data = new Dictionary <string, string>(); Data.Add("staticpage", "http://zhidao.baidu.com/static/html/v3Jump_bf2a8d6e.html"); Data.Add("charset", "GBK"); Data.Add("token", Token); Data.Add("tpl", "ik"); Data.Add("apiver", "v3"); Data.Add("tt", Utility.GetCurrentTimeStamp().ToString()); Data.Add("codestring", ""); Data.Add("isPhone", "false"); Data.Add("safeflg", "0"); Data.Add("u", "http://www.baidu.com/"); Data.Add("username", userName); Data.Add("password", pwd); Data.Add("verifycode", ""); Data.Add("mem_pass", "on"); Data.Add("ppui_logintime", "22429"); Data.Add("callback", "parent.bd__pcbs__7doo5q"); StringBuilder sb = new StringBuilder(); foreach (KeyValuePair <string, string> kv in Data) { sb.Append(HttpUtility.UrlEncode(kv.Key)); sb.Append("="); sb.Append(HttpUtility.UrlEncode(kv.Value)); sb.Append("&"); } byte[] data = Encoding.UTF8.GetBytes(sb.ToString().TrimEnd('&')); Stream stream = new MemoryStream(data); page.POST(loginUrl, "", null, stream); requestCookie = page.RequestCookie; List <Cookie> listCookie = GetAllCookies(requestCookie); if (listCookie.Select(o => o.Name == "BDUSS").Count() == 0) { isLogin = false; return; } //Bduss = Convert.ToString(page.ResponseCookie["BDUSS"].Value); }
/// <summary> /// 获取文件目录 /// </summary> /// <returns></returns> public List <BaiDuFile> GetFileDir() { if (!IsLogin) { return(null); } Dictionary <string, string> data = new Dictionary <string, string>(); data.Add("app_id", SkyDrive_app_id.ToString()); string url = "http://pan.baidu.com/api/list?"; UrolsPage page = new UrolsPage(); foreach (KeyValuePair <string, string> item in data) { url += item.Key + "=" + item.Value + "&"; } page.Url = url; page.RequestCookie = requestCookie; page.GET(); string html = page.Html; Regex reg = new Regex(@",.*,"); Match list_m = reg.Match(html); reg = new Regex(@"\{.*?\}"); MatchCollection mc = reg.Matches(list_m.Value); List <BaiDuFile> fileList = new List <BaiDuFile>(); BaiDuFile fileItem; foreach (Match item in mc) { fileItem = new BaiDuFile(); if (GetJsonValue("isdir", item.Value) == "1") { fileItem.Type = 2; } else { fileItem.Type = 1; } fileItem.Name = GetJsonValue("server_filename", item.Value); fileList.Add(fileItem); } return(fileList); }
/// <summary> /// 获取Token,用于登陆用 /// </summary> private void GetTokenAndCodeString() { string url = string.Format("https://passport.baidu.com/v2/api/?getapi&tpl=ik&apiver=v3&tt={0}&class=login", Utility.GetCurrentTimeStamp()); UrolsPage page = new UrolsPage(); page.Url = url; page.RequestCookie = requestCookie; page.GET(); requestCookie = page.RequestCookie; GetAllCookies(requestCookie); ResultData result = JsonConvert.DeserializeObject <ResultData>(page.Html); if (result.ErrInfo.no == "0") { Token = result.Data.Token; //CodeString = result.Data.CodeString; } }