【问题标题】:HttpWebRequest, Redirects, chunked encoding error on live site, but works i IIS Express locallyHttpWebRequest,重定向,实时站点上的分块编码错误,但在本地 IIS Express 上工作
【发布时间】:2012-05-20 23:12:01
【问题描述】:

我有一些代码可以使用 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);

任何想法有什么问题以及好的解决方案是什么?

// 约翰

【问题讨论】:

  • 我很确定您不需要设置 ContentLength。试着把那条线去掉。
  • 不需要,但我仍然遇到同样的错误。甚至尝试设置 SendChunked = false。
  • 也许在生产中,您的数据字节数组不知何故为空?

标签: c# redirect httpwebrequest chunked-encoding


【解决方案1】:

我遇到了类似的问题。在生产中,所有 url 都会自动重定向到 https。似乎 HttpWebRequest 无法处理重定向并抛出此异常消息。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-23
    • 2015-08-06
    • 1970-01-01
    • 2014-01-20
    • 1970-01-01
    相关资源
    最近更新 更多