.NET Compact Framework中带有POST参数的异步WebRequest [英] Asynchronous WebRequest with POST-parameters in .NET Compact Framework
问题描述
我正在尝试在.NET Compact Framework上进行异步HTTP(S)POST,但似乎无法正常工作。
I'm trying to do an asynchronous HTTP(S) POST on .NET Compact Framework and I can't seem to get it working.
这就是我的意思我正在做:
Here's what I'm doing:
private void sendRequest(string url, string method, string postdata) {
WebRequest rqst = HttpWebRequest.Create(url);
CredentialCache creds = new CredentialCache();
creds.Add(new Uri(url), "Basic", new NetworkCredential(this.Uname, this.Pwd));
rqst.Credentials = creds;
rqst.Method = method;
if (!String.IsNullOrEmpty(postdata)) {
rqst.ContentType = "application/xml";
byte[] byteData = UTF8Encoding.UTF8.GetBytes(postdata);
rqst.ContentLength = byteData.Length;
using (Stream postStream = rqst.GetRequestStream()) {
postStream.Write(byteData, 0, byteData.Length);
postStream.Close();
}
}
((HttpWebRequest)rqst).KeepAlive = false;
rqst.BeginGetResponse(DataLoadedCB, rqst);
}
private void DataLoadedCB(IAsyncResult result) {
WebRequest rqst = ((WebRequest)(((BCRqst)result.AsyncState).rqst));
WebResponse rsps = rqst.EndGetResponse(result);
/* ETC...*/
}
...但是由于某种原因,我在DataLoadedCB的第二行上得到了WebException:
...but for some reason I get a WebException on the second row of DataLoadedCB:
此请求需要缓冲数据才能成功进行身份验证或重定向。
"This request requires buffering of data for authentication or redirection to be successful."
当我执行简单的HTTP GET时,完全相同的代码可以完美地工作,但是当我放入一些POST参数时,一切都会失败。
The exact same code works perfectly, when I do a simple HTTP GET, but when I throw in some POST params, everything fails.
有什么想法吗?
推荐答案
我真高兴!我找到了问题的答案!!!!
I am ever so happy! I found the answer to my question!!!
这行代码可以解决问题:
This little line did the trick:
((HttpWebRequest)rqst).AllowWriteStreamBuffering = true;
这篇关于.NET Compact Framework中带有POST参数的异步WebRequest的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!