在 IIS 中查看工作进程时,我们一直注意到挂起的请求,我们最初认为指定 HTTP 超时值可以解决问题,但事实并非如此。我们找到了导致问题的代码,但我们仍然不确定是什么导致了它。
以下代码接受键/值对列表并创建 HTTP Post 请求并将值放入 POST 有效负载中。它大部分时间都可以正常工作,但不确定可能导致问题的原因。
public List<KeyValuePair<string, string>> Data { get; set; }
public string Invoke()
{
StringBuilder paramBuilder = new StringBuilder();
for (int i = 0; i < Data.Count; i++)
{
var d = Data[i];
string delimeter = i < Data.Count - 1 ? "&" : string.Empty;
paramBuilder.AppendFormat("{0}={1}{2}", d.Key.Trim(), d.Value != null ? HttpUtility.UrlEncode(d.Value.Trim()) : string.Empty, delimeter);
}
WebRequest request = WebRequest.Create(Url);
request.ContentLength = paramBuilder.Length;
request.ContentType = "application/x-www-form-urlencoded";
request.Method = "POST";
Stream rs = request.GetRequestStream();
ASCIIEncoding encoding = new ASCIIEncoding();
var postData = encoding.GetBytes(paramBuilder.ToString());
rs.Write(postData, 0, postData.Length);
var response = request.GetResponse();
StreamReader reader = new StreamReader(response.GetResponseStream());
string str = reader.ReadToEnd();
rs.Close(); //JUST ADDED _ MAYBE THESE WERE THE PROBLEM?
reader.Close(); //JUST ADDED - MAYBE THESE WERE THE PROBLEM?
return str;
}
任何想法都会有所帮助。
谢谢