Beispiel #1
0
 public ApiRequestClient(string baseUrl, string apiKey, int?requestTimeoutInSeconds = null)  : base(baseUrl, requestTimeoutInSeconds)
 {
     if (apiKey != null)
     {
         CustomHeaders.Add("apikey", apiKey);
     }
 }
 private void AsJwtToken(string key, ConnectionType type)
 {
     ConnectionType = type;
     CustomHeaders.Add("Authorization", "Bearer " + key);
     _isConfigured   = true;
     IsAuthenticated = true;
 }
Beispiel #3
0
 /// <summary>
 /// Adds an custom header to the MailMessage.
 ///	NOTE: some SMTP servers will reject improper custom headers
 ///</summary>
 /// <param name="mailheader">MailHeader instance</param>
 /// <example>
 /// <code>
 ///     MailMessage msg = new MailMessage("*****@*****.**", "*****@*****.**");
 ///		MailHeader header = new MailHeader("X-Something", "HeaderValue");
 ///		msg.AddCustomHeader(header);
 /// </code>
 /// </example>
 public void AddCustomHeader(MailHeader mailheader)
 {
     if (mailheader.Name != null && mailheader.Body != null)
     {
         CustomHeaders.Add(mailheader);
     }
 }
Beispiel #4
0
        public void AddCustomHeader(string key, string value)
        {
            if (string.IsNullOrWhiteSpace(key))
            {
                return;
            }

            CustomHeaders.Add(key, value);
        }
 public void AsStaticHeaderUserViaProxy(string userId, string headerName, bool certificateValidation)
 {
     ConnectionType        = ConnectionType.StaticHeaderUserViaProxy;
     CertificateValidation = certificateValidation;
     UserId           = userId;
     UserDirectory    = Environment.UserDomainName;
     StaticHeaderName = headerName;
     CustomHeaders.Add(headerName, userId);
     _isConfigured = true;
 }
 public void AsNtlmUserViaProxy(NetworkCredential credential, bool certificateValidation = true)
 {
     ConnectionType        = ConnectionType.NtlmUserViaProxy;
     CertificateValidation = certificateValidation;
     if (credential != null)
     {
         var credentialCache = new CredentialCache();
         credentialCache.Add(this.BaseUri, "ntlm", credential);
         CustomCredential = credentialCache;
     }
     CustomHeaders.Add("User-Agent", "Windows");
     _isConfigured = true;
 }
        public void AsDirectConnection(string userDirectory, string userId, int port    = 4242, bool certificateValidation = true,
                                       X509Certificate2Collection certificateCollection = null)
        {
            ConnectionType = ConnectionType.DirectConnection;
            var uriBuilder = new UriBuilder(BaseUri)
            {
                Port = port
            };

            BaseUri               = uriBuilder.Uri;
            UserId                = userId;
            UserDirectory         = userDirectory;
            CertificateValidation = certificateValidation;
            Certificates          = certificateCollection;
            var userHeaderValue = string.Format("UserDirectory={0};UserId={1}", UserDirectory, UserId);

            CustomHeaders.Add("X-Qlik-User", userHeaderValue);
            _isConfigured = true;
        }
