I think that was the issue, I changed the code to this, based of that
link you sent and your recomendation.  It seems to not error. 

private string getData(string url, Boolean postData, String xmlData)
        {
            String responseFromServer=string.Empty;
            try
            {
                // Create a request using a URL that can receive a post.

                WebRequest request = WebRequest.Create(url);
                if (postData)
                {
                    // Set the Method property of the request to POST.
                    request.Method = "POST";
                    // Create POST data and convert it to a byte array.
                    byte[] byteArray = Encoding.ASCII.GetBytes(xmlData);
                    // Set the ContentType property of the WebRequest.
                    request.ContentType = "text/plain";
                    // Set the ContentLength property of the WebRequest.
                    request.ContentLength = byteArray.Length;
                    // Get the request stream.
                    using (Stream dataStream =
request.GetRequestStream())
                    {
                        dataStream.Write(byteArray, 0,
byteArray.Length);
                    }
                }

                // Get the response.
                using (WebResponse response = request.GetResponse())
                {
 
Console.WriteLine(((HttpWebResponse)response).StatusDescription);
                    // Get the stream containing content returned by the
server.
                    using (Stream responseStream =
response.GetResponseStream())
                    {
                        using (StreamReader reader = new
StreamReader(responseStream))
                        {
                            responseFromServer = reader.ReadToEnd();
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                System.Diagnostics.StackTrace stackTrace = new
System.Diagnostics.StackTrace();
                String method = stackTrace.GetFrame(0).GetMethod().Name;
                log.writeLogInfo("ERROR " + method + ":", ex.Message +
"\n" + ex.StackTrace.ToString());
            }
            return responseFromServer;
        }


-----Original Message-----
From: [email protected]
[mailto:[email protected]] On Behalf Of Jonathan Pryor
Sent: Thursday, August 04, 2011 5:06 PM
To: Discussions related to Mono for Android
Subject: Re: [mono-android] Program stops

On Aug 4, 2011, at 4:29 PM, Tim Kelly wrote:
> However, on another note.  One of the things I took out of the proof
was a HttpWebRequest and HttpWebResponse which hit the server.  So, I'm
adding more logging and have found one thing. 
>  
> Repeatedly calling these cause the network to stop functioning until
you reboot the emulator.  Calling the equivalent in java posses no
issues.

I'm reminded of https://bugzilla.novell.com/show_bug.cgi?id=648862#c9

However, I see that you are disposing of `response` and other Stream
instances, so I'm not sure where the leak is coming from. That said, you
have lots of catch{} blocks; are those being hit? I ask because there
are code paths where, if an exception were thrown, the stream wouldn't
be closed:

                    Stream dataStream = request.GetRequestStream();
                    // Write the data to the request stream.
                    dataStream.Write(byteArray, 0, byteArray.Length);
                    // Close the Stream object.
                    dataStream.Close();

Instead, try:

        using (Stream dataStream = request.GetRequestStream())
                dataStream.WRite (byteArray, 0, byteArray.Length);

I would actually suggest using `using` more often, as I found that
method hard to read (but that may be just me), e.g. a simple translation
would be:

        private string GetData(string url, Boolean postData, String
xmlData)
        {
                StringBuilder sb = new StringBuilder();
                try {
                        using (var request = (HttpWebRequest)
WebRequest.Create(url)) {
                                request.ContentType = "text/plain";
                                if (postData) {
                                        request.Method = "POST";
                                        byte[] data =
Encoding.ASCII.GetBytes(xmlData);
                                        request.ContentLength =
data.Length;
                                        using (o =
request.GetRequestStream())
                                                o.Write (data, 0,
data.Length);
                                }
                                using (var response = (HttpWebResponse)
request.GetReponse())
                                using (var resStream =
response.GetResponseStream ()) {
                                        byte[] buf = new byte[4 * 1024];

                                        int count = 0;
                                        do {
                                                count = resStream.Read
(buf, 0, buf.Length);
                                                if (count != 0)
                                                        sb.Append
(Encoding.ASCII.GetString(buf, 0, count));
                                        } while (count > 0);
                                }
                        }
                } catch {}
                return sb.ToString ();
        }

 - Jon

_______________________________________________
Monodroid mailing list
[email protected]

UNSUBSCRIBE INFORMATION:
http://lists.ximian.com/mailman/listinfo/monodroid
_______________________________________________
Monodroid mailing list
[email protected]

UNSUBSCRIBE INFORMATION:
http://lists.ximian.com/mailman/listinfo/monodroid

Reply via email to