2
我有這個例子工作,但我想知道如何管理這個例子的超時正好。請幫幫我。 在此先感謝如何管理超時使用httpWebRequest和方法BeginGetRequestStream Windows Phone 7
public void callREST()
{
Uri uri = new Uri("http://www.domain.com/RestService");
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
request.Method = "POST";
request.ContentType = "application/xml";
request.BeginGetRequestStream(sendXML_RequestCallback, request);
}
private void sendXML_RequestCallback(IAsyncResult result)
{
var req = result.AsyncState as HttpWebRequest;
byte[] toSign = Encoding.GetEncoding("ISO-8859-1").GetBytes("<xml></xml>");
using (var strm = req.EndGetRequestStream(result))
{
strm.Write(toSign, 0, toSign.Length);
strm.Flush();
}
req.BeginGetResponse(this.fCallback, req);
}
private void fCallback(IAsyncResult result)
{
var req = result.AsyncState as HttpWebRequest;
var resp = req.EndGetResponse(result);
var strm = resp.GetResponseStream();
//Do something
}
是的,但我有兩個異步方法,響應在兩次完成。也許我應該管理兩個定時器? 什麼方法應該停止計時器? sendXML_RequestCallback或fCallback? – mlpaucara 2011-05-30 15:33:22
使用一個計時器。當你滿意的時候停止計時器。這可能是你有迴應時。這取決於你正在實現的超時時間。 – 2011-05-30 17:26:52