I’m trying to do an HTTP POST REQUEST on .NET Compact Framework and I can’t get it working.
This is what I got:
public static string DoPost(string url)
{
// initialize from variables
string responseString = string.Empty;
ASCIIEncoding encoding = new ASCIIEncoding();
//// UTF8Encoding encoding = new UTF8Encoding();
HttpWebResponse response;
byte[] data = encoding.GetBytes("dummy");
StreamReader reader;
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
//do the processing
SetRequestProperties(request, "POST"); // SETTING METHOD TO POST HERE
request.GetRequestStream().Write(data, 0, data.Length);
request.GetRequestStream().Close();
response = (HttpWebResponse)request.GetResponse();
reader = new StreamReader(response.GetResponseStream());
responseString = reader.ReadToEnd();
//clean up
response.Close();
response.GetResponseStream().Close();
reader.Close();
reader = null;
response = null;
request = null;
encoding = null;
//return
MessageBox.Show("POST SUCCESS");
return responseString;
}
private static void SetRequestProperties(HttpWebRequest request, string s)
{
request.Method = s;
request.AllowWriteStreamBuffering = true;
request.KeepAlive = false;
request.ContentType = "application/x-www-form-urlencoded";
request.SendChunked = false;
request.Credentials = CredentialCache.DefaultCredentials;
request.UserAgent = "my mobile user agent";
request.Timeout = 60000;
request.ProtocolVersion = new System.Version("1.1");
}
…but for some reasons it always send 0 length of binary data. The code seems to be working fine with WinForms and Webpages, but not on CF.
Any Idea what´s wrong or what I forget in my code?
Thanks
I don’t see you setting the request.ContentLength. Here’s code that I use that I know works: