WebRequest.getRequestStream()返回null

时间:2012-12-05 19:59:06

标签: c# webrequest

我试图创建一个简单地将webrequest传递给php的应用程序,但发现在第二次迭代后它无法创建dataStream。

第一次尝试一切都很顺利,但在第二次尝试创建时,我得到一个null dataStream。

知道为什么吗?

    public static void pingServer(string acctString) {
        try
        {
            WebRequest request = WebRequest.Create(@"server/log.php");
            request.Method = "POST";
            request.Timeout = 30;
            string postData = "acctString=" + acctString + "&data=ping";
            byte[] byteArray = Encoding.UTF8.GetBytes(postData);
            request.ContentType = "application/x-www-form-urlencoded";
            request.ContentLength = byteArray.Length;

            using (Stream dataStream = request.GetRequestStream())
            {
                dataStream.Write(byteArray, 0, byteArray.Length);
                dataStream.Close();
            }
        }
        catch (Exception e)
        {
            MessageBox.Show(e.ToString());
        }
    }

0 个答案:

没有答案