Example #1
0
        private static string SubmitRequestToWebService(APIType api,
                                                        string uri,
                                                        WebMethod method,
                                                        ContentType contentType,
                                                        ContentType acceptType,
                                                        string data = "")
        {
            var uBuilder = new UriBuilder(APIMapper(api));

            uBuilder.Path += uri;

            ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12; //3072
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uBuilder.Uri);

            if (GlobalAppInfo.User != null)
            {
                var auth = string.Format("{0}:{1}", GlobalAppInfo.User.Username, GlobalAppInfo.User.Password);
                var enc  = Convert.ToBase64String(Encoding.ASCII.GetBytes(auth));
                var cred = string.Format("{0} {1}", "Basic", enc);

                request.Headers.Add("Authorization", cred);
            }

            request.Method      = method.Description();
            request.Accept      = acceptType.Description();
            request.ContentType = contentType.Description();
            if (method == WebMethod.POST && !string.IsNullOrWhiteSpace(data))
            {
                var edata = Encoding.ASCII.GetBytes(data);
                request.ContentLength = edata.Length;
                using (var stm = request.GetRequestStream())
                {
                    stm.Write(edata, 0, edata.Length);
                }
            }

            var response = "";

            try
            {
                using (var responseStm = request.GetResponse().GetResponseStream())
                {
                    using (var sReader = new StreamReader(responseStm))
                    {
                        response = sReader.ReadToEnd();
                    }
                }
            }
            catch
            {
            }

            return(response);
        }
Example #2
0
        private static async Task <string> SubmitRequestToWebService(APIType api,
                                                                     string uri,
                                                                     WebMethod method,
                                                                     ContentType contentType,
                                                                     ContentType acceptType,
                                                                     string data = "")
        {
            var uBuilder = new UriBuilder(APIMapper(api));

            uBuilder.Path += uri;

            ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12; //3072
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uBuilder.Uri);

            request.Method      = method.Description();
            request.Accept      = acceptType.Description();
            request.ContentType = contentType.Description();
            if (method == WebMethod.POST && !string.IsNullOrWhiteSpace(data))
            {
                var edata = Encoding.ASCII.GetBytes(data);
                request.ContentLength = edata.Length;
                using (var stm = await request.GetRequestStreamAsync().ConfigureAwait(false))
                {
                    await stm.WriteAsync(edata, 0, edata.Length);
                }
            }

            var response = "";

            try
            {
                using (var theWebTask = request.GetResponseAsync())
                {
                    using (var theWebResp = await theWebTask.ConfigureAwait(false))
                    {
                        using (var sReader = new StreamReader(theWebResp.GetResponseStream()))
                        {
                            response = await sReader.ReadToEndAsync().ConfigureAwait(false);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
            }

            return(response);
        }