使用WebRequest和Stream执行请求

时间:2013-01-20 18:13:39

标签: c# stream webrequest

public class MyWebRequest
{
    private WebRequest request;
    private Stream dataStream;

    private string status;

    public String Status
    {
        get
        {
            return status;
        }
        set
        {
            status = value;
        }
    }

    public MyWebRequest(string url)
    {
        // Create a request using a URL that can receive a post.

        request = WebRequest.Create(url);
    }

    public MyWebRequest(string url, string method)
        : this(url)
    {
        if (method.Equals("GET") || method.Equals("POST"))
        {
            // Set the Method property of the request to POST.
            request.Method = method;
        }
        else
        {
            throw new Exception("Invalid Method Type");
        }
    }

    public MyWebRequest(string url, string method, string data)
        : this(url, method)
    {
        // Create POST data and convert it to a byte array.
        string postData = data;
        byte[] byteArray = Encoding.UTF8.GetBytes(postData);

        // Set the ContentType property of the WebRequest.
        request.ContentType = "application/x-www-form-urlencoded";

        // Set the ContentLength property of the WebRequest.
        request.ContentLength = byteArray.Length;

        // Get the request stream.
        dataStream = request.GetRequestStream();

        // Write the data to the request stream.
        dataStream.Write(byteArray, 0, byteArray.Length);

        // Close the Stream object.
        dataStream.Close();
    }

    public string GetResponse()
    {
        // Get the original response.
        WebResponse response = request.GetResponse();

        this.Status = ((HttpWebResponse)response).StatusDescription;

        // Get the stream containing all content returned by the requested server.
        dataStream = response.GetResponseStream();

        // Open the stream using a StreamReader for easy access.
        StreamReader reader = new StreamReader(dataStream);

        // Read the content fully up to the end.
        string responseFromServer = reader.ReadToEnd();

        // Clean up the streams.
        reader.Close();
        dataStream.Close();
        response.Close();

        return responseFromServer;
    }
}

我看到了这段代码并正在分析它。我在WebRequest搜索了msdn课并理解了它,但我不明白为什么我必须使用Stream来执行请求。我不知道流是什么,以及为什么需要它。有人能帮助我吗?另外,有人可以向我解释以下两行吗?

            // Get the request stream.
            dataStream = request.GetRequestStream();

            // Write the data to the request stream.
            dataStream.Write(byteArray, 0, byteArray.Length);

流获取数据,然后我将数据写入流而不是WebRequest对象?

感谢。

3 个答案:

答案 0 :(得分:1)

GetRequestStream用于启动向Internet资源发送数据。 它还用于返回流实例以将数据发送到Internet资源。

答案 1 :(得分:1)

要与HTTP服务器通信,您需要发出HTTP请求(examples)。现在,您实际上通过Stream将该请求作为一系列字节发送。流实际上只是一系列字节。

大多数I / O操作(包括文件或网络套接字)都是缓冲的。这意味着你将字节重复放入缓冲区,当缓冲区足够满时,实际发送它。流实际上只是一个抽象。所以你真的只是在这两行中通过网络发送字节。

答案 2 :(得分:0)

来自MSDN "Provides a generic view of a sequence of bytes"。 I Stream是您可能想要读取或写入数据的各种对象的抽象。具体示例是FileStream用于读取和写入磁盘,以及MemoryStreamTake a look at the other types that inherit from Stream

您突出显示的两行是您在WebRequest中发送数据的方式。想象一下HTTP请求,当您发出请求时,一些数据被发送到服务器。当您使用WebRequest时,通过获取请求流并向其写入字节来编写此数据。