2010-08-10 79 views
0

我有以下代碼:捕捉WebClient的響應,並將其保存到一個變量

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並將其轉換爲字符串的更有效的方法?

回答

0

使用StringReader來讀取流的字符串:

new StringReader(new MemoryStream(response)).ReadToEnd(); 
相關問題