How to read a WebClient response after posting data?

前端 未结 2 2090
死守一世寂寞
死守一世寂寞 2021-02-18 17:51

Behold the code:

using (var client = new WebClient())
{
    using (var stream = client.OpenWrite(\"http://localhost/\", \"POST\"))
    {
        stream.Write(pos         


        
2条回答
  •  悲哀的现实
    2021-02-18 18:08

    If you want to keep streams everywhere and avoid allocating huge arrays of bytes, which is good practise (for example, if you plan to post big files), you still can do it with a derived version of WebClient. Here is a sample code that does it.

    using (var client = new WebClientWithResponse())
    {
        using (var stream = client.OpenWrite(myUrl))
        {
            // open a huge local file and send it
            using (var file = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
            {
                file.CopyTo(stream);
            }
        }
    
        // get response as an array of bytes. You'll need some encoding to convert to string, etc.
        var bytes = client.Response;
    }
    

    And here is the customized WebClient:

    public class WebClientWithResponse : WebClient
    {
        // we will store the response here. We could store it elsewhere if needed.
        // This presumes the response is not a huge array...
        public byte[] Response { get; private set; }
    
        protected override WebResponse GetWebResponse(WebRequest request)
        {
            var response = base.GetWebResponse(request);
            var httpResponse = response as HttpWebResponse;
            if (httpResponse != null)
            {
                using (var stream = httpResponse.GetResponseStream())
                {
                    using (var ms = new MemoryStream())
                    {
                        stream.CopyTo(ms);
                        Response = ms.ToArray();
                    }
                }
            }
            return response;
        }
    }
    

提交回复
热议问题