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

2 回答 2

1

以下是您可以做的两件事:

  1. 使 GetResponseCallback 不是静态的,并将xmlName 存储在实例变量中
  2. 将一个状态对象(任何具有名为 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);
于 2012-05-28T20:23:37.480 回答
0

这是一个映射到特定委托类型的回调方法,所以不,你不能修改它的签名。

解决此问题的一种选择是有一个单独的类,例如:

class HttpRequestState {
    HttpWebRequest httpWebRequest;
    string xmlFileName;
}

然后,您可以在运行 Begin 回调时将该类的实例设置为状态对象:

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

会变成

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

然后,当然,您可以像这样提取 xmlFileNamehttpWebRequest:

HttpRequestState stateObj = (HttpRequestState)asynchronousResult.AsyncState;
HttpWebRequest httpRequest = stateObj.httpWebRequest;
string fileName = xmlFileName;
于 2012-05-28T20:24:17.083 回答