/// <summary> /// Post data onto the given URL and return the response. /// </summary> internal HttpWebResponse Post(Uri uri, MultipartEntity entity) { var request = Initialize(WebRequest.Create(uri)); request.ServicePoint.Expect100Continue = false; request.Method = "POST"; request.ContentLength = entity.ContentLength; request.ContentType = entity.ContentType; var postData = entity.Content; request.GetRequestStream().Write(postData, 0, postData.Length); return (HttpWebResponse)request.GetResponse(); }
/// <summary> /// Add parts to the entity. /// </summary> private void AddParts(MultipartEntity entity, string command, string barFilePath, bool includePackageId, bool includePackageName, string packageId) { entity.AddBody(new StringBody(PartEncoding, "command", command)); if (barFilePath != null) { entity.AddBody(new FileBody("file", Path.GetFileName(barFilePath), new FileInfo(barFilePath), "application/zip")); } if (includePackageId || includePackageName) { string name = null; if (packageId == null) { if (barFilePath == null) throw new ArgumentNullException("barFilePath"); var barFile = new BarFile(barFilePath); packageId = barFile.Manifest.PackageId; name = barFile.Manifest.PackageName; } if (includePackageId) { if (packageId == null) throw new ArgumentException("Missing Package-Id"); entity.AddBody(new StringBody(PartEncoding, "package_id", packageId)); } if (includePackageName || (name != null)) { if (name == null) throw new ArgumentException("Missing Package-Name"); entity.AddBody(new StringBody(PartEncoding, "package_name", name)); } } }
/// <summary> /// Old style login. /// Don't know on which devices this is actually used. /// </summary> private void DoOldLogin() { var url = new UriBuilder("https", deviceIp, DevicePort, "/cgi-bin/login.cgi").Uri; var loginEntity = new MultipartEntity(); loginEntity.AddBody(new StringBody(PartEncoding, "user", UserName)); loginEntity.AddBody(new StringBody(PartEncoding, "passwd", password)); httpClient.Post(url, loginEntity); }
/// <summary> /// Perform a command on the AppInstaller. /// </summary> private void DoCommand(string command, string barFilePath, bool includePackageId, bool includePackageName, string packageId) { var url = new UriBuilder("https", deviceIp, DevicePort, "/cgi-bin/appInstaller.cgi").Uri; var entity = new MultipartEntity(); AddParts(entity, command, barFilePath, includePackageId, includePackageName, packageId); var resp = httpClient.Post(url, entity); PrintResult(resp.GetResponseStream()); }