[c#][Windows 10 | Windows 10 mobile | Windows Phone] HttpClient 实现form-data POST上传请求

来源:互联网 发布:国外播放器软件 编辑:程序博客网 时间:2024/05/23 02:05
HttpClient _httpClient = new HttpClient();CancellationTokenSource _cts = new CancellationTokenSource();

构建传输的文本:

HttpStringContent uidStringContent = new HttpStringContent(“string文本”);

构建传输的文件:

IStorageFile saveFile = await applicationFolder.CreateFileAsync("head.png", CreationCollisionOption.OpenIfExists);IRandomAccessStreamWithContentType stream1 = await saveFile.OpenReadAsync();HttpStreamContent streamContent = new HttpStreamContent(stream1);

构建HTTP内容:

HttpMultipartFormDataContent fileContent = new HttpMultipartFormDataContent();fileContent.Add(streamContent, "photo", "head.png");fileContent.Add(uidStringContent, "uid");

发送HTTP请求:

HttpResponseMessage response = await _httpClient.PostAsync(new Uri("your Uri"), fileContent).AsTask(_cts.Token);string head = Utils.ConvertUnicodeStringToChinese(await response.Content.ReadAsStringAsync().AsTask(_cts.Token));



1 0
原创粉丝点击