public IConsumerResponse Run(IConsumerRequest consumerRequest)
        {
            HttpWebRequest webRequest = consumerRequest.ToWebRequest();
            IConsumerResponse consumerResponse = null;

            var stopwatch = Stopwatch.StartNew();

            try
            {
                var httpWebResponse = webRequest.GetResponse() as HttpWebResponse;
                consumerResponse = new ConsumerResponse(httpWebResponse, GetElapsedTimespan(stopwatch));
            }
            catch (WebException webEx)
            {
                // I *think* it's safe to assume that the response will always be a HttpWebResponse...
                HttpWebResponse httpWebResponse = (HttpWebResponse)(webEx.Response);

                if (httpWebResponse == null)
                {
                    throw new ApplicationException("An HttpWebResponse could not be obtained from the WebException. Status was " + webEx.Status, webEx);
                }

                consumerResponse = new ConsumerResponse(httpWebResponse, webEx, GetElapsedTimespan(stopwatch));
            }

            return consumerResponse;
        }
Example #2
0
        public IConsumerResponse Run(IConsumerRequest consumerRequest)
        {
            HttpWebRequest    webRequest       = consumerRequest.ToWebRequest();
            IConsumerResponse consumerResponse = null;

            var stopwatch = Stopwatch.StartNew();

            try
            {
                var httpWebResponse = webRequest.GetResponse() as HttpWebResponse;
                consumerResponse = new ConsumerResponse(httpWebResponse, GetElapsedTimespan(stopwatch));
            }
            catch (WebException webEx)
            {
                // I *think* it's safe to assume that the response will always be a HttpWebResponse...
                HttpWebResponse httpWebResponse = (HttpWebResponse)(webEx.Response);

                if (httpWebResponse == null)
                {
                    throw new ApplicationException("An HttpWebResponse could not be obtained from the WebException. Status was " + webEx.Status, webEx);
                }

                consumerResponse = new ConsumerResponse(httpWebResponse, webEx, GetElapsedTimespan(stopwatch));
            }

            return(consumerResponse);
        }
        public IConsumerResponse Run(IConsumerRequest consumerRequest)
        {
            HttpWebRequest webRequest = consumerRequest.ToWebRequest();
            IConsumerResponse consumerResponse = null;

            try
            {
                consumerResponse = new ConsumerResponse(webRequest.GetResponse() as HttpWebResponse);
            }
            catch (WebException webEx)
            {
                // I *think* it's safe to assume that the response will always be a HttpWebResponse...
                HttpWebResponse httpWebResponse = (HttpWebResponse)(webEx.Response);

                if (httpWebResponse == null)
                {
                    throw new ApplicationException("An HttpWebResponse could not be obtained from the WebException");
                }

                consumerResponse = new ConsumerResponse(httpWebResponse, webEx);
            }

            return consumerResponse;
        }
        public IConsumerResponse Run(IConsumerRequest consumerRequest)
        {
            HttpWebRequest webRequest = consumerRequest.ToWebRequest();
            IConsumerResponse consumerResponse = null;

            var stopwatch = Stopwatch.StartNew();

            try
            {
                var httpWebResponse = webRequest.GetResponse() as HttpWebResponse;
                consumerResponse = new ConsumerResponse(httpWebResponse, GetElapsedTimespan(stopwatch));
            }
            catch (WebException webEx)
            {
                // I *think* it's safe to assume that the response will always be a HttpWebResponse...
                HttpWebResponse httpWebResponse = (HttpWebResponse)(webEx.Response);

                if (httpWebResponse == null)
                {
                    //Generate a more helpful error message, hopefully this will save someone else hours in "the abyss"
                    if (webEx.Message.ToLower().Contains("could not create ssl/tls secure channel"))
                    {
                        //Find out what certificates were in the web request
                        var clientCerts = webRequest.ClientCertificates;
                        var certString = "";
                        foreach (var clientCert in clientCerts)
                        {
                            var thumbprint = "unknown";
                            var name = "unknown";

                            var x509Certificate2 = clientCert as X509Certificate2;
                            if (x509Certificate2 != null)
                            {
                                thumbprint = x509Certificate2.Thumbprint;
                                name = x509Certificate2.GetNameInfo(X509NameType.SimpleName, false);
                            }

                            certString += String.Format("[name: \"{0}\", serial: \"{1}\", thumbprint: \"{2}\"] ",
                                                        name,
                                                        clientCert.GetSerialNumberString(),
                                                        thumbprint);
                        }

                        //Try get the username that is running the current process (so we know who to give permissions to)
                        var processUser = System.Security.Principal.WindowsIdentity.GetCurrent();
                        var userId = "your current process";
                        if (processUser != null)
                        {
                            userId = String.Format("\"{0}\"", processUser.Name);
                        }

                        throw new WebException(String.Format("{0} Check that {1} has permission to read the following certificates: {2}", webEx.Message, userId, certString));
                    }

                    throw new ApplicationException("An HttpWebResponse could not be obtained from the WebException. Status was " + webEx.Status, webEx);
                }

                consumerResponse = new ConsumerResponse(httpWebResponse, webEx, GetElapsedTimespan(stopwatch));
            }

            return consumerResponse;
        }
        //DevDefined.OAuth library doesn't support multipart/form-data contentType
        //This method takes
        private void PostMultiPart(IConsumerRequest devDefinedRequest, FileInfo filePath)
        {
            try
            {
                HttpWebRequest request = devDefinedRequest.ToWebRequest();

                var boundary = string.Format("----------{0:N}", Guid.NewGuid());
                var content  = new MemoryStream();
                var writer   = new StreamWriter(content);

                var fs   = new FileStream(filePath.FullName, FileMode.Open, FileAccess.Read);
                var data = new byte[fs.Length];
                fs.Read(data, 0, data.Length);
                fs.Close();

                writer.WriteLine("--{0}", boundary);
                writer.WriteLine("Content-Disposition: form-data; name=\"file\"; filename=\"{0}\"", filePath.Name);
                writer.WriteLine("Content-Type: application/octet-stream");
                writer.WriteLine();
                writer.Flush();

                content.Write(data, 0, data.Length);

                writer.WriteLine();

                writer.WriteLine("--" + boundary + "--");
                writer.Flush();
                content.Seek(0, SeekOrigin.Begin);

                request.ContentType   = string.Format("multipart/form-data; boundary={0}", boundary);
                request.ContentLength = content.Length;

                using (Stream requestStream = request.GetRequestStream())
                {
                    content.WriteTo(requestStream);
                    content.Close();
                    writer.Close();
                }

                using (var response = request.GetResponse());
            }
            catch (WebException wex)
            {
                if (wex.Response != null)
                {
                    using (var errorResponse = wex.Response as HttpWebResponse)
                    {
                        using (var reader = new StreamReader(errorResponse.GetResponseStream()))
                        {
                            ErrorResponse er = JsonConvert.DeserializeObject <ErrorResponse>(reader.ReadToEnd());

                            throw new JiraException(er);
                        }
                    }
                }
                else
                {
                    ErrorResponse er = new ErrorResponse();
                    er.ErrorMessages    = new string[1];
                    er.ErrorMessages[0] = wex.Message;

                    throw new JiraException(er);
                }
            }
        }
        public IConsumerResponse Run(IConsumerRequest consumerRequest)
        {
            HttpWebRequest    webRequest       = consumerRequest.ToWebRequest();
            IConsumerResponse consumerResponse = null;

            var stopwatch = Stopwatch.StartNew();

            try
            {
                var httpWebResponse = webRequest.GetResponse() as HttpWebResponse;
                consumerResponse = new ConsumerResponse(httpWebResponse, GetElapsedTimespan(stopwatch));
            }
            catch (WebException webEx)
            {
                // I *think* it's safe to assume that the response will always be a HttpWebResponse...
                HttpWebResponse httpWebResponse = (HttpWebResponse)(webEx.Response);

                if (httpWebResponse == null)
                {
                    //Generate a more helpful error message, hopefully this will save someone else hours in "the abyss"
                    if (webEx.Message.ToLower().Contains("could not create ssl/tls secure channel"))
                    {
                        //Find out what certificates were in the web request
                        var clientCerts = webRequest.ClientCertificates;
                        var certString  = "";
                        foreach (var clientCert in clientCerts)
                        {
                            var thumbprint = "unknown";
                            var name       = "unknown";

                            var x509Certificate2 = clientCert as X509Certificate2;
                            if (x509Certificate2 != null)
                            {
                                thumbprint = x509Certificate2.Thumbprint;
                                name       = x509Certificate2.GetNameInfo(X509NameType.SimpleName, false);
                            }

                            certString += String.Format("[name: \"{0}\", serial: \"{1}\", thumbprint: \"{2}\"] ",
                                                        name,
                                                        clientCert.GetSerialNumberString(),
                                                        thumbprint);
                        }

                        //Try get the username that is running the current process (so we know who to give permissions to)
                        var processUser = System.Security.Principal.WindowsIdentity.GetCurrent();
                        var userId      = "your current process";
                        if (processUser != null)
                        {
                            userId = String.Format("\"{0}\"", processUser.Name);
                        }

                        throw new WebException(String.Format("{0} Check that {1} has permission to read the following certificates: {2}", webEx.Message, userId, certString));
                    }

                    throw new ApplicationException("An HttpWebResponse could not be obtained from the WebException. Status was " + webEx.Status, webEx);
                }

                consumerResponse = new ConsumerResponse(httpWebResponse, webEx, GetElapsedTimespan(stopwatch));
            }

            return(consumerResponse);
        }