3

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);

何が間違っていて、良い解決策は何ですか?

//ヨハン

4

1 に答える 1

0

同様の問題がありました。本番環境では、すべての URL が自動的に https にリダイレクトされます。HttpWebRequest はリダイレクトを処理できず、この例外メッセージをスローするようです。

于 2013-08-02T04:50:41.950 に答える