我正在尝试在从 3rd 方服务下载请求的代码中使用 HttpRequestCachePolicy。我的代码目前是这样的:
protected virtual XmlDocument Send(XmlDocument requestDoc)
{
// Get a SOAP request document that wraps the user request
XmlDocument soapRequestDocument = NewSoapRequestDocument();
InsertRequestDocumentIntoSoapRequestDocument(requestDoc, soapRequestDocument);
Debug.WriteLine(soapRequestDocument.OuterXml);
// Process the request
HttpWebRequest webRequest = HttpWebRequest.Create(URI) as HttpWebRequest;
var policy = new HttpRequestCachePolicy(HttpRequestCacheLevel.CacheIfAvailable);
HttpWebRequest.DefaultCachePolicy = policy;
webRequest.CachePolicy = policy;
webRequest.Accept = "text/xml";
webRequest.Method = "POST";
webRequest.ContentType = "text/xml;charset=\"UTF-8\"";
webRequest.Headers.Add("SOAPAction", string.Format("\"{0}#{1}\"", Service, Method));
byte[] requestBytes = Encoding.UTF8.GetBytes(soapRequestDocument.OuterXml);
webRequest.ContentLength = requestBytes.Length;
Stream requestStream = webRequest.GetRequestStream();
requestStream.Write(requestBytes, 0, requestBytes.Length);
requestStream.Flush();
requestStream.Close();
// Process the response
WebResponse webResponse = webRequest.GetResponse();
Stream responseStream = webResponse.GetResponseStream();
StreamReader responseStreamReader = new StreamReader(responseStream);
// Create SOAP response document
XmlDocument soapResponseDocument = new XmlDocument();
soapResponseDocument.Load(responseStreamReader);
responseStreamReader.Close();
//close
responseStream.Close();
return GetResponseDocument(soapResponseDocument);
}
但是,没有任何请求被缓存,我需要以不同的方式使用它吗?