Xamarin Android httpwebrequest, Unable to access remote object

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.

+8
android c # post xamarin webrequest
source share
4 answers

After you try many ways and read the answers above and try them, I finally solve the problem by making both codes as follows: GET:

  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 reader = new StreamReader(response.GetResponseStream())) { 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; } } } 

and POST one:

  var uri = new Uri(string.Format(_host + url, string.Empty)); var webRequest2 = (HttpWebRequest)WebRequest.Create(uri); webRequest2.Headers.Add("Cookie", GlobalConfig.SessionId); webRequest2.Headers.Add("_RequestVerificationToken", GlobalConfig.Token); webRequest2.Method = "POST"; webRequest2.ContentType = "application/xml"; byte[] bytes = Encoding.UTF8.GetBytes(body); webRequest2.ContentLength = bytes.Length; StreamWriter writer = new StreamWriter(webRequest2.GetRequestStream()); writer.Write(bytes); webResponse = webRequest2.GetResponse(); 

when I use the request stream inside the block, it will still throw an exception, but without using the block it will work :)

+1
source

This may be the case of using the correct using statements, as they properly handle the closure of the outgoing and incoming flows when necessary. See the example below, this should work for you:

 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; } 

Note. I changed the returned variable to the body of the string so that you would not open the stream.

Koda

+5
source
  • Is your device / emulator connected to the Internet?

  • Does your application require permission to connect to the Internet?

  • You must close the requestStream.Close() request stream before calling webRequest.GetResponse()

  • It is required to use using for IDisposable objects, as in your GetTokenInfo() method, otherwise you will have problems with excessive memory

I just check your code and IT WORKS , this is my complete test console application

 using System; using System.IO; using System.Net; using System.Text; namespace test01 { class MainClass { public static void Main(string[] args) { Console.WriteLine("Hello World!"); PostData("a", "/"); } public static WebResponse PostData(string body, string url) { WebResponse webResponse = null; try { var uri = new Uri(string.Format("http://google.it" + url, string.Empty)); var webRequest = (HttpWebRequest)WebRequest.Create(uri); webRequest.Headers.Add("Cookie","test"); webRequest.Headers.Add("_RequestVerificationToken", "test"); 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; } } } 
+3
source

just to be safe - try removing the extra use - instead

using (var requestStream = response.GetResponseStream()) { using (var reader = new StreamReader(requestStream))

using

using (var reader = new StreamReader(response.GetResponseStream())

See https://msdn.microsoft.com/library/ms182334.aspx for more details.

+1
source

All Articles