我有一些代码可以使用 C# 创建 HTTP POST WebRequest 并将一些数据发布到 https 站点。然后该站点重定向到一个新的 URL 并返回一些数据。
这在我运行 IIS Express 的本地机器上工作得很好,但在我的生产服务器上抛出一个异常,说Content-Length or Chunked Encoding cannot be set for an operation that does not write data.
.
这是代码的样子:
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.AllowAutoRedirect = true;
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = data.Length;
request.UserAgent = userAgent;
StreamWriter stOut = new StreamWriter(request.GetRequestStream(), System.Text.Encoding.ASCII);
stOut.Write(data);
stOut.Close();
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
if (response.StatusCode == HttpStatusCode.OK)
{
using (StreamReader stIn = new StreamReader(request.GetResponse().GetResponseStream()))
{
string responseHtml = stIn.ReadToEnd();
Uri downloadUri = GetDownloadUri(responseHtml);
return downloadUri;
}
}
throw new HttpException((int)response.StatusCode, response.StatusDescription);
有什么想法有什么问题,什么是好的解决方案?
//约翰