2010-08-10 20 views
0

J'ai le code suivant:Capture une réponse webclient et l'enregistrement à une variable

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

est-il un moyen plus efficace de saisir le code XML de réponse et le convertir en chaîne?

Répondre

0

Utilisez StringReader pour lire une chaîne à partir d'un flux:

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