Example #1
0
        public static string PostString(BloomServer server, string endPoint, string data, ContentType returnType,
                                        EndpointHandler handler = null)
        {
            if (handler != null)
            {
                server.ApiHandler.RegisterEndpointHandler(endPoint, handler, true);
            }
            server.StartListening();
            var client = new WebClientWithTimeout
            {
                Timeout = 3000
            };

            client.Headers[HttpRequestHeader.ContentType] = returnType == ContentType.Text ? "text/plain" : "application/json";

            return(client.UploadString(BloomServer.ServerUrlWithBloomPrefixEndingInSlash + "api/" + endPoint, "POST", data));
        }
Example #2
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);
        }
        public string PostRequest(string url, string requestBody, Dictionary<string, string> requestHeaders)
        {
            WebClientWithTimeout client = new WebClientWithTimeout();
            client.Timeout = Timeout;

            if (requestHeaders != null)
            {
                foreach (KeyValuePair<string, string> keys in requestHeaders)
                    client.Headers.Add(keys.Key, keys.Value);
            }

            if (url.StartsWith("https://"))
            {
                //Sending secure, ignore any SSL errors
                ServicePointManager.ServerCertificateValidationCallback = (obj, certificate, chain, errors) => true;
            }

            string response;

            //for integration tests
            if (url.Contains("fakesuccess"))
            {
                string successXML =
                    "<RESULT><CODE>1</CODE><MESSAGE>Application Processed</MESSAGE><URL><![CDATA[http://whatever.com/ApplyLead?lala=TEST]]></URL></RESULT>";
                response = successXML;

                return response;
            }

            if(url.Contains("fakerejected"))
            {
                string successXML =
                   "<RESULT><CODE>0</CODE><MESSAGE>Rejected - no partners accepted this lead</MESSAGE><URL></URL></RESULT>";
                response = successXML;

                return response;
            }

            response = client.UploadString(url, requestBody);

            return response;
        }
Example #4
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;
        }