简体   繁体   中英

HttpWebRequest GetRequestStream timeout on Xamarin Android C#

The following code runs perfectly and returns data immediately in a Wpf test application but hangs on request.GetRequestStream() in Xamarin Android:

var request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "POST";
request.ContentType = @"application/json; charset=utf-8";
request.Accept = @"application/json; charset=utf-8";

string postData = "{}";

using (StreamWriter sw = new StreamWriter(request.GetRequestStream()))
    sw.Write(postData);

var response = request.GetResponse();

using (var sw = new StreamReader(response.GetResponseStream()))
{
    var result = sw.ReadToEnd();
}

No code change between the apps, copy and paste. Any ideas?

Edit: I do have another section of code that uses ChannelFactory to connect to a different wcf service. That code works and returns data to the app. This url is for a service hosting RESTful/JSON contracts. The Internet permission is checked.

Make sure you dispose your response, otherwise it will only take a couple of requests before you potentially start queueing in .NET code.

// **snip**
using (var response = request.GetResponse())
{
    using (var sw = new StreamReader(response.GetResponseStream()))
    {
        var result = sw.ReadToEnd();
    }
}
// **snip**

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM