c# - Xamarin Android httpwebrequest,无法访问已处置的对象

标签 c# android post xamarin webrequest

我有一个 android 移动应用程序,我想向服务器发送 webrequest 以发布一些数据,但在发布数据之前我发送一个 http get 请求以获取一些数据,然后发送 post 请求, 首先我收到成功但是当我发送 post 请求时它在我的代码 requestStream.Write(bytes, 0, bytes.Length);

这行抛出异常

异常(exception)是:

System.ObjectDisposedException: Cannot access a disposed object. Object name: 'System.Net.Sockets.NetworkStream'.

这是我的获取和发布请求代码 获取:

public void GetTokenInfo()
        {

            try
            {
                var uri = new Uri(string.Format(_host + "webserver/SesTokInfo", string.Empty));
                var webRequest = WebRequest.Create(uri);
                using (var response = webRequest.GetResponse() as HttpWebResponse)
                {
                    using (var requestStream = response.GetResponseStream())
                    {
                        using (var reader = new StreamReader(requestStream))
                        {
                            var content = reader.ReadToEnd();
                            XmlDocument xDocument = new XmlDocument();
                            xDocument.LoadXml(content);
                            XmlElement root = xDocument.DocumentElement;
                            if (IsResponseReturned(root))
                            {
                                GlobalConfig.SessionId = root.GetElementsByTagName("SesInfo")[0].InnerText;
                                GlobalConfig.Token = root.GetElementsByTagName("TokInfo")[0].InnerText;

                            }
                        }
                    }
                }
            }
            catch (Exception exception)
            {
                Debug.WriteLine(exception);
            }
        }

使用此代码我可以毫无问题地收到我的结果,这是我的帖子:

 public WebResponse PostData(string body, string url)
        {
            WebResponse webResponse = null;
            try
            {
                var uri = new Uri(string.Format(_host + url, string.Empty));
                var webRequest = (HttpWebRequest)WebRequest.Create(uri);
                webRequest.Headers.Add("Cookie",
                    GlobalConfig.SessionId);
                webRequest.Headers.Add("_RequestVerificationToken", GlobalConfig.Token);
                webRequest.Method = "POST";
                webRequest.ContentType = "application/xml";
                byte[] bytes = Encoding.UTF8.GetBytes(body);
                webRequest.ContentLength = bytes.Length;
                Stream requestStream = webRequest.GetRequestStream();
                requestStream.Write(bytes, 0, bytes.Length);
                webResponse = webRequest.GetResponse();
            }
            catch (Exception exception)
            {
                Console.WriteLine(exception);
            }
            return webResponse;
        }

我已经搜索并尝试了一些方法,但我没有得到解决方案,而且当我评论第一个函数时,只运行第二个函数它会工作正常但是当我运行第一个然后运行第二个时它抛出异常, 是否有任何东西属于处理来自第一个代码的流和网络响应? 我认为使用语句已经处理了它们。

感谢任何帮助。

最佳答案

这可能只是确保使用正确的 using 语句的情况,因为它们会在适当的时候正确处理关闭传出和传入流。请参阅下面的修改示例,这应该适合您:

public string PostData(string body, string url)
{
    string responseText = null;

    try
    {
        var uri = new Uri(string.Format(_host + url, string.Empty));
        var webRequest = WebRequest.Create(uri) as HttpWebRequest;

        webRequest.Headers.Add("Cookie", GlobalConfig.SessionId);
        webRequest.Headers.Add("_RequestVerificationToken", GlobalConfig.Token);
        webRequest.Method = "POST";
        webRequest.ContentType = "application/xml";

        using (Stream requestStream = webRequest.GetRequestStream())
        {
            using (StreamWriter writer = new StreamWriter(requestStream))
            {
                writer.Write(body);
            }
        }

        var webResponse = webRequest.GetResponse() as HttpWebResponse;

        using (Stream responseStream = webResponse.GetResponseStream())
        {
            using (StreamReader reader = new StreamReader(responseStream))
            {
                responseText = reader.ReadToEnd();
            }
        }
    }
    catch (Exception exception)
    {
        Console.WriteLine(exception);
    }

    return responseText;
}

请注意,我已将返回变量修改为字符串主体,这样您就不会潜在地让流处于打开状态。

科达

关于c# - Xamarin Android httpwebrequest,无法访问已处置的对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45715073/

相关文章:

android - 使用网站信息启动添加联系人 Activity

javascript - 如何将第三方应用程序 apk 集成到我的 android studio 项目中?

php - ModSecurity 最大发布限制(PCRE 限制错误)

c# - 相机跟随不顺畅

c# - Unity如何让我的枪在玩家移动时停止射击?

c# - LINQ left join + default if empty + 匿名类型 + group by

c# - C# 中用于 MySQL 的数据库抽象层?

android - 可以在 google places api 中插入我们的地点数据吗?

Magento 支付模块,如 PayPal(外部 URL 重定向)

asp.net - jQuery AJAX 调用将数据发布到 ASP.Net 页面(不是 Get 而是 POST)