Exemple #1
0
        public static void MakePostRequestAsync(string url, byte[] bytes, Action <string> onResponse, Action <Exception, string> onError, Action onCompletion = null)
        {
            ThreadPool.QueueUserWorkItem(_ => {
                bool success;
                string output = null;

                try {
                    //lock( NetHelpers.RequestMutex ) {
                    output = NetHelpers.MakePostRequest(url, bytes, out success);
                    //}

                    if (success)
                    {
                        onResponse(output);
                    }
                    else
                    {
                        output = null;
                        throw new HamstarException("POST request unsuccessful (url: " + url + ")");
                    }
                } catch (Exception e) {
                    onError?.Invoke(e, output);
                }

                onCompletion?.Invoke();
            });
        }
Exemple #2
0
        ////////////////

        public static void MakeGetRequestAsync <T>(string url,
                                                   Func <string, Tuple <T, bool> > onResponse,
                                                   Action <Exception, string> onError,
                                                   Action <T, bool> onCompletion = null) where T : class
        {
            ThreadPool.QueueUserWorkItem(_ => {
                bool success  = false;
                string output = null;

                var responseVal = Tuple.Create((T)null, false);

                try {
                    //lock( NetHelpers.RequestMutex ) {
                    output = NetHelpers.MakeGetRequest(url, out success);
                    //}

                    if (success)
                    {
                        responseVal = onResponse(output);
                    }
                    else
                    {
                        onError?.Invoke(new Exception("GET request unsuccessful (url: " + url + ")"), output ?? "");
                    }
                } catch (Exception e) {
                    onError?.Invoke(e, output ?? "");
                }

                onCompletion?.Invoke(responseVal.Item1, (success && responseVal.Item2));
            });
        }
Exemple #3
0
        private void LoadIPAsync()
        {
            Func <string, Tuple <object, bool> > onResponse = ( string output ) => {
                if (this.PublicIP != null)
                {
                    return(null);
                }

                string[] a  = output.Split(':');
                string   a2 = a[1].Substring(1);
                string[] a3 = a2.Split('<');
                string   a4 = a3[0];

                this.PublicIP = a4;

                return(Tuple.Create((object)null, true));
            };

            Action <Exception, string> onFail = delegate(Exception e, string output) {
                if (e is WebException)
                {
                    LogHelpers.Log("Could not acquire IP: " + e.Message);
                }
                else
                {
                    LogHelpers.Log("Could not acquire IP: " + e.ToString());
                }
            };

            NetHelpers.MakeGetRequestAsync <object>("http://checkip.dyndns.org/", onResponse, onFail);
            //NetHelpers.MakeGetRequestAsync( "https://api.ipify.org/", onSuccess, onFail );
            //using( WebClient webClient = new WebClient() ) {
            //	this.PublicIP = webClient.DownloadString( "http://ifconfig.me/ip" );
            //}
        }