我有一个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();
}
答案 0 :(得分:1)
您可以做以下两件事:
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);
答案 1 :(得分: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" });
然后,当然,您可以像这样提取xmlFileName 和 httpWebRequest:
HttpRequestState stateObj = (HttpRequestState)asynchronousResult.AsyncState;
HttpWebRequest httpRequest = stateObj.httpWebRequest;
string fileName = xmlFileName;