Exemple #1
0
        /// <summary>
        /// Sets a standard content for the request.
        /// </summary>
        /// <param name="postData">The content string</param>
        /// <param name="contentType">The content type</param>
        /// <param name="method">The HTTP method</param>
        /// <param name="encodeContent">Whether to URLencode the content automatically</param>
        /// <param name="log">The log (if any)</param>
        /// <returns>The request itself</returns>
        public Request SetStandardContent(string postData, string contentType,
                                          HttpMethod method = HttpMethod.POST, bool encodeContent = false, List <LogEntry> log = null)
        {
            this.contentType = contentType;
            var pData = Regex.Replace(postData, @"(?<!\\)\\n", Environment.NewLine).Unescape();

            if (HttpRequest.CanContainRequestBody(method))
            {
                if (encodeContent)
                {
                    // Very dirty but it works
                    Random rand  = new Random();
                    var    nonce = rand.Next(1000000, 9999999);
                    pData = pData.Replace("&", $"{nonce}&{nonce}").Replace("=", $"{nonce}={nonce}");
                    pData = string.Join("", BlockFunction.SplitInChunks(pData, 2080)
                                        .Select(s => Uri.EscapeDataString(s)))
                            .Replace($"{nonce}%26{nonce}", "&").Replace($"{nonce}%3D{nonce}", "=");
                }

                content             = new StringContent(pData);
                content.ContentType = contentType;

                if (log != null)
                {
                    log.Add(new LogEntry($"Post Data: {pData}", Colors.MediumTurquoise));
                }
            }

            return(this);
        }