开发者

Capturing a webclient response and saving it to a variable

开发者 https://www.devze.com 2023-01-10 19:22 出处:网络
I have the following code: private void UploadSelectedImages(ListBox.ObjectCollection objectCollection)

I have the following code:

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());                    
        }
    }            
}

Is there a more efficient way of grabbing the response XML and convert it to string?


Use StringReader to read an string from a stream:

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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号