发送同步请求在Xamarin为IOS
本文关键字:Xamarin IOS 请求 同步 | 更新日期: 2023-09-27 18:17:13
如何在Xamarin中发送同步请求。
string url = "http://search.twitter.com/search.json?q=xamarin&rpp=10&include_entities=false&result_type=mixed";
NSUrlRequest urlRequest = NSUrlRequest.FromUrl (new Uri (url));
NSUrlResponse response = null;
NSError error = null;
NSData data = NSUrlConnection.SendSynchronousRequest (urlRequest,response,error);
Console.WriteLine ("Error Data value {0}", data);
这就是我所做的。
能给我一个建议吗如何在Xamarin IOS发送同步请求?有同步请求吗?
我不建议同步请求,但如果你必须,你可以使用WebClient或HttpClient异步阻塞调用。
从同步函数调用HttpClient:
var httpClient = new HttpClient();
HttpResponseMessage response = httpClient.GetAsync("http://search.twitter.com/search.json?q=xamarin&rpp=10&include_entities=false&result_type=mixed").Result;
if (response.IsSuccessStatusCode)
{
var data = response.Content.ReadAsStringAsync().Result;
System.Diagnostics.Debug.WriteLine(data);
}
else
{
System.Diagnostics.Debug.WriteLine(response);
}
WebClient电话:
var webClient = new System.Net.WebClient();
try
{
var str = webClient.DownloadString("http://search.twitter.com/search.json?q=xamarin&rpp=10&include_entities=false&result_type=mixed");
System.Diagnostics.Debug.WriteLine(str);
}
catch (System.Net.WebException exception)
{
System.Diagnostics.Debug.WriteLine(exception.Message);
}