简体   繁体   English

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

[英]Does this HttpWebRequest/HttpWebResponse/StreamReader code even make sense?

Rather than add on to my question here , I'm adding a new one as, once I looked at my code with my X-Ray vision goggles attached, I don't grok it. 与其在这里添加我的问题,不如在添加我的新问题,因为一旦我在安装了X射线视觉护目镜的代码中查看了代码,就不会感到困惑。

I don't even remember where I got this code, but it's an adaptation of an example I found somewhere. 我什至不记得我从哪里得到了这段代码,但这是我在某个地方找到的示例的改编。 Yet it doesn't seem that the data is even being sent to the server. 但是,似乎数据甚至没有发送到服务器。 To be specific, this code: 具体来说,此代码:

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

...seems to be doing this: ...似乎正在这样做:

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()
  • but what does "request" contain? 但是“请求”包含什么? The contents (of the StreamReader => StringBuilder => Array of Bytes => Stream) are never assigned to it! 内容(StreamReader => StringBuilder =>字节数组=>流)永远不会分配给它! It seems as if the Stream exists for the sole purpose of writing lines of code, heating up the processor, and slowing down the operation! 似乎Stream的存在仅仅是为了编写代码行,加热处理器并减慢操作速度!

Is this code nonsensical, or am I just not grokking it? 这段代码是荒谬的,还是我只是不喜欢它?

GetRequestStream() returns a stream that forwards writes through the HttpWebRequest to the network. GetRequestStream()返回一个流,该流将通过HttpWebRequest的写入转发到网络。
Your code is unnecessarily long, but correct. 您的代码不必要长,但正确。

However, response.ToString() is wrong; 但是, response.ToString()是错误的; you want to read response.GetResponseStream() with a StreamReader . 您想使用StreamReader读取response.GetResponseStream()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM