Upload with HttpWebRequest/Response (POST)

P

Peter Bladh

Hi

I found a really good example here by Alex Feinman that describes who to
upload a file with PUT.
But I need to upload the file with POST and don't get it to work. I get
(500) Internal Server Error. Here's the code (I've also tried with some
other headers)

HttpWebRequest req = (HttpWebRequest)WebRequest.Create(Url);
BinaryReader rdr = new BinaryReader(File.OpenRead(FileToUpload));
byte[] data = rdr.ReadBytes((int)rdr.BaseStream.Length);
req.Method = "POST";
req.ContentType = "application/octet-stream";
req.SendChunked = true;
req.Timeout = 10000;
req.ContentLength = data.Length;
req.KeepAlive = true;

Stream stream = req.GetRequestStream();
stream.Write(data, 0, data.Length);
stream.Close();
HttpWebResponse res = (HttpWebResponse)req.GetResponse();
Console.WriteLine(res.ToString());
res.Close();



/peter bladh
 
A

Alex Feinman [MVP]

For starters you neeed to set req.ContentType to
"application/x-www-form-urlencoded", otherwise on the other end Request.Form
does not get populated
 
P

Peter Bladh

OK, now I've tried that, but it still get Internal Server Error.

What else should I try?

/peter
 

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