我有一個基於MonoTouch的iOS通用應用程序。它使用REST服務進行調用以獲取數據。我使用HttpWebRequest類來構建和打電話。一切都很好,除了它似乎保持了記憶。我已經在代碼中使用了限制事物的範圍。我避免了匿名代表以及我聽說他們可能成爲問題。我有一個助手類,它可以建立我對REST服務的呼叫。當我打電話時,似乎只是通過撥打我的電話來保持記憶。我很好奇,如果有人遇到類似的問題與HttpWebClient和如何處理它。我目前正在查看是否可以使用nsMutableRequest進行調用,並避免使用HttpWebClient,但我正在努力使其與NTLM身份驗證一起工作。任何建議表示讚賞。HttpWebClient在MonoTouch中使用高內存
protected T IntegrationCall<T,I>(string methodName, I input) {
HttpWebRequest invokeRequest = BuildWebRequest<I>(GetMethodURL(methodName),"POST",input, true);
WebResponse response = invokeRequest.GetResponse();
T result = DeserializeResponseObject<T>((HttpWebResponse)response);
invokeRequest = null;
response = null;
return result;
}
protected HttpWebRequest BuildWebRequest<T>(string url, string method, T requestObject, bool IncludeCredentials)
{
ServicePointManager.ServerCertificateValidationCallback = Validator;
var invokeRequest = WebRequest.Create(url) as HttpWebRequest;
if (invokeRequest == null)
return null;
if (IncludeCredentials)
{
invokeRequest.Credentials = CommonData.IntegrationCredentials;
}
if(!string.IsNullOrEmpty(method))
invokeRequest.Method = method;
else
invokeRequest.Method = "POST";
invokeRequest.ContentType = "text/xml";
invokeRequest.Timeout = 40000;
using(Stream requestObjectStream = new MemoryStream())
{
DataContractSerializer serializedObject = new DataContractSerializer(typeof(T));
serializedObject.WriteObject(requestObjectStream, requestObject);
requestObjectStream.Position = 0;
using(StreamReader reader = new StreamReader(requestObjectStream))
{
string strTempRequestObject = reader.ReadToEnd();
//byte[] requestBodyBytes = Encoding.UTF8.GetBytes(strTempRequestObject);
Encoding enc = new UTF8Encoding(false);
byte[] requestBodyBytes = enc.GetBytes(strTempRequestObject);
invokeRequest.ContentLength = requestBodyBytes.Length;
using (Stream postStream = invokeRequest.GetRequestStream())
{
postStream.Write(requestBodyBytes, 0, requestBodyBytes.Length);
}
}
}
return invokeRequest;
}
您是否嘗試過使用內存分析器?您使用的數據有多大? – svick