I don't know if has something related to that but there is missing a "using"
on webrequest and the error appear to be related to request and not on
receive, so that could be like:

using (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);
                    }
                }
}

Karl

From:  Tim Kelly <tim.ke...@designerware.com>
Reply-To:  "monodroid@lists.ximian.com" <monodroid@lists.ximian.com>
Date:  Thu, 4 Aug 2011 21:32:57 -0400
To:  "monodroid@lists.ximian.com" <monodroid@lists.ximian.com>
Subject:  Re: [mono-android] Program stops

Spoke to soon.  Looping hitting the Internet causes it to fail even
after changing the code as suggested.  Eventually it dies and you have
to restart the emulator.  Not sure if this behavior would be the same in
production?

[ERROR get:](652): Error: NameResolutionFailure
[ERROR get:](652):   at System.Net.HttpWebRequest.EndGetRequestStream
(IAsyncResult asyncResult) [0x00000] in <filename unknown>:0
[ERROR get:](652):   at System.Net.HttpWebRequest.GetRequestStream ()
[0x00000] in <filename unknown>:0
[ERROR get:](652):   at DroidAgent.Class_Web_Services.getData
(System.String url, Boolean postData, System.String xmlData) [0x00000]
in <filename unknown>:0

with the following code:

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: monodroid-boun...@lists.ximian.com
[mailto:monodroid-boun...@lists.ximian.com] 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
Monodroid@lists.ximian.com

UNSUBSCRIBE INFORMATION:
http://lists.ximian.com/mailman/listinfo/monodroid
_______________________________________________
Monodroid mailing list
Monodroid@lists.ximian.com

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



_______________________________________________
Monodroid mailing list
Monodroid@lists.ximian.com

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

Reply via email to