Sign in | Join
Last post 06-04-2009 7:05 AM by ksirg. 2 replies.
Sort Posts: Oldest to newest Newest to oldest
Hi I have communication problem, I use HttpClient and send periodically (every 60 second) HttpGet message asynchronusly. But I get (to often) exception HttpStageProcessingException with message GetResponse timed out and I don't know why? The other strange thing is that when I open fiddler it starts working and this exception nerer occure
m_HttpClient.BeginSend( new HttpRequestMessage("GET", query), new AsyncCallback(AfterUserDashboardSince), m_HttpClient);
calback AfterUserDashboardSince looks :
1 private void AfterUserDashboardSince(IAsyncResult result) 2 { 3 4 var client = result.AsyncState as HttpClient; 5 6 7 HttpResponseMessage resp=null; 8 9 try 10 { 11 12 using (resp = client.EndSend(result)) 13 { 14 15 resp.EnsureStatusIsSuccessful(); 16 17 18 if (resp.Content.GetLength() > 2) 19 { 20 var statuses = resp.Content.ReadAsJsonDataContract<StatusesList>(); 21 22 23 if ((statuses.Count > 0) && (StatusesUpdated != null)) 24 { 25 //rise event 26 StatusesUpdated(this, new StatusesLoadingEventArgs(statuses)); 27 } 28 } 29 } 30 } 31 catch (ArgumentOutOfRangeException aorEx) 32 { 33 //rise event 34 if (CommunicationError != null) 35 { 36 CommunicationError(this, new CommunicationErrorEventArgs(resp.StatusCode)); 37 } 38 } 39 catch (HttpStageProcessingException timeEx) 40 { 41 42 //wtf 43 } 44 catch (Exception ex) 45 { 46 //todo 47 } 48 49 }
It's hard to say what is happening here without more details about the server. As a side note, if you're using this in a WinForms or WPF application, using SendAsync() and SendCompleted += handler might be easier than BeginSend.
I resolve this by locking the HttpClient object, when retrive response, and it worked
var client = result.AsyncState as HttpClient; HttpResponseMessage resp = null; try { lock (httpClientLock) { resp = client.EndSend(result); } //code }