public HttpRequestBuilder WithBody(FormDataContent body) { if (!this.result.Headers.ContainsKey("Content-Type")) { this.result.Headers.Add("Content-Type", body.GetMediaType()); } this.result.BodyBytes = body.Get(); return(this); }
/// <summary> /// 添加文件内容到已有的Content /// 要求content-type为multipart/form-data /// </summary> /// <param name="stream">文件流</param> /// <param name="name">名称</param> /// <param name="fileName">文件名</param> /// <param name="contentType">文件Mime</param> /// <exception cref="NotSupportedException"></exception> public override void AddFormDataFile(Stream stream, string name, string?fileName, string?contentType) { this.EnsureMediaTypeEqual(FormDataContent.MediaType); if (!(this.Content is MultipartContent httpContent)) { httpContent = new FormDataContent(); } var fileContent = new FormDataFileContent(stream, name, fileName, contentType); httpContent.Add(fileContent); this.Content = httpContent; }
/// <summary> /// 添加文本内容到已有的Content /// 要求content-type为multipart/form-data /// </summary> /// <param name="keyValues">键值对</param> /// <exception cref="NotSupportedException"></exception> /// <exception cref="ArgumentNullException"></exception> public override void AddFormDataText(IEnumerable <KeyValue> keyValues) { this.EnsureMediaTypeEqual(FormDataContent.MediaType); if (!(this.Content is MultipartContent httpContent)) { httpContent = new FormDataContent(); } foreach (var keyValue in keyValues) { var textContent = new FormDataTextContent(keyValue); httpContent.Add(textContent); this.Content = httpContent; } }
public async Task <List <DriveAuto> > SearchDriveAutoByCognitiveAsync(FormDataContent formDataContent, CancellationToken cancellationToken = default(CancellationToken)) => await Task.Run(async() => { List <DriveAuto> driveAutos = null; string url = BaseSingleton <GlobalSetting> .Instance.RestEndpoints.CarInfoEndPoints.SearchByCognitiveEndPoint; string accesToken = BaseSingleton <GlobalSetting> .Instance.UserProfile.AccesToken; try { driveAutos = await _requestProvider.PostComplexFormDataAsync <List <DriveAuto>, FormDataContent>(url, formDataContent, accesToken); } catch (ConnectivityException exc) { throw exc; } catch (Exception ex) { Debug.WriteLine($"ERROR: {ex.Message}"); throw new Exception(ex.Message); } return(driveAutos); }, cancellationToken);
private async void AnalysePhotoAsync() { Guid busyKey = Guid.NewGuid(); SetBusy(busyKey, true); PickedImage targetImage = null; ResetCancellationTokenSource(ref _analysePhotoCancellationTokenSource); CancellationTokenSource cancellationTokenSource = _analysePhotoCancellationTokenSource; try { using (var file = await _pickMediaService.TakePhotoAsync()) { if (file != null) { List <string> results = await _visionService.AnalyzeImageForText(file); if (results != null && results.Any()) { List <string> parsedResult = results.ParseVisionResult(); targetImage = await _pickMediaService.BuildPickedImageAsync(file); if (targetImage != null) { FormDataContent formDataContent = new FormDataContent { Content = parsedResult, MediaContent = targetImage }; List <DriveAuto> driveAutoDetails = await _driveAutoService.SearchDriveAutoByCognitiveAsync(formDataContent, cancellationTokenSource.Token); if (driveAutoDetails != null) { DriveAutoDetails = driveAutoDetails.ToObservableCollection(); HasResult = true; } else { HasResult = false; ErrorMessage = string.Empty; } } } else { HasResult = false; ErrorMessage = ResourceLoader.GetString(nameof(AppStrings.TryMore)).Value; } file.Dispose(); } } } catch (Exception ex) { Debug.WriteLine($"ERROR: -{ex.Message}"); Debugger.Break(); try { HttpRequestExceptionResult httpRequestExceptionResult = JsonConvert.DeserializeObject <HttpRequestExceptionResult>(ex.Message); HasResult = false; ErrorMessage = httpRequestExceptionResult.Message; } catch (Exception exc) { Debug.WriteLine($"ERROR: -{exc.Message}"); Debugger.Break(); BackCommand.Execute(null); } } SetBusy(busyKey, false); if (targetImage == null) { BackCommand.Execute(null); } }
public HttpRequestBuilder WithBody(FormDataContent formDataContent) { this.result.BodyBytes = formDataContent.Get(); return(this); }