Beispiel #8
0
        /// <summary>
        ///     Parses a single header and sets member variables according to it.
        /// </summary>
        /// <param name="headerName">The name of the header</param>
        /// <param name="headerValue">The value of the header in unfolded state (only one line)</param>
        /// <exception cref="ArgumentNullException">
        ///     If <paramref name="headerName" /> or <paramref name="headerValue" /> is
        ///     <see langword="null" />
        /// </exception>
        private void ParseHeader(string headerName, string headerValue)
        {
            if (headerName == null)
            {
                throw new ArgumentNullException(nameof(headerName));
            }

            if (headerValue == null)
            {
                throw new ArgumentNullException(nameof(headerValue));
            }

            switch (headerName.ToUpperInvariant())
            {
            // See http://tools.ietf.org/html/rfc5322#section-3.6.3
            case "TO":
                To = RfcMailAddress.ParseMailAddresses(headerValue);
                break;

            // See http://tools.ietf.org/html/rfc5322#section-3.6.3
            case "CC":
                Cc = RfcMailAddress.ParseMailAddresses(headerValue);
                break;

            // See http://tools.ietf.org/html/rfc5322#section-3.6.3
            case "BCC":
                Bcc = RfcMailAddress.ParseMailAddresses(headerValue);
                break;

            // See http://tools.ietf.org/html/rfc5322#section-3.6.2
            case "FROM":
                // There is only one MailAddress in the from field
                From = RfcMailAddress.ParseMailAddress(headerValue);
                break;

            // http://tools.ietf.org/html/rfc5322#section-3.6.2
            // The implementation here might be wrong
            case "REPLY-TO":
                // This field may actually be a list of addresses, but no
                // such case has been encountered
                ReplyTo = RfcMailAddress.ParseMailAddress(headerValue);
                break;

            // http://tools.ietf.org/html/rfc5322#section-3.6.2
            case "SENDER":
                Sender = RfcMailAddress.ParseMailAddress(headerValue);
                break;

            // See http://tools.ietf.org/html/rfc5322#section-3.6.5
            // RFC 5322:
            // The "Keywords:" field contains a comma-separated list of one or more
            // words or quoted-strings.
            // The field are intended to have only human-readable content
            // with information about the message
            case "KEYWORDS":
                var keywordsTemp = headerValue.Split(',');
                foreach (var keyword in keywordsTemp)
                {
                    Keywords.Add(Utility.RemoveQuotesIfAny(keyword.Trim()));
                }
                break;

            // See http://tools.ietf.org/html/rfc5322#section-3.6.7
            case "RECEIVED":
                // Simply add the value to the list
                Received.Add(new Received(headerValue.Trim()));
                break;

            case "IMPORTANCE":
                Importance = HeaderFieldParser.ParseImportance(headerValue.Trim());
                break;

            // See http://tools.ietf.org/html/rfc3798#section-2.1
            case "DISPOSITION-NOTIFICATION-TO":
                DispositionNotificationTo = RfcMailAddress.ParseMailAddresses(headerValue);
                break;

            case "MIME-VERSION":
                MimeVersion = headerValue.Trim();
                break;

            // See http://tools.ietf.org/html/rfc5322#section-3.6.5
            case "SUBJECT":
                Subject = EncodedWord.Decode(headerValue);
                break;

            // See http://tools.ietf.org/html/rfc5322#section-3.6.7
            case "RETURN-PATH":
                // Return-paths does not include a username, but we
                // may still use the address parser
                ReturnPath = RfcMailAddress.ParseMailAddress(headerValue);
                break;

            // See http://tools.ietf.org/html/rfc5322#section-3.6.4
            // Example Message-ID
            // <*****@*****.**>
            case "MESSAGE-ID":
                MessageId = HeaderFieldParser.ParseId(headerValue);
                break;

            // See http://tools.ietf.org/html/rfc5322#section-3.6.4
            case "IN-REPLY-TO":
                InReplyTo = HeaderFieldParser.ParseMultipleIDs(headerValue);
                break;

            // See http://tools.ietf.org/html/rfc5322#section-3.6.4
            case "REFERENCES":
                References = HeaderFieldParser.ParseMultipleIDs(headerValue);
                break;

            // See http://tools.ietf.org/html/rfc5322#section-3.6.1))
            case "DATE":
                Date     = headerValue.Trim();
                DateSent = Rfc2822DateTime.StringToDate(headerValue);
                break;

            // See http://tools.ietf.org/html/rfc2045#section-6
            // See ContentTransferEncoding class for more details
            case "CONTENT-TRANSFER-ENCODING":
                ContentTransferEncoding = HeaderFieldParser.ParseContentTransferEncoding(headerValue.Trim());
                break;

            // See http://tools.ietf.org/html/rfc2045#section-8
            case "CONTENT-DESCRIPTION":
                // Human description of for example a file. Can be encoded
                ContentDescription = EncodedWord.Decode(headerValue.Trim());
                break;

            // See http://tools.ietf.org/html/rfc2045#section-5.1
            // Example: Content-type: text/plain; charset="us-ascii"
            case "CONTENT-TYPE":
                ContentType = HeaderFieldParser.ParseContentType(headerValue);
                break;

            // See http://tools.ietf.org/html/rfc2183
            case "CONTENT-DISPOSITION":
                ContentDisposition = HeaderFieldParser.ParseContentDisposition(headerValue);
                break;

            // See http://tools.ietf.org/html/rfc2045#section-7
            // Example: <foo4*[email protected]>
            case "CONTENT-ID":
                ContentId = HeaderFieldParser.ParseId(headerValue);
                break;

            default:
                // This is an unknown header

                // Custom headers are allowed. That means headers
                // that are not mentionen in the RFC.
                // Such headers start with the letter "X"
                // We do not have any special parsing of such

                // Add it to custom headers
                if (CustomHeaders.ContainsKey(headerName))
                {
                    CustomHeaders[headerName].Add(headerValue);
                }
                else
                {
                    CustomHeaders.Add(headerName, new List <string> {
                        headerValue
                    });
                }

                break;
            }
        }
