2011-03-18 84 views
0

對於Windows Phone 7的httpWebRequest和NetworkCredential,或Windows Phone 7的Silverlight,它們是非常新的。它們似乎與以前的.net版本有很大不同。希望有人可以給我看一個示例代碼,以供以上學習。希望你的幫助。如何使用HttpWebRequest和NetworkCredential從服務器下載圖像(jpg)

我迷路了:

HttpWebRequest httpReq = (HttpWebRequest)HttpWebRequest.Create(new Uri("http://xxx/myImage.jpg")); 
httpReq.BeginGetResponse(HTTPWebRequestCallBack, httpReq); 

回答

0

如果你不介意去看看WebClient類。它有更方便的下載圖像的方法。

這裏是MSDN示例代碼:

string remoteUri = "http://www.contoso.com/library/homepage/images/"; 
string fileName = "ms-banner.gif", myStringWebResource = null; 
// Create a new WebClient instance. 
WebClient myWebClient = new WebClient(); 
// Concatenate the domain with the Web resource filename. 
myStringWebResource = remoteUri + fileName; 
Console.WriteLine("Downloading File \"{0}\" from \"{1}\" .......\n\n", fileName, myStringWebResource); 
// Download the Web resource and save it into the current filesystem folder. 
myWebClient.DownloadFile(myStringWebResource,fileName);   
Console.WriteLine("Successfully Downloaded File \"{0}\" from \"{1}\"", fileName, myStringWebResource); 
Console.WriteLine("\nDownloaded file saved in the following file system folder:\n\t" + Application.StartupPath); 
+0

塞拉芬:將不勝感激,如果你能看到裏面的HttpWebRequest方法。瞭解WebClient是一個包裝,可能有其他問題。謝謝 – MilkBottle 2011-03-18 08:11:35

1

試試這個,也許有幫助你

HttpWebRequest reqest = (HttpWebRequest)WebRequest.Create("your url"); 
reqest.BeginGetResponse(ReadCallback, reqest); 
//callback method defination........ 

void ReadCallback(IAsyncResult result) 
{ 
    HttpWebRequest req = (HttpWebRequest)result.AsyncState; 
    HttpWebResponse responce = (HttpWebResponse)req.EndGetResponse(result); 
    Stream s = responce.GetResponseStream(); 
    StreamReader str = new StreamReader(responce.GetResponseStream()); 
    { 
     nowparsingstring = str.ReadToEnd(); 
    } 
} 
相關問題