在C#WP8.1项目中使用Catchoom

时间:2014-07-09 13:26:25

标签: c# asp.net windows-phone-8 dotnet-httpclient

我想在c#中使用catchoom,但无法找到任何样本,如果有,请提供任何示例代码。

我有这个卷曲样本

 curl -F "image=@CATask1-Correct.png" -F "token=sometoken" https://r.catchoom.com/v1/search

有人可以将此转换为c#吗? 我尝试将其转换为如下所示:

这是我的代码:

public static async Task<string> Upload(byte[] image)
    {
        using (var client = new HttpClient())
        {
           // string boundary = "---XXX---";
            using (var content =  new MultipartFormDataContent())
            {

                string token = "sometoken";
                MemoryStream stream = new MemoryStream();
                StreamWriter writer = new StreamWriter(stream);
                writer.Write(token);
                writer.Flush();
                stream.Position = 0;

               // adding Token and Image to the request 
                content.Add(new StreamContent(stream), "token");

                content.Add(new StreamContent(new MemoryStream(image)), "bilddatei", "upload.jpg");

                using (

                     var message = await client.PostAsync("https://r.catchoom.com/v1/search", content)) 

                       {
                    var input = await message.Content.ReadAsStringAsync();

                    return input;
                }
            }
        }
    }

然后我在on-click事件处理程序中调用了该方法:

     private async void mybtn_Click(object sender, RoutedEventArgs e)
    {

        // converting the image to a byte array
        BitmapImage bitmapImage = new BitmapImage(new Uri("http://www.familyfuntwincities.com/wp-content/uploads/2013/09/apple_red_1_clipart.png?s=128&g=1"));
        RandomAccessStreamReference rasr = RandomAccessStreamReference.CreateFromUri(bitmapImage.UriSource);
        var streamWithContent = await rasr.OpenReadAsync();
        byte[] buffer = new byte[streamWithContent.Size];
        await streamWithContent.ReadAsync(buffer.AsBuffer(), (uint)streamWithContent.Size, InputStreamOptions.None);

 // calling the upload method
        string output = await Upload(buffer);
        mytext1.Text = output;

    }

但我仍然从catchoo服务器获得“图像丢失”错误,尽管我设法以相同的方式将图像上传到其他服务器(当然没有令牌部分)。

我的问题是:如何添加多部分内容? Token部分和图像部分之间的正确边界是什么才能被catchoom识别?

1 个答案:

答案 0 :(得分:0)

这对我有用:

public static async Task<string> Test(string token, Stream stream, string fileName)
{
    HttpClient client = new HttpClient();
    MultipartFormDataContent content = new MultipartFormDataContent();
    content.Add(new StringContent(token), "token");
    content.Add(new StreamContent(stream), "image", fileName);
    var resp = await client.PostAsync("https://r.catchoom.com/v1/search", content);
    return await resp.Content.ReadAsStringAsync();
}

如果您想使用样本中列出的图像,请按以下步骤进行操作:

HttpClient client = new HttpClient();
var stream = await client.GetStreamAsync("http://www.familyfuntwincities.com/wp-content/uploads/2013/09/apple_red_1_clipart.png?s=128&g=1");
return await Test("<SOME_TOKEN_VALUE>", stream, "apple.png");

然而,API声称由于Alpha透明度而导致图片不受支持。您可以找到一些其他示例,向您展示如果重要的话,如何剥离Alpha透明度。上面的代码适用于没有alpha透明度的图像。