Beispiel #9
0
        public HttpRequest WithHeader(string name, object value)
        {
            CustomHeaders.Add(name, value);

            return(this);
        }
        public async Task StartDownload()
        {
            try
            {
                ProgressBar.Tag       = this;
                ProgressLabel.Text    = "";
                ProgressLabel.Visible = true;
                Directory.CreateDirectory(Path.GetDirectoryName(SavePath));
                FileInfo     file          = new FileInfo(SavePath);
                DialogResult overwriteFile = DialogResult.Yes;
                if (file.Exists)
                {
                    switch (ParentDownload.OverwriteMode)
                    {
                    case 0:
                        overwriteFile = DialogResult.No;
                        break;

                    case 1:
                        overwriteFile = DialogResult.Yes;
                        break;

                    case 2:
                        if (FileInfo.Modified > file.CreationTime)
                        {
                            overwriteFile = DialogResult.Yes;
                        }
                        else
                        {
                            overwriteFile = DialogResult.No;
                        }
                        break;

                    case 3:
                        overwriteFile = MessageBox.Show($"File [{file.Name}] already exists. Overwrite?", "", MessageBoxButtons.YesNo);
                        break;
                    }
                    if (overwriteFile == DialogResult.Yes)
                    {
                        file.Delete();
                    }
                }

                if (overwriteFile == DialogResult.Yes)
                {
                    CustomHeaders.Add("X-Requested-With", "XMLHttpRequest");
                    string downloadPath = EncodeWebUrl(FileInfo.PublicUrl.OriginalString);
                    if (ParentDownload.CloudService == CloudServiceType.Allsync)
                    {
                        var encodedUrl = new Uri(downloadPath);
                        var host       = encodedUrl.Host;
                        downloadPath = $"https://{host}/public.php/webdav{EncodeAllsyncUrl(FileInfo.Path)}";
                    }

                    //var logFileName = $"download-log-{DateTime.Now.ToString("MM-dd-yyyy")}.txt";
                    //string log = $"{DateTime.Now}\ndownloadPath: {downloadPath}\nSavePath: {SavePath}\n";
                    //File.AppendAllText(logFileName, log);

                    DownloadTask = DownloadFileAsync(downloadPath, SavePath, Progress, ParentDownload.cancellationTokenSource.Token, _networkCredential);
                    await DownloadTask;
                }
                ParentDownload.UpdateQueue(this);
            }
            catch (Exception ex)
            {
                if (DownloadTask.IsCanceled)
                {
                    DownloadTask.Dispose();
                }
                //MessageBox.Show(ex.Message);
            }
        }