C# HttpClient将带有消息的照片发布到Facebook图表
发行 我正试图上传一张照片到Facebook的API与一条消息 代码片段-上载 代码-webRequestClient 注释 如果我删除messageContent:它会上传图片 如果我使用MultipartContent:它上载图片,但忽略我的消息 现在不必为我为什么不使用异步功能而烦恼了 当它失败时,我会收到错误的请求 当我将message=helloworld附加到requestUri中时,它可以工作,但这不是我的体系结构中处理此问题的最灵活的解决方案。C# HttpClient将带有消息的照片发布到Facebook图表,c#,facebook-graph-api-v2.0,C#,Facebook Graph Api V2.0,发行 我正试图上传一张照片到Facebook的API与一条消息 代码片段-上载 代码-webRequestClient 注释 如果我删除messageContent:它会上传图片 如果我使用MultipartContent:它上载图片,但忽略我的消息 现在不必为我为什么不使用异步功能而烦恼了 当它失败时,我会收到错误的请求 当我将message=helloworld附加到requestUri中时,它可以工作,但这不是我的体系结构中处理此问题的最灵活的解决方案。 选中此选项将解决您的问题,或者您必
选中此选项将解决您的问题,或者您必须通过流发送图像,然后您不需要明确地告诉类型是image/jpeg
protected async void TakePictureAndUpload()
{
var ui = new CameraCaptureUI();
var file = await ui.CaptureFileAsync(CameraCaptureUIMode.Photo);
if (file != null)
{
byte[] myPicArray = await GetPhotoBytesAsync(file);
HttpClient httpClient = new HttpClient();
httpClient.BaseAddress = new Uri("http://yourdomainname.com");
MultipartFormDataContent form = new MultipartFormDataContent();
HttpContent content = new ByteArrayContent(myPicArray);
form.Add(content, "media", "filename.jpg");
content = new StringContent("my-username");
form.Add(content, "username");
HttpResponseMessage response = await httpClient.PostAsync("directory/my-site.php", form);
}
}
public async Task<byte[]> GetPhotoBytesAsync(StorageFile file)
{
IRandomAccessStream fileStream = await file.OpenAsync(FileAccessMode.Read);
var reader = new Windows.Storage.Streams.DataReader(fileStream.GetInputStreamAt(0));
await reader.LoadAsync((uint)fileStream.Size);
byte[] pixels = new byte[fileStream.Size];
reader.ReadBytes(pixels);
return pixels;
}
public string Post(string uri, HttpContent postData)
{
return PostAsync(uri, postData).Result;
}
public async Task<string> PostAsync(string uri, HttpContent httpContent)
{
string resultStream;
using (var httpClient = new HttpClient())
{
var response = await httpClient.PostAsync(uri, httpContent);
response.EnsureSuccessStatusCode();
resultStream = await response.Content.ReadAsStringAsync();
}
return resultStream;
}
protected async void TakePictureAndUpload()
{
var ui = new CameraCaptureUI();
var file = await ui.CaptureFileAsync(CameraCaptureUIMode.Photo);
if (file != null)
{
byte[] myPicArray = await GetPhotoBytesAsync(file);
HttpClient httpClient = new HttpClient();
httpClient.BaseAddress = new Uri("http://yourdomainname.com");
MultipartFormDataContent form = new MultipartFormDataContent();
HttpContent content = new ByteArrayContent(myPicArray);
form.Add(content, "media", "filename.jpg");
content = new StringContent("my-username");
form.Add(content, "username");
HttpResponseMessage response = await httpClient.PostAsync("directory/my-site.php", form);
}
}
public async Task<byte[]> GetPhotoBytesAsync(StorageFile file)
{
IRandomAccessStream fileStream = await file.OpenAsync(FileAccessMode.Read);
var reader = new Windows.Storage.Streams.DataReader(fileStream.GetInputStreamAt(0));
await reader.LoadAsync((uint)fileStream.Size);
byte[] pixels = new byte[fileStream.Size];
reader.ReadBytes(pixels);
return pixels;
}