public void GetPhoto(string jid, BinarySuccessCallback bcb, ErrorCallback ecb) { Send( "/photo", ReceiveMode.Blob, sw => sw.Write("token=" + HttpUtility.UrlEncode(token) + "&jid" + HttpUtility.UrlEncode(jid)), null, bcb, ecb, null ); }
private void Send( string uri, ReceiveMode mode, WriteDataCallback wdcb, SuccessCallback scb, BinarySuccessCallback bcb, ErrorCallback ecb, FinishedCallback fcb) { if (!LoggedIn && uri != "/login" && uri != "/crashreport") { throw new InvalidOperationException("Not logged in"); } var req = WebRequest.CreateHttp(((uri == "/login" || uri == "/crashreport") ? DefaultRootUrl : RootUrl) + uri); req.ContentType = "application/x-www-form-urlencoded"; req.Method = "POST"; var waitHandle = new ManualResetEvent(false); req.BeginGetRequestStream(ar => { try { using (var requestStream = req.EndGetRequestStream(ar)) { using (var sr = new StreamWriter(requestStream)) { wdcb(sr); } } req.BeginGetResponse( a => { waitHandle.Set(); try { var response = (HttpWebResponse) req.EndGetResponse(a); var responseStream = response.GetResponseStream(); if (mode == ReceiveMode.Blob) { var data = new byte[response.ContentLength]; responseStream.BeginRead( data, 0, (int) response.ContentLength, result => { if (result.IsCompleted) { bcb(response.ContentType, data); } else { ecb("Incomplete response"); } }, null ); } else { using (var sr = new StreamReader(responseStream)) { switch (mode) { case ReceiveMode.Lines: string line; while ((line = sr.ReadLine()) != null) { if (line != string.Empty) { scb(line); } } break; case ReceiveMode.SingleString: scb(sr.ReadToEnd()); break; } if (fcb != null) { fcb(); } } } } catch (WebException e) { if(e.Status == WebExceptionStatus.RequestCanceled) { ecb(""); return; } var response = (HttpWebResponse) e.Response; if (response.StatusCode == HttpStatusCode.Forbidden) { LoggedIn = false; } try { using (var responseStream = response.GetResponseStream()) { using (var sr = new StreamReader(responseStream)) { ecb(sr.ReadToEnd()); } } } catch (Exception ex) { // What is wrong with this platform?! ecb(ex.Message + "\n" + e.Message); } } }, null ); } catch(WebException) { // The request was aborted ecb(""); } }, null); ThreadPool.QueueUserWorkItem( state => { if (!waitHandle.WaitOne(MessageTimeout)) { (state as HttpWebRequest).Abort(); } }, req ); }
public void GetPhoto(string jid, BinarySuccessCallback bcb, ErrorCallback ecb) { Send( "/photo", ReceiveMode.Blob, sw => sw.Write("token=" + HttpUtility.UrlEncode(token) + "&jid" + HttpUtility.UrlEncode(jid)), null, bcb, ecb, null ); }
private void Send( string uri, ReceiveMode mode, WriteDataCallback wdcb, SuccessCallback scb, BinarySuccessCallback bcb, ErrorCallback ecb, FinishedCallback fcb) { if (!LoggedIn && uri != "/login" && uri != "/crashreport") { throw new InvalidOperationException("Not logged in"); } var req = WebRequest.CreateHttp(((uri == "/login" || uri == "/crashreport") ? DefaultRootUrl : RootUrl) + uri); req.ContentType = "application/x-www-form-urlencoded"; req.Method = "POST"; var waitHandle = new ManualResetEvent(false); req.BeginGetRequestStream(ar => { try { using (var requestStream = req.EndGetRequestStream(ar)) { using (var sr = new StreamWriter(requestStream)) { wdcb(sr); } } req.BeginGetResponse( a => { waitHandle.Set(); try { var response = (HttpWebResponse)req.EndGetResponse(a); var responseStream = response.GetResponseStream(); if (mode == ReceiveMode.Blob) { var data = new byte[response.ContentLength]; responseStream.BeginRead( data, 0, (int)response.ContentLength, result => { if (result.IsCompleted) { bcb(response.ContentType, data); } else { ecb("Incomplete response"); } }, null ); } else { using (var sr = new StreamReader(responseStream)) { switch (mode) { case ReceiveMode.Lines: string line; while ((line = sr.ReadLine()) != null) { if (line != string.Empty) { scb(line); } } break; case ReceiveMode.SingleString: scb(sr.ReadToEnd()); break; } if (fcb != null) { fcb(); } } } } catch (WebException e) { if (e.Status == WebExceptionStatus.RequestCanceled) { ecb(""); return; } var response = (HttpWebResponse)e.Response; if (response.StatusCode == HttpStatusCode.Forbidden) { LoggedIn = false; } try { using (var responseStream = response.GetResponseStream()) { using (var sr = new StreamReader(responseStream)) { ecb(sr.ReadToEnd()); } } } catch (Exception ex) { // What is wrong with this platform?! ecb(ex.Message + "\n" + e.Message); } } }, null ); } catch (WebException) { // The request was aborted ecb(""); } }, null); ThreadPool.QueueUserWorkItem( state => { if (!waitHandle.WaitOne(MessageTimeout)) { (state as HttpWebRequest).Abort(); } }, req ); }