I have some code that creates an HTTP POST WebRequest with C # and sends some data to the https site. This site is then redirected to the new URL and returns some data.
This works very well on my local machine running IIS Express, but an exception is thrown on my production server 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 is wrong and what would be a good solution?
// Johan
source
share