Exemplo n.º 1
0
        private void LoadIPAsync()
        {
            Action <bool, string>      onCompletion = null;
            Action <Exception, string> onFail       = null;

            onCompletion = (success, output) => {
                if (!success)
                {
                    onFail(new Exception("Could not reach site."), output);
                    return;
                }
                if (this.PublicIP != null)
                {
                    return;
                }

                string[] a = output.Split(':');
                if (a.Length < 2)
                {
                    onFail(new Exception("Malformed IP output (1)."), output);
                    return;
                }

                string a2 = a[1].Substring(1);

                string[] a3 = a2.Split('<');
                if (a3.Length == 0)
                {
                    onFail(new Exception("Malformed IP output (2)."), output);
                    return;
                }

                string a4 = a3[0];

                this.PublicIP = a4;
            };

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

            WebConnectionHelpers.MakeGetRequestAsync("http://checkip.dyndns.org/", e => onFail(e, ""), onCompletion);
            //NetHelpers.MakeGetRequestAsync( "https://api.ipify.org/", onSuccess, onFail );
            //using( WebClient webClient = new WebClient() ) {
            //	this.PublicIP = webClient.DownloadString( "http://ifconfig.me/ip" );
            //}
        }
Exemplo n.º 2
0
        ////////////////

        /// <summary>
        /// Sends a POST request to a website asynchronously.
        /// </summary>
        /// <param name="url">Website URL.</param>
        /// <param name="jsonData">JSON-formatted string data.</param>
        /// <param name="onError">Called on error. Receives an `Exception`.</param>
        /// <param name="onCompletion">Called regardless of success. Receives a boolean indicating if the site request succeeded,
        /// and the output (if any).</param>
        public static void MakePostRequestAsync(string url, string jsonData,
                                                Action <Exception> onError,
                                                Action <bool, string> onCompletion = null)
        {
            //var cts = new CancellationTokenSource();

            try {
                Task.Run(() => {
                    ServicePointManager.Expect100Continue = false;
                    //var values = new NameValueCollection {
                    //	{ "modloaderversion", ModLoader.versionedName },
                    //	{ "platform", ModLoader.CompressedPlatformRepresentation },
                    //	{ "netversion", FrameworkVersion.Version.ToString() }
                    //};

                    using (var client = new WebClient()) {
                        ServicePointManager.ServerCertificateValidationCallback = (sender, certificate, chain, policyErrors) => { return(true); };

                        client.Headers.Add(HttpRequestHeader.ContentType, "application/json");
                        client.Headers.Add(HttpRequestHeader.UserAgent, "tModLoader " + ModLoader.version.ToString());
                        //client.Headers["UserAgent"] = "tModLoader " + ModLoader.version.ToString();
                        client.UploadStringAsync(new Uri(url), "POST", jsonData);                            //UploadValuesAsync( new Uri( url ), "POST", values );
                        client.UploadStringCompleted += (sender, e) => {
                            WebConnectionHelpers.HandleResponse(sender, e, onError, onCompletion);
                        };
                    }
                });                 //, cts.Token );
            } catch (WebException e) {
                if (e.Status == WebExceptionStatus.Timeout)
                {
                    onCompletion?.Invoke(false, "Timeout.");
                    return;
                }

                if (e.Status == WebExceptionStatus.ProtocolError)
                {
                    var resp = (HttpWebResponse)e.Response;
                    if (resp.StatusCode == HttpStatusCode.NotFound)
                    {
                        onCompletion?.Invoke(false, "Not found.");
                        return;
                    }

                    onCompletion?.Invoke(false, "Bad protocol.");
                }
            } catch (Exception e) {
                onError?.Invoke(e);
                //LogHelpers.Warn( e.ToString() );
            }
        }
Exemplo n.º 3
0
        ////////////////

        /// <summary>
        /// Makes a GET request to a website.
        /// </summary>
        /// <param name="url">Website URL.</param>
        /// <param name="onError">Called on error. Receives an `Exception`.</param>
        /// <param name="onCompletion">Called regardless of success. Receives a boolean indicating if the site request succeeded,
        /// and the output (if any).</param>
        public static void MakeGetRequestAsync(string url,
                                               Action <Exception> onError,
                                               Action <bool, string> onCompletion = null)
        {
            Task.Run(() => {
                //var cts = new CancellationTokenSource();
                try {
                    ServicePointManager.Expect100Continue = false;

                    using (var client = new WebClient()) {
                        ServicePointManager.ServerCertificateValidationCallback =
                            (sender, certificate, chain, policyErrors) => { return(true); };

                        client.Headers.Add(HttpRequestHeader.UserAgent, "tModLoader " + ModLoader.version.ToString());
                        client.DownloadStringAsync(new Uri(url));
                        client.DownloadStringCompleted += (sender, e) => {
                            WebConnectionHelpers.HandleResponse(sender, e, onError, onCompletion);
                        };
                        //client.UploadStringAsync( new Uri(url), "GET", "" );//UploadValuesAsync( new Uri( url ), "POST", values );
                    }
                } catch (WebException e) {
                    if (e.Status == WebExceptionStatus.Timeout)
                    {
                        onCompletion?.Invoke(false, "Timeout.");
                        return;
                    }

                    if (e.Status == WebExceptionStatus.ProtocolError)
                    {
                        var resp = (HttpWebResponse)e.Response;
                        if (resp.StatusCode == HttpStatusCode.NotFound)
                        {
                            onCompletion?.Invoke(false, "Not found.");
                            return;
                        }

                        onCompletion?.Invoke(false, "Bad protocol.");
                    }
                } catch (Exception e) {
                    onError?.Invoke(e);
                    //LogHelpers.Warn( e.ToString() );
                }
            });             //, cts.Token );
        }
 public static void MakeGetRequestAsync(string url,
                                        Action <Exception, string> onError,
                                        Action <bool, string> onCompletion = null)
 {
     WebConnectionHelpers.MakeGetRequestAsync(url, (e) => onError(e, ""), onCompletion);
 }