예제 #1
0
        public HttpTask CreateTask(
            HttpRequestMessage request,
            Action <HttpTask, HttpResponseMessage> onComplete = null,
            Action <HttpTask, Exception> onError                = null,
            Action <HttpTask> onCancel                          = null,
            Action <HttpTask, long, long> onDownloadProgress    = null,
            Action <HttpTask, long, long> onUploadProgress      = null,
            Action <HttpTask, HttpTask.TaskState> onStateChange = null,
            bool disposeRequest  = true,
            bool disposeResponse = true
            )
        {
            if (request == null)
            {
                throw new ArgumentNullException(nameof(request));
            }
            if (string.IsNullOrWhiteSpace(request.UserAgent))
            {
                request.UserAgent = DefaultUserAgent;
            }
            if (!request.HasHeader(HttpAcceptEncodingHeader.NAME))
            {
                request.AcceptedEncodings = AcceptedEncodings;
            }
            request.Connection = ReuseConnections ? HttpConnectionHeader.KEEP_ALIVE : HttpConnectionHeader.CLOSE;

            HttpTask task = new HttpTask(Connections, request, disposeRequest, disposeResponse);

            if (onComplete != null)
            {
                task.OnComplete += onComplete;
            }
            if (onError != null)
            {
                task.OnError += onError;
            }
            if (onCancel != null)
            {
                task.OnCancel += onCancel;
            }
            if (onDownloadProgress != null)
            {
                task.OnDownloadProgress += onDownloadProgress;
            }
            if (onUploadProgress != null)
            {
                task.OnUploadProgress += onUploadProgress;
            }
            if (onStateChange != null)
            {
                task.OnStateChange += onStateChange;
            }

            return(task);
        }
예제 #2
0
 public void RunTask(HttpTask task)
 {
     if (task == null)
     {
         throw new ArgumentNullException(nameof(task));
     }
     if (!Lock.WaitOne())
     {
         throw new HttpTaskManagerLockException();
     }
     new Thread(() => {
         try {
             task.Run();
         } finally {
             Lock?.Release();
         }
     }).Start();
 }
예제 #3
0
 public void RunTask(HttpTask task)
 {
     Tasks.RunTask(task);
 }