在.NET Compact Framework中使用POST参数进行异步WebRequest

9

我正在尝试在.NET Compact Framework上进行异步HTTP(S) POST请求,但似乎无法使其工作。

以下是我的操作:

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:

"此请求需要缓冲数据以便身份验证或重定向成功。"

当我进行简单的HTTP GET时,完全相同的代码可以正常工作,但是当我加入一些POST参数时,一切都失败了。

有什么想法吗?

1个回答

12

我太高兴了!我找到了我的问题的答案!!!

这个小小的代码行起了关键作用:

((HttpWebRequest)rqst).AllowWriteStreamBuffering = true;

1
我猜你的意思是要在 ((HttpWebRequest)rqst).KeepAlive = false; 附近添加这个 :) - Jesse Chisholm

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接