簡體   English   中英

這個HttpWebRequest / HttpWebResponse / StreamReader代碼是否有意義?

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

與其在這里添加我的問題,不如在添加我的新問題,因為一旦我在安裝了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 => StringBuilder =>字節數組=>流)永遠不會分配給它! 似乎Stream的存在僅僅是為了編寫代碼行,加熱處理器並減慢操作速度!

這段代碼是荒謬的,還是我只是不喜歡它?

GetRequestStream()返回一個流,該流將通過HttpWebRequest的寫入轉發到網絡。
您的代碼不必要長,但正確。

但是, response.ToString()是錯誤的; 您想使用StreamReader讀取response.GetResponseStream()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM