对HttpWebRequest.GetRequestStream采取延迟

时间:2013-12-03 05:26:10

标签: c# .net httpwebrequest

我正在使用HttpWebRequest.GetRequestStream连接到Dot Net的其他PHP链接,将一些数据从.Net发送到PHP。但是当控件进入“Stream newrStream = myrRequest.GetRequestStream();”行时,它的前两次工作正常。并且从第三次开始延迟了几秒钟。如何解决这个问题。

我的编码是:

ASCIIEncoding Encode = new ASCIIEncoding();
     string postDat = "Name=xxx";
     byte[] datas = Encode.GetBytes(postDat);

HttpWebRequest myrRequest = HttpWebRequest)WebRequest.Create("http://www.xxx.php");
            myrRequest.Method = "POST";
            myrRequest.ContentType = "application/x-www-form-urlencoded";
            myrRequest.ContentLength = datas.Length;
            Stream newrStream = myrRequest.GetRequestStream();

            // Send the data.
            newrStream.Write(datas, 0, datas.Length);
            newrStream.Close();

1 个答案:

答案 0 :(得分:0)

要记住的第一件事是检查发送的URI,参数和标题,具体来说:

  1. 保留字符。通过URI发送保留字符可以带来 问题! * ' ( ) ; : @ & = + $ , / ? # [ ]
  2. URI长度:不应超过2000个字符
  3. 长度标头:大多数网络服务器都会限制它们的标头大小 接受。例如,在Apache默认限制为8KB。
  4. 请注意,如果您想要发送较长的数据,建议您在邮件正文中发送:

        string uri = string.Format("https://my-server.com/api/document?queryParameter={1}", "value");
        WebRequest req = WebRequest.Create(uri);
        req.Method = "POST";
        string data = "Some data"; //Body data
        ServicePointManager.Expect100Continue = false;
        using (var streamWriter = new StreamWriter(req.GetRequestStream()))
        {
            streamWriter.Write(data);
            streamWriter.Flush();
            streamWriter.Close();
        }
        HttpWebResponse resp = req.GetResponse() as HttpWebResponse;
        Stream GETResponseStream = resp.GetResponseStream();
        StreamReader sr = new StreamReader(GETResponseStream);
        var response = sr.ReadToEnd(); //Response
        resp.Close(); //Close response
        sr.Close(); //Close StreamReader