WebResponse ContentLength=-1

N

nakul.reddy

I'm trying to use HTTPWebRequest to POST data to a website using
multipart/form-data.
It's HTTPS, and it returns a response object with -1 ContentLength.

Any help would be appreciated!

Below is my code:

private string HTTPPost(string URI, string Parameters)
{
System.Net.HttpWebRequest req =
HttpWebRequest)System.Net.WebRequest.Create(URI);
req.ContentType = "multipart/form-data;
boundary="+boundary;

byte[] bytes =
System.Text.Encoding.ASCII.GetBytes(Parameters);
req.Method = "POST";
req.ContentLength = bytes.Length;

req.KeepAlive = true;
req.CachePolicy = new
System.Net.Cache.RequestCachePolicy(System.Net.Cache.RequestCacheLevel.NoCacheNoStore);
req.Timeout = 3300;
req.SendChunked = true;

System.IO.Stream os = req.GetRequestStream();
os.Write(bytes, 0, bytes.Length);
os.Close();
try
{
System.Net.WebResponse resp = req.GetResponse();
System.Text.Encoding enc =
System.Text.Encoding.Default;
if (resp == null) return null;
Console.WriteLine(resp.ContentLength);
System.IO.StreamReader sr = new
System.IO.StreamReader(resp.GetResponseStream(),System.Text.Encoding.ASCII);

string sa = sr.ReadToEnd();
sr.Close();
resp.Close();
return sa;


}
catch (System.Net.WebException webe)
{
Console.WriteLine(webe.Response);
return null;
}
}
 
A

Alberto Poblacion

I'm trying to use HTTPWebRequest to POST data to a website using
multipart/form-data.
It's HTTPS, and it returns a response object with -1 ContentLength.

This probably means that the server is not sending to you the
Content-Length HTTP header, which is optional anyway. So you have to read
the response stream until it ends, and compute the length yourself if you
need it based on the number of bytes received.
 
J

Jon Skeet [C# MVP]

I'm trying to use HTTPWebRequest to POST data to a website using
multipart/form-data.
It's HTTPS, and it returns a response object with -1 ContentLength.
From the docs for HttpWebResponse.ContentLength:

<quote>
The ContentLength property contains the value of the Content-Length
header returned with the response. If the Content-Length header is not
set in the response, ContentLength is set to the value -1.
</quote>

So, presumably it's not setting the header. Given that you're reading
to the end of the stream anyway, do you really need to know the
content length ahead of time?

Jon
 
N

nakul.reddy

<quote>
TheContentLengthproperty contains the value of the Content-Length
header returned with the response. If the Content-Length header is not
set in the response,ContentLengthis set to the value -1.
</quote>

So, presumably it's not setting the header. Given that you're reading
to the end of the stream anyway, do you really need to know the
content length ahead of time?

Jon

Thanks Jon and Alberto.I should have mentioned that I did get a
response. The problem is that the webresponse is identical to the
webrequest content.

I messed around with the code, and this is true regardless of what I
send, even if it is no data.

Suspecting it could be the content, i dumped my request from Fiddler2
and sent it directly, matching the contentlength. Fiddler2 returns a
positive contentlength header, but not so in c#. No exceptions are
thrown. Is there a way I can check the status of the request?

Fiddler2's response headers:
HTTP/1.1 200 OK
Connection: close
Cache-control: no-cache; no-store
Last-Modified: Fri, 30 Mar 2007 14:10:35 GMT
Date: Fri, 30 Mar 2007 14:10:35 GMT
Vary: Accept-Encoding
Content-Length: 9515
Content-Type: text/html; charset=ISO-8859-1;
Server: Apache/1.3.34 (Unix) mod_gzip/1.3.26.1a mod_perl/1.29
Expires: Thu, 01 Jan 1970 00:00:00 GMT

C# response headers:
Connection: close
Transfer-Encoding: chunked
Vary: Accept-Encoding
Cache-Control: no-cache; no-store
Content-Type: text/html; charset=ISO-8859-1;
Date: Fri, 30 Mar 2007 13:57:08 GMT
Expires: Thu, 01 Jan 1970 00:00:00 GMT
Last-Modified: Fri, 30 Mar 2007 13:57:08 GMT
Server: Apache/1.3.34 (Unix) mod_gzip/1.3.26.1a mod_perl/1.29

Argh, been racking my brains for the past two days, any suggestions
appreciated!
Rana
 
J

Jon Skeet [C# MVP]

Thanks Jon and Alberto.I should have mentioned that I did get a
response. The problem is that the webresponse is identical to the
webrequest content.

I messed around with the code, and this is true regardless of what I
send, even if it is no data.

Suspecting it could be the content, i dumped my request from Fiddler2
and sent it directly, matching the contentlength. Fiddler2 returns a
positive contentlength header, but not so in c#. No exceptions are
thrown. Is there a way I can check the status of the request?

It's not clear exactly what you did. Rather than just capturing the
responses, you should capture both the requests and the responses. If
the server is delivering different responses, presumably the requests
are different too.

Jon
 
J

Joerg Jooss

Thus wrote (e-mail address removed),
I'm trying to use HTTPWebRequest to POST data to a website using
multipart/form-data.
It's HTTPS, and it returns a response object with -1 ContentLength.
Any help would be appreciated!

Below is my code:

private string HTTPPost(string URI, string Parameters)
{
System.Net.HttpWebRequest req =
HttpWebRequest)System.Net.WebRequest.Create(URI);
req.ContentType = "multipart/form-data;
boundary="+boundary;
byte[] bytes =
System.Text.Encoding.ASCII.GetBytes(Parameters);
req.Method = "POST";
req.ContentLength = bytes.Length;
req.KeepAlive = true;

Unless "Parameters" has a very special format, I'm doubtful this produces
a valid multipart/form-data request. And why do you even use that MIME type?

Cheers,
 
N

nakul.reddy

Thus (e-mail address removed),


I'm trying to use HTTPWebRequest to POST data to a website using
multipart/form-data.
It's HTTPS, and it returns a response object with -1 ContentLength.
Any help would be appreciated!
Below is my code:
private string HTTPPost(string URI, string Parameters)
{
System.Net.HttpWebRequest req =
HttpWebRequest)System.Net.WebRequest.Create(URI);
req.ContentType = "multipart/form-data;
boundary="+boundary;
byte[] bytes =
System.Text.Encoding.ASCII.GetBytes(Parameters);
req.Method = "POST";
req.ContentLength = bytes.Length;
req.KeepAlive = true;

Unless "Parameters" has a very special format, I'm doubtful this produces
a valid multipart/form-data request. And why do you even use that MIME type?

Cheers,

Joerg/Jon

Thanks. Your responses got me thinking, and I figured out the
ContentLength issue. It was not given because it was optional. By
adding

req.Headers.add("Accept-Encoding: gzip,deflate");

I got a gzipped response with a contentlength. Unfortunately, however,
this was still the same data as the webrequest. That, of course, is
for another thread.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top