Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7440395
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T10:50:42+00:00 2026-05-29T10:50:42+00:00

Using HttpWebRequest, I can call XDocument.Save() to write directly to the request stream: XDocument

  • 0

Using HttpWebRequest, I can call XDocument.Save() to write directly to the request stream:

XDocument doc = ...;
var request = (HttpWebRequest)WebCreate.Create(uri);
request.method = "POST";
Stream requestStream = request.GetRequestStream();
doc.Save(requestStream);

Is it possible to do the same thing with HttpClient? The straight-forward way is

XDocument doc = ...;
Stream stream = new MemoryStream();
doc.Save(stream);
var content = new System.Net.Http.StreamContent(stream);
var client = new HttpClient();
client.Post(uri, content);

But this creates another copy of the XDocument in the MemoryStream.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T10:50:43+00:00Added an answer on May 29, 2026 at 10:50 am

    XDocument.Save() expects a Stream that can be written to. StreamContent expects a stream that can be read. So, you can use a two Streams, where one acts as as a forwarder for the other one. I don’t think such type exists in the framework, but you can write one yourself:

    class ForwardingStream
    {
        private readonly ReaderStream m_reader;
        private readonly WriterStream m_writer;
    
        public ForwardingStream()
        {
            // bounded, so that writing too much data blocks
            var buffers = new BlockingCollection<byte[]>(10);
            m_reader = new ReaderStream(buffers);
            m_writer = new WriterStream(buffers);
        }
    
        private class ReaderStream : Stream
        {
            private readonly BlockingCollection<byte[]> m_buffers;
            private byte[] m_currentBuffer;
            private int m_readFromCurrent;
    
            public ReaderStream(BlockingCollection<byte[]> buffers)
            {
                m_buffers = buffers;
            }
    
            public override void Flush()
            {}
    
            public override long Seek(long offset, SeekOrigin origin)
            {
                throw new NotSupportedException();
            }
    
            public override void SetLength(long value)
            {
                throw new NotSupportedException();
            }
    
            public override int Read(byte[] buffer, int offset, int count)
            {
                if (m_currentBuffer == null)
                {
                    if (!m_buffers.TryTake(out m_currentBuffer, -1))
                    {
                        return 0;
                    }
                    m_readFromCurrent = 0;
                }
    
                int toRead = Math.Min(count, m_currentBuffer.Length - m_readFromCurrent);
    
                Array.Copy(m_currentBuffer, m_readFromCurrent, buffer, offset, toRead);
    
                m_readFromCurrent += toRead;
    
                if (m_readFromCurrent == m_currentBuffer.Length)
                    m_currentBuffer = null;
    
                return toRead;
            }
    
            public override void Write(byte[] buffer, int offset, int count)
            {
                throw new NotSupportedException();
            }
    
            public override bool CanRead
            {
                get { return true; }
            }
    
            public override bool CanSeek
            {
                get { return false; }
            }
    
            public override bool CanWrite
            {
                get { return false; }
            }
    
            public override long Length
            {
                get { throw new NotSupportedException(); }
            }
    
            public override long Position
            {
                get { throw new NotSupportedException(); }
                set { throw new NotSupportedException(); }
            }
        }
    
        private class WriterStream : Stream
        {
            private readonly BlockingCollection<byte[]> m_buffers;
    
            public WriterStream(BlockingCollection<byte[]> buffers)
            {
                m_buffers = buffers;
            }
    
            public override void Flush()
            {}
    
            public override long Seek(long offset, SeekOrigin origin)
            {
                throw new NotSupportedException();
            }
    
            public override void SetLength(long value)
            {
                throw new NotSupportedException();
            }
    
            public override int Read(byte[] buffer, int offset, int count)
            {
                throw new NotSupportedException();
            }
    
            public override void Write(byte[] buffer, int offset, int count)
            {
                if (count == 0)
                    return;
    
                var copied = new byte[count];
                Array.Copy(buffer, offset, copied, 0, count);
    
                m_buffers.Add(copied);
            }
    
            public override bool CanRead
            {
                get { return false; }
            }
    
            public override bool CanSeek
            {
                get { return false; }
            }
    
            public override bool CanWrite
            {
                get { return true; }
            }
    
            public override long Length
            {
                get { throw new NotSupportedException(); }
            }
    
            public override long Position
            {
                get { throw new NotSupportedException(); }
                set { throw new NotSupportedException(); }
            }
    
            protected override void Dispose(bool disposing)
            {
                m_buffers.CompleteAdding();
    
                base.Dispose(disposing);
            }
        }
    
        public Stream Reader
        {
            get { return m_reader; }
        }
    
        public Stream Writer
        {
            get { return m_writer; }
        }
    }
    

    Unfortunately, you can’t read and write to those streams at the same time from the same thread. But you can use Task to write from another thread:

    XDocument doc = …;
    
    var forwardingStream = new ForwardingStream();
    
    var client = new HttpClient();
    var content = new StreamContent(forwardingStream.Reader);
    
    Task.Run(() => doc.Save(forwardingStream.Writer));
    
    var response = client.Post(url, content);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a task to perform an HttpWebRequest using Task<WebResponse>.Factory.FromAsync(req.BeginGetRespone, req.EndGetResponse) which can obviously
I'm trying to make an SSL call using HTTPWebRequest and its continually failing saying
I'm using the System.Net.HttpWebRequest class to implement a simple HTTP downloader that can be
var req = (HttpWebRequest)HttpWebRequest.Create(http://mydomain.com/myservice); var resp = (HttpWebResponse)req.GetResponse(); var cookies = resp.Cookies; Console.WriteLine(Cookie count:
How can I login to the this page http://www.bhmobile.ba/portal/index by using HttpWebRequest? Login button
I'm using the HttpWebRequest object to make a get call to a site/web service
I'm using HttpWebRequest to pull down XML, and POST data back to a 'WebService'
I've been using HttpWebRequest/HttpWebResponse lately and I'm getting encoding problems. HttpWebResponse.CharacterSet doesn't always represent
I am returning XML data from the Yahoo GeoPlanet web service using HttpWebRequest .
I am trying to read a remote file using HttpWebRequest in a C# Console

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.