473,799 Members | 3,290 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.Http WebRequest req =
HttpWebRequest) System.Net.WebR equest.Create(U RI);
req.ContentType = "multipart/form-data;
boundary="+boun dary;

byte[] bytes =
System.Text.Enc oding.ASCII.Get Bytes(Parameter s);
req.Method = "POST";
req.ContentLeng th = bytes.Length;

req.KeepAlive = true;
req.CachePolicy = new
System.Net.Cach e.RequestCacheP olicy(System.Ne t.Cache.Request CacheLevel.NoCa cheNoStore);
req.Timeout = 3300;
req.SendChunked = true;

System.IO.Strea m os = req.GetRequestS tream();
os.Write(bytes, 0, bytes.Length);
os.Close();
try
{
System.Net.WebR esponse resp = req.GetResponse ();
System.Text.Enc oding enc =
System.Text.Enc oding.Default;
if (resp == null) return null;
Console.WriteLi ne(resp.Content Length);
System.IO.Strea mReader sr = new
System.IO.Strea mReader(resp.Ge tResponseStream (),System.Text. Encoding.ASCII) ;

string sa = sr.ReadToEnd();
sr.Close();
resp.Close();
return sa;
}
catch (System.Net.Web Exception webe)
{
Console.WriteLi ne(webe.Respons e);
return null;
}
}

Mar 30 '07 #1
6 13234
<na*********@gm ail.comwrote in message
news:11******** *************@y 66g2000hsf.goog legroups.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...@gma il.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.com wrote:
On Mar 30, 7:04 am, nakul.re...@gma il.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>
TheContentLengt hproperty contains the value of the Content-Length
header returned with the response. If the Content-Length header is not
set in the response,Conten tLengthis 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...@gma il.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*********@gma il.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.Http WebRequest req =
HttpWebRequest) System.Net.WebR equest.Create(U RI);
req.ContentType = "multipart/form-data;
boundary="+boun dary;
byte[] bytes =
System.Text.Enc oding.ASCII.Get Bytes(Parameter s);
req.Method = "POST";
req.ContentLeng th = 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********@joer gjooss.de
Mar 30 '07 #6
On Mar 30, 12:40 pm, Joerg Jooss <news-re...@joergjoos s.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.Http WebRequest req =
HttpWebRequest) System.Net.WebR equest.Create(U RI);
req.ContentType = "multipart/form-data;
boundary="+boun dary;
byte[] bytes =
System.Text.Enc oding.ASCII.Get Bytes(Parameter s);
req.Method = "POST";
req.ContentLeng th = 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...@joergjoos s.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
8854
by: Aung | last post by:
Has anybody develop RFC1950 and RFC1951 compliant Zip utility? Any pointer will be appreciated.
1
3169
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 reaching the web site but my data seems not to be posted. Also in debug mode I get error "Remote server returned error 401: Unautherized) at the "WebResponse Response = Request.GetResponse();" line and debugging stops. I have the required certificate...
2
4092
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(). webRequest.Method = "POST"; buffer = new byte; for (int i = 0 ; i < urlExecute.Length - 1 ; i++)
2
3390
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 scenario - when the web browser requests a file, the IIS Server1 has to go back to Server2 to make a request for the file. Server2 then responds the file back to Server1 and Server1 should be able to just forward the file down to the web browser....
4
1910
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 to my target url and it will respond with a value depend on a value which i send. <SELECT name=langpair><OPTION value=en|de>English to German</OPTION><OPTION value=en|es>English to Spanish</OPTION> </select>
1
8207
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 to SMCHALLANGE=YES to allow me to obtain a session. I then iterate the cookie collection to extract the SMSESSION value. All is good (so far). This is when the sky turns grey and the rain starts to fall. I then use this SMSESSION in a web...
4
2132
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 what is going on. I'm not getting any errors right now, but code is not working either - not reading the post in second form or else the first form is not sending it correctly....
0
1109
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; System.Text.ASCIIEncoding encoding1 = new System.Text.ASCIIEncoding(); Byte byt1 = encoding.GetBytes(myxmlDoc1.OuterXml);
0
10484
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10228
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10027
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9072
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7565
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6805
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5463
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4141
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2938
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.