这个HttpWebRequest / HttpWebResponse / StreamReader代码是否有意义?

时间:2014-03-12 18:33:05

标签: c# httpwebrequest httpwebresponse getresponse get-request

而不是添加到我的问题here,我正在添加一个新问题,因为一旦我用我的X射线视觉护目镜查看我的代码,我就不会理解它。

我甚至不记得我在哪里获得这段代码,但它是我在某处找到的一个例子的改编版。然而,似乎数据甚至没有被发送到服务器。具体来说,这段代码:

public static string SendXMLFile(string xmlFilepath, string uri)
{
    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);

    request.KeepAlive = false;
    request.ProtocolVersion = HttpVersion.Version10;

    request.Method = "POST";

    StringBuilder sb = new StringBuilder();
    using (StreamReader sr = new StreamReader(xmlFilepath))
    {
        String line;
        while ((line = sr.ReadLine()) != null)
        {
            // test to see if it's finding any lines
            //MessageBox.Show(line); <= works fine
            sb.AppendLine(line);
        }
        byte[] postBytes = Encoding.UTF8.GetBytes(sb.ToString());

        request.ContentLength = postBytes.Length;
        // Did the sb get into the byte array?
        //MessageBox.Show(request.ContentLength.ToString()); <= shows "112" (seems right)
        request.KeepAlive = false;

        request.ContentType = "application/xml";

        try
        {
            Stream requestStream = request.GetRequestStream();
            // now test this: MessageBox.Show() below causing exception? See https://stackoverflow.com/questions/22358231/why-is-the-httpwebrequest-body-val-null-after-crossing-the-rubicon
            //MessageBox.Show(string.Format("requestStream length is {0}", requestStream.Length.ToString()));
            requestStream.Write(postBytes, 0, postBytes.Length);
            MessageBox.Show(string.Format("requestStream length is {0}", requestStream.Length.ToString()));
            requestStream.Close();

            using (var response = (HttpWebResponse)request.GetResponse())
            {
                return response.ToString();
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show("SendXMLFile exception " + ex.Message);
            request.Abort();
            return string.Empty;
        }
    }
}

......似乎是这样做的:

0) Reads the contents of the file at xmlFilepath and puts it into a StreamReader ("sr")
1) A StringBuilder ("sb") is populated with the contents of the StreamReader
2) The contents of the StringBuilder are put into an array of Bytes ("postBytes")
- then here comes the weird part (or so it seems to me, after analyzing the code more closely):
3) The contents of the array of bytes are written to a Stream ("requestStream")
4) The Stream is closed (???)
5) The HttpWebRequest ("request") attempts to return a HttpWebResponse by calling GetResponse()
  • 但“请求”包含什么?内容(StreamReader =&gt; StringBuilder =&gt; Bytes数组=&gt; Stream)永远不会分配给它!似乎Stream的存在只是为了编写代码行,加热处理器和减慢操作!

这段代码是荒谬的,还是我只是不喜欢它?

1 个答案:

答案 0 :(得分:1)

GetRequestStream()返回一个流,该流通过HttpWebRequest转发到网络 您的代码不必要很长,但是正确。

然而,response.ToString()是错误的;您想要使用response.GetResponseStream()阅读StreamReader