【问题标题】:Capturing a webclient response and saving it to a variable捕获 Web 客户端响应并将其保存到变量中
【发布时间】:2010-08-10 14:37:15
【问题描述】:

我有以下代码:

private void UploadSelectedImages(ListBox.ObjectCollection objectCollection)
{
    foreach (var photo in objectCollection)
    {
        using (var w = new WebClient())
        {
            var values = new NameValueCollection
            {
                { "key", "<MYAPIHERE>" },
                { "image", Convert.ToBase64String(File.ReadAllBytes(photo.ToString())) }
            };

            byte[] response = w.UploadValues("http://imgur.com/api/upload.xml", values);

            MessageBox.Show(XDocument.Load(new MemoryStream(response)).ToString());                    
        }
    }            
}

private void UploadSelectedImages(ListBox.ObjectCollection objectCollection)
{
    foreach (var photo in objectCollection)
    {
        using (var w = new WebClient())
        {
            var values = new NameValueCollection
            {
                { "key", "e0201e0b4528c146027c4f6dcd730787" },
                { "image", Convert.ToBase64String(File.ReadAllBytes(photo.ToString())) }
            };

            byte[] response = w.UploadValues("http://imgur.com/api/upload.xml", values);

            MessageBox.Show(XDocument.Load(new MemoryStream(response)).ToString());                    
        }
    }            
}

有没有更有效的方法来获取响应 XML 并将其转换为字符串?

【问题讨论】:

    标签: c# api webclient imgur


    【解决方案1】:

    使用 StringReader 从流中读取字符串:

    new StringReader(new MemoryStream(response)).ReadToEnd();
    

    【讨论】:

      猜你喜欢
      • 2020-08-12
      • 1970-01-01
      • 1970-01-01
      • 2021-08-21
      • 1970-01-01
      • 1970-01-01
      • 2019-07-28
      • 1970-01-01
      • 2013-04-26
      相关资源
      最近更新 更多