예제 #1
0
        /// <inheritdoc />
        protected override void Execute(Context context)
        {
            var url  = Url.Get(context);
            var json = Json.Get(context);

            using (var client = new ExtendedWebClient())
            {
                var response = client.UploadString(url, json ?? string.Empty);
                Response.Set(context, response);
            }
        }
예제 #2
0
        /// <summary>
        /// Gets the authentication token.
        /// </summary>
        /// <returns></returns>
        public override String getAuthenticationToken()
        {
            String authToken = getToken();
            JavaScriptSerializer serializer = new JavaScriptSerializer();

            Debug.WriteLine("It seems token is not generated yet, Going for OAuth mechanism");
            // Token is not generated yet
            if (authToken == null && tokenRegeneration)
            {
                try{
                    UrlMaker maker = UrlMaker.getInstance();
                    String   url   = maker.getAbsoluteUrl(oAuthUrl);

                    String authHeader = Constants.BASIC + Convert.ToBase64String(Encoding.UTF8.GetBytes(consumerKey + ":" + consumerSecretKey));
                    Uri    uri        = new Uri(url);

                    using (ExtendedWebClient webClient = new ExtendedWebClient())
                    {
                        NameValueCollection headers = new NameValueCollection();

                        headers.Add(Constants.AUTH_HEADER, authHeader);
                        webClient.Headers.Add(headers);

                        string jsonResponse = webClient.UploadString(url, Constants.GRANT_TYPE + "=" + Constants.CLIENT_CREDENTIALS);

                        OAuthServiceResponse oAuthServiceResponse = serializer.Deserialize <OAuthServiceResponse>(jsonResponse);
                        authToken = Constants.BEARER + oAuthServiceResponse.access_token;
                    }
                }

                catch (Exception e) {
                    Debug.WriteLine("Unexpected Exception while generating token, so Invalidating the Authentication Token" + e);
                    invalidateIdentifyServiceManagerInstance();
                    throw new SdkException(new SdkInternalError(e.Message));
                }
            }

            return(authToken);
        }
예제 #3
0
    public void GetChannelUpdates(ref LongbowInstanceData Data, string parameters)
    {
        bool success = false;

        try {
            ExtendedWebClient fetch = new ExtendedWebClient();
            fetch.Timeout     = 4000;
            fetch.CachePolicy = new RequestCachePolicy(RequestCacheLevel.NoCacheNoStore);
            fetch.Headers[HttpRequestHeader.ContentType] = "application/x-www-form-urlencoded";
            string         NewData = fetch.UploadString(LongbowCore.api_url, parameters);
            LongbowToolkit Toolkit = new LongbowToolkit();
            Toolkit.AddNewPosts(ref Data, NewData);
            success = true;
        } catch (WebException e) {
            Data.ConnectionErrorCount++;
            if (Data.ConnectionErrorCount > 3)
            {
                Console.ForegroundColor = ConsoleColor.White;
                Console.BackgroundColor = ConsoleColor.Red;
                Console.SetCursorPosition(0, Console.CursorTop);
                Console.WriteLine("# It appears your connection to the server has been interrupted. (Tried to load post data.)");
                Console.WriteLine("Exception Message: " + e.Status);
                if (e.Status == WebExceptionStatus.ProtocolError)
                {
                    Console.WriteLine("Status Code : {0}", ((HttpWebResponse)e.Response).StatusCode);
                    Console.WriteLine("Status Description : {0}", ((HttpWebResponse)e.Response).StatusDescription);
                }
                Console.ResetColor();
                Console.Write("\n> ");
                Console.SetCursorPosition(2, Console.CursorTop);
            }
        }

        if (success)
        {
            Data.ConnectionErrorCount = 0;
        }
    }
예제 #4
0
        /// <summary>
        /// Processes the API Request Object.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="url">The URL.</param>
        /// <param name="inputaddress">inputaddress String</param>
        /// <returns>string</returns>
        public static string processAPIRequestInternal <T>(String url, String inputaddress)
        {
            String       endPoint          = String.Empty;
            String       contentTypeString = String.Empty;
            SdkException exception         = null;

            JavaScriptSerializer serializer = new JavaScriptSerializer();

            try
            {
                String accessToken = OAuthFactory.getOAuthService().getAuthenticationToken();
                //Add xml API fragment string to complete the endpoint for xml input
                endPoint          = url + Constants.API_FRAGMENT_JSON;
                contentTypeString = "application/json;charset=utf-8";

                Uri uri = new Uri(endPoint);
                using (ExtendedWebClient client = new ExtendedWebClient())
                {
                    client.Headers.Add(HttpRequestHeader.ContentType, contentTypeString);
                    client.Headers.Add(Constants.AUTH_HEADER, accessToken);
                    client.Headers.Add(Constants.USER_AGENT, "CSharp-SDK");
                    String resp = (client.UploadString(uri, inputaddress));
                    return(resp);
                }
            }
            catch (WebException webException)
            {
                Debug.WriteLine("Got an error response from API" + webException);
                string responseText = string.Empty;
                int    statusCode   = 0;

                if (webException.Response != null)
                {
                    var responseStream = webException.Response.GetResponseStream();
                    statusCode = (int)((HttpWebResponse)webException.Response).StatusCode;

                    using (var reader = new StreamReader(responseStream))
                    {
                        responseText = reader.ReadToEnd();
                    }
                    try
                    {
                        ErrorInfo apiError;
                        apiError = serializer.Deserialize <ErrorInfo>(responseText);
                        if (apiError != null)
                        {
                            apiError.HttpStatusCode = statusCode;
                            apiError.Reason         = webException.Message;
                            apiError.Response       = responseText;
                        }
                        exception = new SdkException(apiError);
                    }
                    catch (Exception e)
                    {
                        Debug.WriteLine("Unexpected Error: " + e);
                        exception = new SdkException(new SdkInternalError(Constants.ERROR_MSG_API_PROCESSING, e));
                    }
                }
                else
                {
                    Debug.WriteLine("Unexpected Error: " + webException);
                    exception = new SdkException(new SdkInternalError(Constants.ERROR_MSG_API_PROCESSING, webException));
                }
            }
            catch (Exception e)
            {
                Debug.WriteLine("Unexpected Error: " + e);
                exception = new SdkException(new SdkInternalError(Constants.ERROR_MSG_API_PROCESSING, e));
            }

            throw exception;
        }