private HttpResponseMessage getConnection(string urlFragment, string method, string body)
        {
            try
            {
                SignalConnectionInformation connectionInformation = getRandom(signalConnectionInformation);
                string       url        = connectionInformation.getUrl();
                May <string> hostHeader = connectionInformation.getHostHeader();
                Uri          uri        = new Uri(string.Format("{0}{1}", url, urlFragment));
                Debug.WriteLine("{0}: Uri {1}", TAG, uri);
                HttpClient connection = new HttpClient();

                var headers = connection.DefaultRequestHeaders;

                if (credentialsProvider.GetPassword() != null)
                {
                    string authHeader = getAuthorizationHeader();
                    Debug.WriteLine(String.Format("Authorization: {0}", authHeader), TAG);
                    headers.Add("Authorization", authHeader);
                }

                if (userAgent != null)
                {
                    headers.Add("X-Signal-Agent", userAgent);
                }

                if (hostHeader.HasValue)
                {
                    headers.Host = hostHeader.ForceGetValue();
                }

                StringContent content;
                if (body != null)
                {
                    content = new StringContent(body, Encoding.UTF8, "application/json");
                    Debug.WriteLine(body);
                }
                else
                {
                    content = new StringContent("");
                }
                switch (method)
                {
                case "POST":
                    return(connection.PostAsync(uri, content).Result);

                case "PUT":
                    return(connection.PutAsync(uri, content).Result);

                case "DELETE":
                    return(connection.DeleteAsync(uri).Result);

                case "GET":
                    return(connection.GetAsync(uri).Result);

                default:
                    throw new Exception("Unknown method: " + method);
                }
            }
            catch (Exception e)
            {
                Debug.WriteLine("getConnection() failed:", TAG);
                Debug.WriteLine(e.Message);
                Debug.WriteLine(e.StackTrace);
                throw new PushNetworkException(e);
            }
        }
Beispiel #2
0
        private async Task <HttpResponseMessage> getConnection(string urlFragment, string method, string body)
        {
            try
            {
                SignalConnectionInformation connectionInformation = getRandom(signalConnectionInformation);
                string       url        = connectionInformation.getUrl();
                May <string> hostHeader = connectionInformation.getHostHeader();
                //TrustManger[] trustManagers = connectionInformation.getTrustManagers();

                /*SSLContext context = SSLContext.getInstance("TLS");
                 * context.init(null, trustManagers, null);*/

                Uri uri = new Uri(string.Format("{0}{1}", url, urlFragment));
                //Log.w(TAG, "Push service URL: " + serviceUrl);
                //Log.w(TAG, "Opening URL: " + url);
                var filter = new HttpBaseProtocolFilter();

                if (HttpClientCertificatePolicyState.Policy == HttpClientCertificatePolicy.DevelopmentMode)
                {
                    filter.IgnorableServerCertificateErrors.Add(Windows.Security.Cryptography.Certificates.ChainValidationResult.Expired);
                    filter.IgnorableServerCertificateErrors.Add(Windows.Security.Cryptography.Certificates.ChainValidationResult.Untrusted);
                    filter.IgnorableServerCertificateErrors.Add(Windows.Security.Cryptography.Certificates.ChainValidationResult.InvalidName);
                }

                //HttpURLConnection connection = (HttpURLConnection)url.openConnection();

                HttpClient connection = new HttpClient(filter);

                /*if (ENFORCE_SSL)
                 * {
                 *  ((HttpsURLConnection)connection).setSSLSocketFactory(context.getSocketFactory());
                 *  ((HttpsURLConnection)connection).setHostnameVerifier(new StrictHostnameVerifier());
                 * }*/

                var headers = connection.DefaultRequestHeaders;

                //connection.setRequestMethod(method);
                //headers.Add("Content-Type", "application/json");

                if (credentialsProvider.GetPassword() != null)
                {
                    headers.Add("Authorization", getAuthorizationHeader());
                }

                if (userAgent != null)
                {
                    headers.Add("X-Signal-Agent", userAgent);
                }

                if (hostHeader.HasValue)
                {
                    headers.Host = new HostName(hostHeader.ForceGetValue());
                }

                /*if (body != null)
                 * {
                 *  connection.setDoOutput(true);
                 * }*/

                //connection.connect();

                HttpStringContent content;
                if (body != null)
                {
                    content = new HttpStringContent(body, Windows.Storage.Streams.UnicodeEncoding.Utf8, "application/json");
                    Debug.WriteLine(body);
                }
                else
                {
                    content = new HttpStringContent("");
                }
                switch (method)
                {
                case "POST":
                    return(await connection.PostAsync(uri, content));

                case "PUT":
                    return(await connection.PutAsync(uri, content));

                case "DELETE":
                    return(await connection.DeleteAsync(uri));

                case "GET":
                    return(await connection.GetAsync(uri));

                default:
                    throw new Exception("Unknown method: " + method);
                }
            }
            catch (UriFormatException e)
            {
                throw new Exception(string.Format("Uri {0} {1} is wrong", "", urlFragment));
            }
            catch (Exception e)
            {
                Debug.WriteLine(string.Format("Other exception {0}{1} is wrong", "", urlFragment));
                throw new PushNetworkException(e);
            }
        }