I have an Android mobile app and I want to send webrequest to the server to publish some data, but before sending the data, I send an http get request to receive some data, and then send an email request, I receive a successful receipt first, but when I send a request to send, it throws an exception below in this line of my code requestStream.Write(bytes, 0, bytes.Length);
an exception:
System.ObjectDisposedException: Unable to access the remote object. Object Name: "System.Net.Sockets.NetworkStream".
and here is my request code for receiving and sending a GET:
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); } }
with this code I get my result without any problems, and here is my POST:
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; }
I searched and tried the ways, but I did not get a solution, plus plus, when I comment on the first function and perform only the second function, it will work fine, but when I run the first and then the second, it throws an exception, something belongs to removing a stream and network response from the first code? I think the use of the instruction already has.
any help appreciated.
android c # post xamarin webrequest
Younis qadir
source share