【问题标题】:Asynchronous WebRequest with POST-parameters in .NET Compact Framework.NET Compact Framework 中带有 POST 参数的异步 WebRequest
【发布时间】:2009-04-20 14:54:28
【问题描述】:

我正在尝试在 .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 参数时,一切都失败了。

有什么想法吗?

【问题讨论】:

    标签: c# .net compact-framework


    【解决方案1】:

    我真的很开心!我找到了我的问题的答案!!!

    这条小线成功了:

    ((HttpWebRequest)rqst).AllowWriteStreamBuffering = true;
    

    【讨论】:

    • 我想你的意思是在 ((HttpWebRequest)rqst).KeepAlive = false; 附近添加这个 :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-04
    • 2020-12-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多