예제 #1
0
        /// <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.文件成功);
        }
예제 #2
0
        /// <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);
        }