2012-05-28 37 views
0

我有一個httpWebRequest來訪問XML並將其保存到本地,然後將其讀取並顯示到屏幕上。問題是,我有一個以上的「樞紐項目」做到這一點,而且節省了XML的方法是將變量推入方法

private static void GetResponseCallback(IAsyncResult asynchronousResult) 

,不支持增加一個新的變量,所以我可以動態命名XML (「tmp」+ xmlName +「。xml」)。

所以問題是:我如何推送一個變量在XML名稱?

public class HttpWebReqMethod 
    { 

     public void httpRequestMethod (string url, string xmlName) 
     {  
      HttpWebRequest httpRequest = (HttpWebRequest)WebRequest.Create(url); 
      httpRequest.ContentType = "text/xml"; 
      httpRequest.Method = "POST"; 

      httpRequest.BeginGetRequestStream(new AsyncCallback(GetRequestStreamCallback), httpRequest); 
     } 

     private static void GetRequestStreamCallback(IAsyncResult asynchronousResult) 
     { 
      HttpWebRequest httpRequest = (HttpWebRequest)asynchronousResult.AsyncState; 

      // End the operation 
      Stream postStream = httpRequest.EndGetRequestStream(asynchronousResult); 


      string postData = ""; 

      // Convert the string into a byte array. 
      byte[] byteArray = Encoding.UTF8.GetBytes(postData); 

      // Write to the request stream. 
      postStream.Write(byteArray, 0, postData.Length); 
      postStream.Close(); 

      // Start the asynchronous operation to get the response 
      httpRequest.BeginGetResponse(new AsyncCallback(GetResponseCallback), httpRequest); 
     } 

     private static void GetResponseCallback(IAsyncResult asynchronousResult) 
     { 
      HttpWebRequest httpRequest = (HttpWebRequest)asynchronousResult.AsyncState; 

      // End the operation 
      HttpWebResponse response = (HttpWebResponse)httpRequest.EndGetResponse(asynchronousResult); 
      Stream streamResponse = response.GetResponseStream(); 
      StreamReader streamRead = new StreamReader(streamResponse); 
      string responseStream = streamRead.ReadToEnd(); 

      using (var store = IsolatedStorageFile.GetUserStoreForApplication()) 
      { 
       using (var istream = new IsolatedStorageFileStream(@"tmp" + xmlName + ".xml", FileMode.OpenOrCreate, store)) 
       { 
        using (var sw = new StreamWriter(istream)) 
        { 
         sw.Write(responseStream); 
        } 
       } 
      } 

      // Close the stream object 
      streamResponse.Close(); 
      streamRead.Close(); 

      // Release the HttpWebResponse 
      response.Close(); 
     } 

回答

1

這裏有兩個事情可以做:

  1. 使GetResponseCallback不是靜態的,儲存在一個實例變量
  2. 的xmlName傳遞狀態對象(任何有一個名爲xmlName的屬性,以及其他用來標識它的內容),並且您可以從AsyncState
  3. 變化GetResponseCallback的功能,下面,使整個事情的回調 「工廠」

    private static AsyncCallback GetResponseCallback(string xmlName) 
    { 
        return (IAsyncResult asynchronousResult) =>{ 
        HttpWebRequest httpRequest = (HttpWebRequest)asynchronousResult.AsyncState; 
        // End the operation 
        HttpWebResponse response = (HttpWebResponse)httpRequest.EndGetResponse(asynchronousResult); 
        Stream streamResponse = response.GetResponseStream(); 
        StreamReader streamRead = new StreamReader(streamResponse); 
        string responseStream = streamRead.ReadToEnd(); 
    
        using (var store = IsolatedStorageFile.GetUserStoreForApplication()) 
        { 
         using (var istream = new IsolatedStorageFileStream(@"tmp" + xmlName + ".xml", FileMode.OpenOrCreate, store)) 
         { 
          using (var sw = new StreamWriter(istream)) 
          { 
           sw.Write(responseStream); 
          } 
         } 
        } 
    
        // Close the stream object 
        streamResponse.Close(); 
        streamRead.Close(); 
    
        // Release the HttpWebResponse 
        response.Close(); 
        } 
    } 
    

編輯補充:
使用則變爲

httpRequest.BeginGetRequestStream(GetRequestStreamCallback(xmlName), httpRequest); 
+0

哦,我看到rudi_visser選擇了第二個選項,並解釋它更好的:)還有第三個是一個很好的和對於某些場景可能非常有用。 – TDaver

+0

你能幫我打回「工廠」嗎?我得到:GetResponseCallback(字符串)'返回void,一個返回關鍵字不能跟在一個對象表達式 – observ

+0

請注意,我已經更改了SIGNATURE ... – TDaver

0

它一個映射到特定委託類型的回調方法,所以不能,你不能修改它的簽名。解決這個

一種選擇是將有一個單獨的類像這樣:

class HttpRequestState { 
    HttpWebRequest httpWebRequest; 
    string xmlFileName; 
} 

然後,您可以設置類作爲狀態對象的實例在運行開始回調:

httpRequest.BeginGetRequestStream(new AsyncCallback(GetRequestStreamCallback), httpRequest);

將變爲

httpRequest.BeginGetRequestStream(new AsyncCallback(GetRequestStreamCallback), new HttpRequestState() { httpWebRequest = httpRequest; xmlFileName = "tmp"+xmlName+".xml" });

然後,當然,你可以拉出來的xmlFileName 的HttpWebRequest的是這樣的:

HttpRequestState stateObj = (HttpRequestState)asynchronousResult.AsyncState; 
HttpWebRequest httpRequest = stateObj.httpWebRequest; 
string fileName = xmlFileName;