将变量推送到方法中
本文关键字:方法 变量 | 更新日期: 2023-09-27 18:35:30
我有一个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();
}
以下是您可以做的两件事:
- 使 GetResponseCallback 不是静态的,并将 xmlName 存储在实例变量中
- 将状态对象(具有名为 xmlName 的属性的任何内容,以及用于标识它的其他内容)传递给请求,您可以从
AsyncState
-
将 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);
这是一个映射到特定委托类型的回调方法,所以不,你不能修改它的签名。
解决此问题的一种选择是使用一个单独的类,如下所示:
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" });
然后,当然,你可以像这样拉出xmlFileName和httpWebRequest:
HttpRequestState stateObj = (HttpRequestState)asynchronousResult.AsyncState;
HttpWebRequest httpRequest = stateObj.httpWebRequest;
string fileName = xmlFileName;