.NET Compact Framework中带有POST参数的WebRequest [英] WebRequest with POST-parameters in .NET Compact Framework
本文介绍了.NET Compact Framework中带有POST参数的WebRequest的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试在.NET Compact Framework上执行HTTP POST请求,但我无法使其正常工作。
I'm trying to do an HTTP POST REQUEST on .NET Compact Framework and I can't get it working.
这就是我得到的:
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");
}
...但是由于某些原因,它总是发送0长度的二进制数据。该代码似乎可以在WinForms和网页上正常工作,但不能在CF上正常工作。
...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?
谢谢
推荐答案
我看不到您设置了request.ContentLength。这是我使用的我知道有效的代码:
I don't see you setting the request.ContentLength. Here's code that I use that I know works:
private string SendData(string method, string directory, string data)
{
string page = string.Format("http://{0}/{1}", DeviceAddress, directory);
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(page);
request.KeepAlive = false;
request.ProtocolVersion = HttpVersion.Version10;
request.Method = method;
CredentialCache creds = GenerateCredentials();
if (creds != null)
{
request.Credentials = creds;
}
// turn our request string into a byte stream
byte[] postBytes;
if(data != null)
{
postBytes = Encoding.UTF8.GetBytes(data);
}
else
{
postBytes = new byte[0];
}
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = postBytes.Length;
Stream requestStream = request.GetRequestStream();
// now send it
requestStream.Write(postBytes, 0, postBytes.Length);
requestStream.Close();
HttpWebResponse response;
response = (HttpWebResponse)request.GetResponse();
return GetResponseData(response);
}
public string Post(string directory, string data)
{
return SendData("POST", directory, data);
}
这篇关于.NET Compact Framework中带有POST参数的WebRequest的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文