I have some code that creates a HTTP POST WebRequest with C# and posts some data to a https site. That site then redirects to a new URL and returns some data.
This works just fine on my local machine running IIS Express, but on my production server throws an exception that says Content-Length or Chunked Encoding cannot be set for an operation that does not write data.
.
This is what the code looks like:
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);
Any ideas what's wrong and what would a good solution be?
// Johan