예제 #1
0
        private HttpWebResponse SendRequestImpl(MethodType type, string url, IEnumerable <KeyValuePair <string, object> > parameters, ConnectionOptions options)
        {
            try
            {
                var prmArray = InternalUtils.FormatParameters(parameters);
                var uri      = CreateUri(type, url, prmArray);

                if (type == MethodType.Post && ContainsBinaryData(prmArray))
                {
                    return(Request.HttpPostWithMultipartFormData(uri, prmArray,
                                                                 CreateAuthorizationHeader(type, uri, null), options));
                }

                return(type == MethodType.Post
                    ? Request.HttpPost(uri, prmArray, CreateAuthorizationHeader(type, uri, prmArray), options)
                    : Request.HttpNoBody(type, uri, CreateAuthorizationHeader(type, uri, null), options));
            }
            catch (WebException ex)
            {
                var tex = TwitterException.Create(ex);
                if (tex != null)
                {
                    throw tex;
                }
                throw;
            }
        }
예제 #2
0
        private Task <AsyncResponse> SendRequestAsyncImpl(MethodType type, string url, IEnumerable <KeyValuePair <string, object> > parameters, ConnectionOptions options, CancellationToken cancellationToken, IProgress <UploadProgressInfo> progress = null)
        {
            if (cancellationToken.IsCancellationRequested)
            {
                var tcs = new TaskCompletionSource <AsyncResponse>();
                tcs.SetCanceled();
                return(tcs.Task);
            }

            var prmArray = InternalUtils.FormatParameters(parameters);
            var uri      = CreateUri(type, url, prmArray);

            Task <AsyncResponse> task;

            switch (type)
            {
            case MethodType.Get:
                task = Request.HttpGetAsync(
                    uri,
                    CreateAuthorizationHeader(type, uri, null),
                    options,
                    cancellationToken
                    );
                break;

            case MethodType.Post:
                task = ContainsBinaryData(prmArray)
                        ? Request.HttpPostWithMultipartFormDataAsync(
                    uri,
                    prmArray,
                    CreateAuthorizationHeader(type, uri, null),
                    options,
                    cancellationToken,
                    progress
                    )
                        : Request.HttpPostAsync(
                    uri,
                    prmArray,
                    CreateAuthorizationHeader(type, uri, prmArray),
                    options,
                    cancellationToken,
                    progress
                    );
                break;

            case MethodType.Delete:
                task = Request.HttpDeleteAsync(
                    uri,
                    CreateAuthorizationHeader(type, uri, null),
                    options,
                    cancellationToken
                    );
                break;

            default:
                throw new ArgumentOutOfRangeException(nameof(type));
            }

            return(task.ResponseCallback(cancellationToken));
        }