[英]What is the best use of download and upload files in a windows store app?
我嘗試使用許多類來下載和上傳文件,以及添加標頭和身份驗證,但是這些類都不支持兩者!
我嘗試了backgrounddownloader,backgrounduploader,httpclient,httpwebclient,httprequestmessage類,但是它們都使我失敗了。
也許我錯過了一些東西,所以如果有人知道支持通過身份驗證和標頭下載和上傳的類,請共享您的代碼。
應該使用BackgroundTransfer API處理標頭和身份驗證。 標頭是在BackgroundDownloader和BackgroundUploader的SetRequestHeader方法中設置的。 此方法旨在多次調用,每個所需的標頭一次。
然后兩者都具有ServerCredential和ProxyCredential屬性。 這些都是Windows.Security.Credentials.PasswordCredential對象( http://msdn.microsoft.com/library/windows/apps/windows.security.credentials.passwordcredential.aspx )的實例。 通常,您將創建一個PasswordCredential,如下所示:
var cred = new Windows.Security.Credentials.PasswordCredential(resource, userName, password);
其中,在這種情況下,資源只是一個字符串,用於標識憑據所應用的資源(通常與憑據鎖定器API一起使用,此處並不重要)。
您是說標頭和信用分別工作,但不能一起工作嗎? 您如何測試它們?
解決了它,整個問題是請求方法,它需要一個帖子而不是獲取,我通過使用wireshark軟件就知道了。
var uri = new Uri("");
var downloader = new BackgroundDownloader();
downloader.SetRequestHeader("Range", "bytes=0-");
downloader.Method = "POST";
downloader.SetRequestHeader("Content-Type", "multipart/form-data; boundary=----WebKitFormBoundaryx5pLn3bHAS5Q8Ns5");
downloader.ServerCredential = new Windows.Security.Credentials.PasswordCredential()
{
UserName = signin.theusername,
Password = signin.thepassword
};
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.