gpt4 book ai didi

c# - 在商店应用程序中通过 HTTP POST 发送字节数组

转载 作者:可可西里 更新时间:2023-11-01 03:03:31 26 4
gpt4 key购买 nike

我正在尝试通过 HTTP post 从 Windows 应用商店应用程序向服务器发送一些图像 + 一些元数据,但在尝试将数据实际包含在 post 中时卡住了。由于商店应用程序 API 的更改,无法像在 Windows 窗体应用程序或类似应用程序中那样完成此操作。

我得到了错误。

cannot convert source type byte[] to target type System.Net.Http.httpContent

现在这显然是因为它有 2 种不同的类型不能隐式转换,但这基本上是我希望能够做到的。如何将我的字节数组数据放入 httpContent 类型,以便我可以将其包含在以下调用中

httpClient.PostAsync(Uri uri,HttpContent content);

这是我的完整上传方法:

async private Task UploadPhotos(List<Photo> photoCollection, string recipient, string format)
{
PhotoDataGroupDTO photoGroupDTO = PhotoSessionMapper.Map(photoCollection);

try
{
var client = new HttpClient();
client.MaxResponseContentBufferSize = 256000;
client.DefaultRequestHeaders.Add("Upload", "Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; WOW64; Trident/6.0)");

// POST action_begin
const string actionBeginUri = "http://localhost:51139/PhotoService.axd?action=Begin";
HttpResponseMessage response = await client.GetAsync(actionBeginUri);
response.EnsureSuccessStatusCode();
string responseBodyAsText = await response.Content.ReadAsStringAsync();
string id = responseBodyAsText;
////

// POST action_upload
Uri actionUploadUri = new Uri("http://localhost:51139/PhotoService.axd?action=Upload&brand={0}&id={1}&name={2}.jpg");

var metaData = new Dictionary<string, string>()
{
{"Id", id},
{"Brand", "M3rror"}, //TODO: Denne tekst skal komme fra en konfigurationsfil.
{"Format", format},
{"Recipient", recipient}
};

string stringData = "";
foreach (string key in metaData.Keys)
{
string value;
metaData.TryGetValue(key, out value);
stringData += key + "=" + value + ",";
}

UTF8Encoding encoding = new UTF8Encoding();
byte[] byteData = encoding.GetBytes(stringData);

HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, actionUploadUri);

// send meta data
// TODO get byte data in as content
HttpContent metaDataContent = byteData;
HttpResponseMessage actionUploadResponse = await client.PostAsync(actionUploadUri, metaDataContent);

actionUploadResponse.EnsureSuccessStatusCode();
responseBodyAsText = await actionUploadResponse.Content.ReadAsStringAsync();

// send photos
// TODO get byte data in as content
foreach (byte[] imageData in photoGroupDTO.PhotosData)
{
HttpContent imageContent = imageData;
actionUploadResponse = await client.PostAsync(actionUploadUri, imageContent);
actionUploadResponse.EnsureSuccessStatusCode();
responseBodyAsText = await actionUploadResponse.Content.ReadAsStringAsync();
}
////

// POST action_complete
const string actionCompleteUri = "http://localhost:51139/PhotoService.axd?action=Complete";
HttpResponseMessage actionCompleteResponse = await client.GetAsync(actionCompleteUri);
actionCompleteResponse.EnsureSuccessStatusCode();
responseBodyAsText = await actionCompleteResponse.Content.ReadAsStringAsync();
////
}

catch (HttpRequestException e)
{
}
catch (Exception e)
{
Debug.WriteLine(e.ToString());
}
}

最佳答案

使用 System.Net.Http.ByteArrayContent 会更直接。例如:

// Converting byte[] into System.Net.Http.HttpContent.
byte[] data = new byte[] { 1, 2, 3, 4, 5};
ByteArrayContent byteContent = new ByteArrayContent(data);
HttpResponseMessage reponse = await client.PostAsync(uri, byteContent);

对于仅具有特定文本编码的文本,使用:

// Convert string into System.Net.Http.HttpContent using UTF-8 encoding.
StringContent stringContent = new StringContent(
"blah blah",
System.Text.Encoding.UTF8);
HttpResponseMessage reponse = await client.PostAsync(uri, stringContent);

或者如您上面提到的,对于使用 multipart/form-data 的文本和图像:

// Send binary data and string data in a single request.
MultipartFormDataContent multipartContent = new MultipartFormDataContent();
multipartContent.Add(byteContent);
multipartContent.Add(stringContent);
HttpResponseMessage reponse = await client.PostAsync(uri, multipartContent);

关于c# - 在商店应用程序中通过 HTTP POST 发送字节数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23518817/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com