473,405 Members | 2,354 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

WebResponse ContentLength=-1

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.Cac he.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(),Sy stem.Text.Encoding.ASCII);

string sa = sr.ReadToEnd();
sr.Close();
resp.Close();
return sa;
}
catch (System.Net.WebException webe)
{
Console.WriteLine(webe.Response);
return null;
}
}

Mar 30 '07 #1
6 13199
<na*********@gmail.comwrote in message
news:11*********************@y66g2000hsf.googlegro ups.com...
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.
Mar 30 '07 #2
On Mar 30, 7:04 am, nakul.re...@gmail.com wrote:
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

Mar 30 '07 #3
On Mar 30, 3:19 am, "Jon Skeet [C# MVP]" <s...@pobox.comwrote:
On Mar 30, 7:04 am, nakul.re...@gmail.com wrote:
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 -1ContentLength.
From the docs for HttpWebResponse.ContentLength:

<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

Mar 30 '07 #4
On Mar 30, 3:22 pm, nakul.re...@gmail.com wrote:
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

Mar 30 '07 #5
Thus wrote na*********@gmail.com,
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 Jooss
ne********@joergjooss.de
Mar 30 '07 #6
On Mar 30, 12:40 pm, Joerg Jooss <news-re...@joergjooss.dewrote:
Thus wrotenakul.re...@gmail.com,
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 Jooss
news-re...@joergjooss.de
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.

Apr 1 '07 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Aung | last post by:
Has anybody develop RFC1950 and RFC1951 compliant Zip utility? Any pointer will be appreciated.
1
by: Jacob | last post by:
Please help figure what I am doing wrong in this code. All I am trying to do is send come data to an aspx page and get some data back. I watched my variable in debug mode and can tell that I am...
2
by: Randy | last post by:
How do I get the contents of the HttpWebResponse to display in a web browser (IE or browser control)? Here is a code snippet. The response ends up being jscript with a window.open(). ...
2
by: Jeff G. | last post by:
Hello everyone, I have read through the newsgroups (thank God for 'em!) extensively looking for an example of how to pass a file (PDF) from a webresponse stream down to a web client. Here's the...
4
by: Savas Ates | last post by:
I have a vb.net web application. I want to post some variables to another web page and take some values back and process them. This is codes in my target url. I should post "langpair" cariable...
1
by: Mr Flibble | last post by:
OK I logon to a web site and I manage to get an SMSESSION cookie that I then store in a variable called _session (a class scoping variable). I do this by calling a logon URL and setting a cookie...
4
by: CindyH | last post by:
Hi - hope someone can help with this - this code was working for a while in the 'real' code and then suddenly stopped - not sure what happen. I made two simple forms on localhost to try to test...
0
by: deeps1512 | last post by:
hi All, I have written the following code to get a webresponse. XmlDocument myxmlDoc1 = new XmlDocument(); myxmlDoc1.LoadXml(BkXMLRQ); string s = myxmlDoc1.OuterXml;...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.