HTTP POST接收多个HTTP响应

时间:2016-05-20 10:55:58

标签: asp.net http http-post

我有一个通过.NET System.Net.WebRequest操作的http POST,如下所示:

                ...                
                XXXUtilities.Log.WriteLog(string.Format("XXXHTTPPost PostToUri has uri={0}, body={1}", uri, messageBodyAsString));
                System.Net.WebRequest req = System.Net.WebRequest.Create(uri);

                req.ContentType = "application/x-www-form-urlencoded";
                req.Method = "POST";

                byte[] bytes = System.Text.Encoding.ASCII.GetBytes(messageBodyAsString);
                req.ContentLength = bytes.Length;
                System.IO.Stream os = req.GetRequestStream();
                os.Write(bytes, 0, bytes.Length);
                os.Close();

                try
                {
                    using (System.Net.WebResponse resp = req.GetResponse())
                    {
                        if (resp == null) return null;
                        System.IO.StreamReader sr =
                              new System.IO.StreamReader(resp.GetResponseStream());

                        string rs = sr.ReadToEnd().Trim();

                        sr.Close();
                        resp.Close();

                        XXXUtilities.Log.WriteLog(string.Format("XXXHTTPPost PostToUri has string response = {0}", rs));

                        MongoDB.Bson.BsonDocument doc2 = new BsonDocument();

                        doc2.Add("Response", rs);

                        return doc2;
                    }
                }
                catch (System.Net.WebException e)
                {...

这一切在大多数时候都很好。但是,查看这个创建的日志文件我发现了一些奇怪的东西。可疑日志条目如下所示:

18:59:17.0608 HPSHTTPPost PostToUri has uri=https://salesforce.ringlead.com/cgi-bin/2848/3/dedup.pl, body=LastName=Doe&FirstName=Jon
18:59:17.5608 HPSHTTPPost PostToUri has string response = Success
18:59:18.0295 HPSHTTPPost PostToUri has string response = Success

似乎Http Response被收到两次。这在技术上是否可行?即,Http POST是否可以一个接一个地接收两个响应?如果是这样,我的代码是否可能被调用两次,从而导致观察到的日志文件条目?非常感谢。

编辑: 为了回应日志代码可能被破坏的注释,这里是日志代码:

    public class Log
    {
        public static void WriteLog(string commandText)
        {
            string clientDBName = "test";
            string username = "test";
            try
            {

                string filePath = "c:\\Data\\XXXLogs\\" + clientDBName + "logs\\";

                string filename = System.DateTime.Now.ToString("yyyyMMdd_") + username + ".log";

                DirectoryInfo dir = new DirectoryInfo(filePath);
                if (!dir.Exists)
                {
                    dir.Create();
                }
                System.IO.FileStream stream = new System.IO.FileStream(
                    filePath + filename
                    , System.IO.FileMode.Append); // Will create if not already exists
                StreamWriter writer = new StreamWriter(stream);

                writer.WriteLine(); // Writes a line terminator, thus separating entries by 1 blank line
                writer.WriteLine(System.DateTime.Now.ToString("HH:mm:ss.ffff") + " " + commandText);
                writer.Flush();
                stream.Close();
            }
            catch { }

        }
    }

0 个答案:

没有答案