Beispiel #1
0
        public static void UsingByteArrayContent()
        {
            // ByteArrayContent is hardly different than StringContent,
            // but it can be more convenient for lower-level operations
            // when bytes are already available (as opposed to using a Stream).
            var bytes = Encoding.ASCII.GetBytes(Helpers.__);
            var content = new ByteArrayContent(bytes); // This also takes offset and limit parameters.

            var body = content.ReadAsStringAsync().Result;

            Helpers.AssertEquality("Hello, bytes!", body);
        }
Beispiel #2
0
        public void ReadAsMultipartAsyncNestedMultipartContentAsync(string boundary)
        {
            const int    nesting   = 10;
            const string innerText = "Content";

            MultipartContent innerContent = new MultipartContent("mixed", boundary);

            innerContent.Add(new StringContent(innerText));
            for (var cnt = 0; cnt < nesting; cnt++)
            {
                string           outerBoundary = String.Format("{0}_{1}", boundary, cnt);
                MultipartContent outerContent  = new MultipartContent("mixed", outerBoundary);
                outerContent.Add(innerContent);
                innerContent = outerContent;
            }

            MemoryStream memStream = new MemoryStream();

            innerContent.CopyToAsync(memStream).Wait();
            memStream.Position = 0;
            byte[]      data    = memStream.ToArray();
            HttpContent content = new ByteArrayContent(data);

            content.Headers.ContentType = innerContent.Headers.ContentType;

            for (var cnt = 0; cnt < nesting + 1; cnt++)
            {
                Task <IEnumerable <HttpContent> > task = content.ReadAsMultipartAsync();
                task.Wait(TimeoutConstant.DefaultTimeout);
                IEnumerable <HttpContent> result = task.Result;
                Assert.Equal(1, result.Count());
                content = result.ElementAt(0);
                Assert.NotNull(content);
            }

            string text = content.ReadAsStringAsync().Result;

            Assert.Equal(innerText, text);
        }
Beispiel #3
0
        public async Task ReadAsMultipartAsync_NestedMultipartContent(string boundary)
        {
            const int    nesting   = 10;
            const string innerText = "Content";

            MultipartContent innerContent = new MultipartContent("mixed", boundary);

            innerContent.Add(new StringContent(innerText));
            for (var cnt = 0; cnt < nesting; cnt++)
            {
                string           outerBoundary = String.Format("{0}_{1}", boundary, cnt);
                MultipartContent outerContent  = new MultipartContent("mixed", outerBoundary);
                outerContent.Add(innerContent);
                innerContent = outerContent;
            }

            MemoryStream memStream = new MemoryStream();
            await innerContent.CopyToAsync(memStream);

            memStream.Position = 0;
            byte[]      data    = memStream.ToArray();
            HttpContent content = new ByteArrayContent(data);

            content.Headers.ContentType = innerContent.Headers.ContentType;

            for (var cnt = 0; cnt < nesting + 1; cnt++)
            {
                MultipartMemoryStreamProvider result = await content.ReadAsMultipartAsync();

                Assert.Equal(1, result.Contents.Count);
                content = result.Contents[0];
                Assert.NotNull(content);
            }

            string text = await content.ReadAsStringAsync();

            Assert.Equal(innerText, text);
        }
        public void ReadAsMultipartAsync_NestedMultipartContent(string boundary)
        {
            const int nesting = 10;
            const string innerText = "Content";

            MultipartContent innerContent = new MultipartContent("mixed", boundary);
            innerContent.Add(new StringContent(innerText));
            for (var cnt = 0; cnt < nesting; cnt++)
            {
                string outerBoundary = String.Format("{0}_{1}", boundary, cnt);
                MultipartContent outerContent = new MultipartContent("mixed", outerBoundary);
                outerContent.Add(innerContent);
                innerContent = outerContent;
            }

            MemoryStream memStream = new MemoryStream();
            innerContent.CopyToAsync(memStream).Wait();
            memStream.Position = 0;
            byte[] data = memStream.ToArray();
            HttpContent content = new ByteArrayContent(data);
            content.Headers.ContentType = innerContent.Headers.ContentType;

            for (var cnt = 0; cnt < nesting + 1; cnt++)
            {
                MultipartMemoryStreamProvider result = content.ReadAsMultipartAsync().Result;
                Assert.Equal(1, result.Contents.Count);
                content = result.Contents[0];
                Assert.NotNull(content);
            }

            string text = content.ReadAsStringAsync().Result;
            Assert.Equal(innerText, text);
        }
        public void ReadAsStringAsync()
        {
            byte[] b = { 77, 55 };

            var sc = new ByteArrayContent (b);
            var res = sc.ReadAsStringAsync ().Result;
            Assert.AreEqual ("M7", res, "#1");
        }
        public HttpRequestMessage Decrypt(HttpRequestMessage cipherRequest)
        {
            if (cipherRequest == null)
            {
                throw new ArgumentNullException("cipherRequest");
            }

            var cipherContent = cipherRequest.Content;
            byte[] plain;
            try
            {
                if (cipherContent == null)
                {
                    throw new HttpContentNullException();
                }

                var cipherModel = cipherContent.ReadAsAsync<JObject>().Result;

                string timestamp;
                byte[] cipher;
                string signature;
                this.ParseCipherModel(cipherModel, out timestamp, out cipher, out signature);
                this.timestampProvider.Validate(timestamp);
                this.Decrypt(cipher, timestamp, signature, out plain);

                this.lastTimestamp = timestamp; // keep timestamp for decrypting response.
            }
            catch (Exception ex)
            {
                throw new InvalidHttpMessageException(ex);
            }

            if (DefaultHttpMessageCryptoService.IsEmptyContent(plain))
            {
                Trace.TraceWarning("Content of request is ALTERNATIVES, replace to NULL!");
                cipherRequest.Content = null;
            }
            else
            {
                var decryptedContent = new ByteArrayContent(plain);
                Trace.TraceInformation("Decrypted request content: {0}", decryptedContent.ReadAsStringAsync().Result);

                cipherRequest.Content = decryptedContent;
                cipherRequest.Content.Headers.ContentType = cipherContent.Headers.ContentType;
            }

            return cipherRequest;
        }
        public void ReadAsMultipartAsyncNestedMultipartContentAsync(string boundary)
        {
            const int nesting = 10;
            const string innerText = "Content";

            MultipartContent innerContent = new MultipartContent("mixed", boundary);
            innerContent.Add(new StringContent(innerText));
            for (var cnt = 0; cnt < nesting; cnt++)
            {
                string outerBoundary = String.Format("{0}_{1}", boundary, cnt);
                MultipartContent outerContent = new MultipartContent("mixed", outerBoundary);
                outerContent.Add(innerContent);
                innerContent = outerContent;
            }

            MemoryStream memStream = new MemoryStream();
            innerContent.CopyToAsync(memStream).Wait();
            memStream.Position = 0;
            byte[] data = memStream.ToArray();
            HttpContent content = new ByteArrayContent(data);
            content.Headers.ContentType = innerContent.Headers.ContentType;

            for (var cnt = 0; cnt < nesting + 1; cnt++)
            {
                Task<IEnumerable<HttpContent>> task = content.ReadAsMultipartAsync();
                task.Wait(TimeoutConstant.DefaultTimeout);
                IEnumerable<HttpContent> result = task.Result;
                Assert.Equal(1, result.Count());
                content = result.ElementAt(0);
                Assert.NotNull(content);
            }

            string text = content.ReadAsStringAsync().Result;
            Assert.Equal(innerText, text);
        }