コード例 #1
0
        private static NetworkResponse SendWebSocketRequest(string request, string host)
        {
            try
            {
                var response = new NetworkResponse();

                using (var ws = new WebSocket(host))
                {
                    ws.OnMessage += (sender, e) =>
                    {
                        response.RawResponse     = e.Data;
                        response.JobjectResponse = JsonConvert.DeserializeObject <JObject>(response.RawResponse);
                        ws.Close();
                    };

                    ws.OnError += (sender, e) =>
                    {
                        response.RawResponse = e.Exception.InnerException.ToString();
                        ws.Close();
                    };

                    ws.Connect();
                    ws.Send(request);

                    while (ws.IsAlive)
                    {
                    }

                    if (Convert.ToInt32(response.JobjectResponse.GetValue("Error")) == 0)
                    {
                        return(response);
                    }

                    throw new NetworkException("An error response was received from the server.", response, Protocol.REST, request);
                }
            }
            catch { throw; }
        }
コード例 #2
0
        private static NetworkResponse SendRpcRequest(string request, string requestType, string host)
        {
            NetworkResponse response = null;

            HttpWebRequest webRequest = null;

            byte[] byteArray;

            // Create WebRequest object
            try
            {
                webRequest             = (HttpWebRequest)WebRequest.Create(host);
                webRequest.ContentType = "application/json-rpc";
                webRequest.Method      = requestType;
                byteArray = Encoding.UTF8.GetBytes(request);
                webRequest.ContentLength = byteArray.Length;
            }
            catch (Exception ex)
            {
                throw new NetworkException("Error while creating HttpWebRequest object", ex.InnerException);
            }

            // Send network request
            try
            {
                using (var requestStream = webRequest.GetRequestStream())
                {
                    requestStream.Write(byteArray, 0, byteArray.Length);
                }
            }
            catch (WebException)
            {
                throw;
            }


            response = new NetworkResponse();
            //Receive response from network node
            try
            {
                WebResponse webResponse;
                using (webResponse = webRequest.GetResponse())
                {
                    using (var str = webResponse.GetResponseStream())
                    {
                        using (var sr = new StreamReader(str))
                        {
                            response.RawResponse     = sr.ReadToEnd();
                            response.JobjectResponse = JsonConvert.DeserializeObject <JObject>(response.RawResponse);

                            if (Convert.ToInt32(response.JobjectResponse.GetValue("error")) == 0)
                            {
                                return(response);
                            }

                            throw new NetworkException("An error response was received from the server.", response, Protocol.RPC, request);
                        }
                    }
                }
            }
            catch (WebException)
            {
                throw;
            }
        }
コード例 #3
0
        private static NetworkResponse SendRestRequest(string request, string requestType, string method, string host)
        {
            NetworkResponse response   = null;
            HttpWebRequest  webRequest = null;

            // Create WebRequest object
            try
            {
                switch (requestType)
                {
                case "GET":
                    webRequest = (HttpWebRequest)WebRequest.Create(host + request);
                    break;

                case "POST":
                    webRequest = (HttpWebRequest)WebRequest.Create(host + method);
                    break;

                default:
                    break;
                }

                webRequest.ContentType = "application/json";
                webRequest.Method      = requestType;
            }
            catch (Exception ex)
            {
                throw new Exception("Error while creating HttpWebRequest object", ex.InnerException);
            }

            response = new NetworkResponse();

            // Send request and receive network response
            try
            {
                if (requestType == "POST")
                {
                    var byteArray = Encoding.UTF8.GetBytes(request);
                    webRequest.ContentLength = byteArray.Length;

                    using (var stream = webRequest.GetRequestStream())
                    {
                        stream.Write(byteArray, 0, byteArray.Length);
                    }
                }

                WebResponse webResponse;
                using (webResponse = webRequest.GetResponse())
                {
                    using (var str = webResponse.GetResponseStream())
                    {
                        using (var sr = new StreamReader(str))
                        {
                            response.RawResponse     = sr.ReadToEnd();
                            response.JobjectResponse = JsonConvert.DeserializeObject <JObject>(response.RawResponse);
                            if (Convert.ToInt32(response.JobjectResponse.GetValue("Error")) == 0)
                            {
                                return(response);
                            }
                            else
                            {
                                throw new ArgumentException(response.JobjectResponse + "");
                            }

                            throw new NetworkException("An error response was received from the server.", response, Protocol.REST, request);
                        }
                    }
                }
            }
            catch (WebException)
            {
                throw;
            }
        }