我通過的HttpRequest上的桌面應用程序與hololens眼鏡溝通工作(hololens通過USB連接),特別是,我想讀寫文件。要做到這一點,我使用下面的代碼:文件傳送到hololens(錯誤429)
class BuildDeployPortal
{
// Consts
public static readonly string API_FileQuery = @"http://{0}/api/filesystem/apps/file";
// Classes & Structs
public struct ConnectInfo
{
public ConnectInfo(string ip, string user, string password)
{
IP = ip;
User = user;
Password = password;
}
public string IP;
public string User;
public string Password;
}
public async static void GetFile(ConnectInfo connectInfo, string knownfolderid, string filename, string packagefullname, string path)
{
try
{
// Query
string query = string.Format(API_FileQuery, connectInfo.IP);
query += "?knownfolderid=" + Uri.EscapeUriString(knownfolderid);
query += "&filename=" + Uri.EscapeUriString(filename);
query += "&packagefullname=" + Uri.EscapeUriString(packagefullname);
query += "&path=" + Uri.EscapeUriString(path);
// Create http request
var httpRequest = new HttpClient();
httpRequest.DefaultRequestHeaders.Clear();
httpRequest.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Basic",
EncodeTo64(connectInfo.User + ":" + connectInfo.Password));
HttpResponseMessage resp = await httpRequest.GetAsync(query);
byte[] responseMessage = await resp.Content.ReadAsByteArrayAsync();
}
catch (Exception ex)
{
//log some problems
}
}
public async static void UploadFile(ConnectInfo connectInfo, string knownfolderid, string packagefullname, string path, string filePath)
{
try
{
// Query
string query = string.Format(API_FileQuery, connectInfo.IP);
query += "?knownfolderid=" + Uri.EscapeUriString(knownfolderid);
query += "&packagefullname=" + Uri.EscapeUriString(packagefullname);
query += "&path=" + Uri.EscapeUriString(path);
// Create http request
var httpRequest = new HttpClient();
httpRequest.DefaultRequestHeaders.Clear();
httpRequest.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Basic",
EncodeTo64(connectInfo.User + ":" + connectInfo.Password));
byte[] data = File.ReadAllBytes(filePath);
ByteArrayContent byteContent = new ByteArrayContent(data);
HttpResponseMessage resp = await httpRequest.PostAsync(query, byteContent);
var responseMessage = await resp.Content.ReadAsStringAsync();
}
catch (Exception ex)
{
//log some problems
}
}
// Helpers
private static string EncodeTo64(string toEncode)
{
byte[] toEncodeAsBytes = Encoding.ASCII.GetBytes(toEncode);
string returnValue = Convert.ToBase64String(toEncodeAsBytes);
return returnValue;
}
}
要運行它,我打電話
BuildDeployPortal.ConnectInfo co = new BuildDeployPortal.ConnectInfo("127.0.0.1:10080", "user", "pass");
//get file
BuildDeployPortal.GetFile(co, "LocalAppData", "2017-01-25-09-04-21_TestFile.csv", "app_full_package_name", "\\LocalState");
//upload file
BuildDeployPortal.UploadFile(co, "LocalAppData", "app_full_package_name", "\\LocalState",
"C:\\Users\\myuser\\Desktop\\info.json");
GetFile方法工作得很好,給我我想要的文件,但UploadFile方法給了我以下響應:狀態碼:429,ReasonPhrase:'太多請求'。如果我在執行期間只運行其中一個,結果也是一樣。
我真的不明白爲什麼我有這個錯誤,我只發送1個請求(或者我錯過了一些東西)。
有關信息,可以找到REST API的定義there。
感謝
我收到了同樣的結果。我已將此代碼/問題複製到HoloLens論壇:https://forums.hololens.com/discussion/8749/transfer-file-to-hololens-via-post-httprequest-error-429 - 但還沒有看到迴應那裏。我真的跳了這個解決。 –
opps,我發現這也是你的帖子。我有一個比這更早一點,https://forums.hololens.com/discussion/8736/trying-to-upload-1-files-returns-429-too-many-requests沒有迴應。如果您得到答案,請在此處張貼。 - 謝謝。 –
對不起,丹,我沒有答案,只是一個替代... –