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;
}
}