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