I work on a desktop application to communicate with hololens glasses via httprequest (hololens is connected via USB), especially, I want to read and write files. To do that, I use the following code :
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;
}
}
To run it I call
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");
The GetFile method works fine and gives me the file I want but the UploadFile method gives me the following response : StatusCode : 429, ReasonPhrase : 'Too Many Requests'. Same result if I only run one of its during execution.
I don't really understand why I have this error, I just send 1 request (or I'm missing something).
For information, REST API definition can be found there.
Thanks