C# 这个HttpWebRequest/HttpWebResponse/StreamReader代码有意义吗?

C# 这个HttpWebRequest/HttpWebResponse/StreamReader代码有意义吗?,c#,httpwebrequest,httpwebresponse,getresponse,get-request,C#,Httpwebrequest,Httpwebresponse,Getresponse,Get Request,我没有补充我的问题,而是补充了一个新的问题,因为一旦我戴上X光目镜看了代码,我就不会再胡思乱想了 我甚至不记得我从哪里得到这段代码,但这是我在某处找到的一个例子的改编。然而,数据似乎并没有被发送到服务器。具体而言,此代码: public static string SendXMLFile(string xmlFilepath, string uri) { HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);

我没有补充我的问题,而是补充了一个新的问题,因为一旦我戴上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;
        }
    }
}
  • 但“请求”包含什么?(StreamReader=>StringBuilder=>Array of Bytes=>Stream的)内容从未分配给它!似乎流的存在只是为了编写代码行、加热处理器和降低操作速度
这段代码是毫无意义的,还是我只是不去摸索它?

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


但是,
response.ToString()
是错误的;您希望使用
StreamReader
读取
response.GetResponseStream()

而使用
HttpClient
;这要容易得多。你可以用
File.ReadAllBytes()
替换十行代码。我不认为HttpClient在Compact Framework中可用。File.ReadAllBytes()在Compact Framework中似乎也不可用。CF讨厌“简单方法”。你的意思是简单地将“return-response.ToString();”替换为“return-response.GetResponseStream().ToString();”?我想不会,因为这会给我“NotSupportedException”,并且val仍然为空。否;我的意思是创建一个StreamReader并调用ReadToEnd(),我已经有了一个StreamReader;你是说另一个StreamReader(代替Stream)吗?我根据这个建议进行了更改,如更新4所示:
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()