Asynchronous WebRequest with POST-parameters in .NET Compact Framework

倾然丶 夕夏残阳落幕 提交于 2019-12-03 13:03:27

问题


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...*/
}

...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."

The exact same code works perfectly, when I do a simple HTTP GET, but when I throw in some POST params, everything fails.

Any ideas?


回答1:


I am ever so happy! I found the answer to my question!!!

This little line did the trick:

((HttpWebRequest)rqst).AllowWriteStreamBuffering = true;


来源:https://stackoverflow.com/questions/768641/asynchronous-webrequest-with-post-parameters-in-net-compact-framework

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!