Exemplo n.º 1
0
#pragma warning disable S1541 // Methods and properties should not be too complex
        public string ExecuteRequest(int timeoutMilliseconds, string method, string url, string data, List <Tuple <string, string> > headers, Action <string> asyncCallback)
#pragma warning restore S1541 // Methods and properties should not be too complex
        {
            using (var webClient = new WebClientWithTimeout(timeoutMilliseconds))
            {
                webClient.Credentials = CredentialCache.DefaultCredentials;

                if (headers != null)
                {
                    foreach (var header in headers)
                    {
                        webClient.Headers.Add(header.Item1, header.Item2);
                    }
                }

                var uri = new Uri(url.Contains("http://") || url.Contains("https://") ? url : "http://" + url);

                switch (method)
                {
                case "GET":
                    if (asyncCallback == null)
                    {
                        return(webClient.DownloadString(uri));
                    }
                    webClient.DownloadStringCompleted += (sender, args) => asyncCallback?.Invoke(args.Result);
                    webClient.DownloadStringAsync(uri, null);
                    break;

                case "POST":
                    if (asyncCallback == null)
                    {
                        return(webClient.UploadString(uri, data));
                    }
                    webClient.UploadStringCompleted += (sender, args) => asyncCallback?.Invoke(args.Result);
                    webClient.UploadStringAsync(uri, data);
                    break;

                default:
                    return(string.Empty);
                }
            }
            return(string.Empty);
        }
Exemplo n.º 2
0
        // TODO: factor out the guts of this and the default timout method above with a private method taking a WebClient object
        public string ExecuteRequest(int timeoutMilliseconds, string method, string url, string data, List<Tuple<string, string>> headers = null, Action<string> asyncCallback = null)
        {
            using (var webClient = new WebClientWithTimeout(timeoutMilliseconds))
            {
                webClient.Credentials = CredentialCache.DefaultCredentials;

                if (headers != null)
                {
                    foreach (var header in headers)
                    {
                        webClient.Headers.Add(header.Item1, header.Item2);
                    }
                }

                var uri = new Uri(url.Contains("http://") || url.Contains("https://") ? url : "http://" + url);

                switch (method)
                {
                    case "GET":
                        if (asyncCallback == null)
                        {
                            return webClient.DownloadString(uri);
                        }
                        webClient.DownloadStringCompleted += (sender, args) => asyncCallback(args.Result);
                        webClient.DownloadStringAsync(uri, null);
                        break;
                    case "POST":
                        if (asyncCallback == null)
                        {
                            return webClient.UploadString(uri, data);
                        }
                        webClient.UploadStringCompleted += (sender, args) => asyncCallback(args.Result);
                        webClient.UploadStringAsync(uri, data);
                        break;
                }
            }
            return string.Empty;
        }