我正在尝试在代码中使用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);
}
但是,没有任何请求被缓存,我是否需要以不同方式使用它?