public static string ReadJson(string url, int timeoutMs = WebClientEx.DEFAULT_HTTP_TIMEOUT) { var request = WebRequest.Create(url); request.Timeout = timeoutMs; request.Credentials = CredentialCache.DefaultCredentials; var response = request.GetResponse(); var responceStream = response.GetResponseStream(); try { var ms = new MemoryStream(); responceStream.CopyTo(ms); byte[] data = ms.ToArray(); return(JsonHelper.BytesToJson(data)); } catch { return(""); } finally { responceStream.Close(); response.Close(); } }
public void ReadNewData() { // Read and parse request var buffer = new byte[0] { }; // TODO: add request timeout while (!this.request.RequestError) { try { byte[] incomingPacket = this.request.ReadIncomingPacket(stream, socket); buffer = StringHelper.CombineByteArrays(buffer, incomingPacket); if (buffer.Length > 0 && incomingPacket.Length == 0) { OnFrameRecived(HttpWebSocketFrame.DecodeFrame(buffer)); break; } else if (incomingPacket.Length != 0) { // wait until entire request is recived Thread.Sleep(1); } Thread.Sleep(1); } catch { this.Open = false; return; } } NewData(JsonHelper.BytesToJson(HttpWebSocketFrame.DecodeFrame(buffer).Payload)); }