我用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,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查找?服务器没有响应?)
谢谢和问候,Koen
解决方法
您也可以尝试设置.Proxy = null.有时它会尝试自动检测占用时间的代理.