Exemplo n.º 1
0
        public static int HttpRequest(Uri url, string token = null, ApiList postData = null, HttpMethod httpMethod = HttpMethod.GET, bool ignoreServerCert = false)
        {
            Logger.LogEnter(LoggerBase.Args(url, token, postData == null ? "null" : postData.ToFormData(), httpMethod));

            int returnCode = 0;

            if (ignoreServerCert)
            {
                InitiateSSLTrust();
            }

            WebResponse  response   = null;
            StreamReader reader     = null;
            Stream       dataStream = null;

            try
            {
                // Create a request using a URL that can receive a post.
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);

                request.Method = httpMethod.ToString();

                if (!string.IsNullOrEmpty(token))
                {
                    request.Headers.Add("Authorization", token);
                }

                if (postData != null)
                {
                    // Set the Method property of the request to POST.
                    request.Method = HttpMethod.POST.ToString();

                    // Create POST data and convert it to a byte array.
                    //string postData = "This is a test that posts this string to a Web server.";
                    byte[] byteArray = Encoding.UTF8.GetBytes(postData.ToFormData());

                    // Set the ContentType property of the WebRequest.
                    request.ContentType = "application/x-www-form-urlencoded";

                    // Set the ContentLength property of the WebRequest.
                    request.ContentLength = byteArray.Length;

                    // Get the request stream.
                    dataStream = request.GetRequestStream();

                    // Write the data to the request stream.
                    dataStream.Write(byteArray, 0, byteArray.Length);

                    // Close the Stream object.
                    dataStream.Close();
                }

                // Get the response.
                response = request.GetResponse();

                // Display the status.
                Logger.LogDebug("Response Code:: {0} - {1}", ((HttpWebResponse)response).StatusDescription, (int)(((HttpWebResponse)response).StatusCode));

                returnCode = (int)(((HttpWebResponse)response).StatusCode);

                // Get the stream containing content returned by the server.
                dataStream = response.GetResponseStream();

                // Open the stream using a StreamReader for easy access.
                reader = new StreamReader(dataStream);

                // Read the content.
                string responseFromServer = reader.ReadToEnd();

                // Display the content.
                Logger.LogDebug("Response Data :: {0}", responseFromServer);
            }
            catch (WebException e)
            {
                if (e.Status == WebExceptionStatus.ProtocolError)
                {
                    response = (HttpWebResponse)e.Response;

                    returnCode = (int)(((HttpWebResponse)response).StatusCode);

                    Logger.LogWarning("Error Code: {0} - {1}", returnCode, (((HttpWebResponse)response).StatusDescription));

                    // Get the stream containing content returned by the server.
                    dataStream = response.GetResponseStream();

                    // Open the stream using a StreamReader for easy access.
                    reader = new StreamReader(dataStream);

                    // Read the content.
                    string responseFromServer = reader.ReadToEnd();

                    // Display the content.
                    Logger.LogDebug("Response Data :: {0}", responseFromServer);
                }
                else
                {
                    Logger.LogError("Error: {0}", e.Status);
                    Logger.LogError("Error: {0}", e.ToString());
                }
            }
            finally
            {
                // Clean up the streams and the response.
                if (response != null)
                {
                    response.Close();
                }
                if (reader != null)
                {
                    reader.Close();
                }
                if (dataStream != null)
                {
                    dataStream.Close();
                }
            }

            Logger.LogExit(LoggerBase.Args(returnCode));
            return(returnCode);
        }
Exemplo n.º 2
0
        public static String Token(
            string realm
            , string authPrefix
            , HttpMethod httpMethod
            , Uri urlPath
            , string appId
            , string secret    = null
            , ApiList formList = null
            , RSA privateKey   = null
            , string nonce     = null
            , string timestamp = null
            , string version   = "1.0")
        {
            Logger.LogEnter(LoggerBase.Args(realm, authPrefix, httpMethod, urlPath, appId, secret, formList == null ? null : formList.ToFormData(), privateKey, nonce, timestamp, version));

            Logger.LogDebug("URL:: {0}", urlPath);

            authPrefix = authPrefix.ToLower();

            // Generate the nonce value
            nonce     = nonce ?? ApiAuthorization.NewNonce().ToString();
            timestamp = timestamp ?? ApiAuthorization.NewTimestamp().ToString();

            SignatureMethod signatureMethod = SignatureMethod.HMACSHA256;

            if (secret == null)
            {
                signatureMethod = SignatureMethod.SHA256withRSA;
            }

            String baseString = BaseString(authPrefix, signatureMethod
                                           , appId, urlPath, httpMethod
                                           , formList, nonce, timestamp, version);

            String base64Token = "";

            if (secret != null)
            {
                base64Token = baseString.L1Signature(secret);
            }
            else
            {
                base64Token = baseString.L2Signature(privateKey);
            }

            var tokenList = new ApiList();

            tokenList.Add("realm", realm);
            tokenList.Add(authPrefix + "_timestamp", timestamp);
            tokenList.Add(authPrefix + "_nonce", nonce);
            tokenList.Add(authPrefix + "_app_id", appId);
            tokenList.Add(authPrefix + "_signature_method", signatureMethod.ToString());
            tokenList.Add(authPrefix + "_version", version);
            tokenList.Add(authPrefix + "_signature", base64Token);

            string authorizationToken = string.Format("{0} {1}", authPrefix.Substring(0, 1).ToUpperInvariant() + authPrefix.Substring(1), tokenList.ToString(",", false, true));

            Logger.LogDebug("Token :: {0}", authorizationToken);

            Logger.LogExit(LoggerBase.Args(authorizationToken));
            return(authorizationToken);
        }