0
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;
    }
}

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

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

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

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

谢谢。

4

3 回答 3

1

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

于 2013-01-20T18:21:35.067 回答
1

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

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

于 2013-01-20T18:27:46.883 回答
0

来自MSDN“提供字节序列的通用视图”。I Stream 是您可能想要从中读取或写入数据的各种对象的抽象。具体示例FileStream用于读取和写入磁盘,以及MemoryStream. 看看从 Stream 继承的其他类型

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

于 2013-01-20T18:28:23.623 回答