如何在发布数据后阅读WebClient响应?

时间:2022-10-08 18:22:27

Behold the code:

看看代码:

using (var client = new WebClient())
{
    using (var stream = client.OpenWrite("http://localhost/", "POST"))
    {
        stream.Write(post, 0, post.Length);
    }
}

Now, how do I read the HTTP output?

现在,我如何读取HTTP输出?

2 个解决方案

#1


20  

It looks like you have a byte[] of data to post; in which case I expect you'll find it easier to use:

看起来你要发布一个byte []数据;在这种情况下,我希望你会发现它更容易使用:

byte[] response = client.UploadData(address, post);

And if the response is text, something like:

如果响应是文本,则类似于:

string s = client.Encoding.GetString(response);

(or your choice of Encoding - perhaps Encoding.UTF8)

(或您选择的编码 - 也许是Encoding.UTF8)

#2


2  

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.

如果你想在任何地方保留流并避免分配庞大的字节数组,这是一种很好的做法(例如,如果你计划发布大文件),你仍然可以使用派生版本的WebClient来实现。这是一个示例代码。

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:

这是定制的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;
    }
}

#1


20  

It looks like you have a byte[] of data to post; in which case I expect you'll find it easier to use:

看起来你要发布一个byte []数据;在这种情况下,我希望你会发现它更容易使用:

byte[] response = client.UploadData(address, post);

And if the response is text, something like:

如果响应是文本,则类似于:

string s = client.Encoding.GetString(response);

(or your choice of Encoding - perhaps Encoding.UTF8)

(或您选择的编码 - 也许是Encoding.UTF8)

#2


2  

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.

如果你想在任何地方保留流并避免分配庞大的字节数组,这是一种很好的做法(例如,如果你计划发布大文件),你仍然可以使用派生版本的WebClient来实现。这是一个示例代码。

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:

这是定制的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;
    }
}