C# 我第一次使用 GetRequestStream() 需要 20 秒

标签 c#

我使用 C#。

我第一次在我的代码中使用 WebRequest GetRequestStream() 时,最多需要 20 秒。之后不到 1 秒。

下面是我的代码。 “this.requestStream = httpRequest.GetRequestStream()”行导致延迟。

StringBuilder postData = new StringBuilder(100);
postData.Append("param=");
postData.Append("test");
byte[] dataArray = Encoding.UTF8.GetBytes(postData.ToString());

this.httpRequest = (HttpWebRequest)WebRequest.Create("http://myurl.com");

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

httpRequest.ContentLength = dataArray.Length;

this.requestStream = httpRequest.GetRequestStream();

using (requestStream)
    requestStream.Write(dataArray, 0, dataArray.Length);

this.webResponse = (HttpWebResponse)httpRequest.GetResponse();

Stream responseStream = webResponse.GetResponseStream();
StreamReader responseReader = new System.IO.StreamReader(responseStream, Encoding.UTF8);
String responseString = responseReader.ReadToEnd();

我怎样才能知道是什么原因造成的? (例如:DNS 查找?服务器没有响应?)

感谢和问候,科恩

最佳答案

您也可以尝试设置 .Proxy = null。有时它会尝试自动检测占用时间的代理。

关于C# 我第一次使用 GetRequestStream() 需要 20 秒,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3354455/

相关文章:

c# - 使用 NPOI 在 Excel 电子表格中创建图表

c# - 如何在只有一个数据库查询的情况下在计数器字段上执行 +1?

c# - 为什么在winforms应用程序中点击treeview节点时会出现异常?

c# - MQTT Java 或 C# 服务器

c# - 为什么 ServicePointManager.SecurityProtocol 默认值在不同机器上不同?

c# - 清除文件内容

c# - 在 .net 中的两个项目之间共享 dll

c# - 检查 Ajax 请求

c# - DB Transaction : can we block just a record, 不是整个表?

c# - 隐式和显式类型转换的优缺点