[英]I get a System.NullReferenceException when trying to use HttpWebReaspone on xamarin
當我在普通的控制台應用程序項目中嘗試此代碼時,它可以工作,但不能在 xamarin 上工作,異常發生在
" 使用 (HttpWebResponse 響應 = (HttpWebResponse)request.GetResponse()) "
string html = string.Empty;
string url = $"https://stackoverflow.com/";
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.AutomaticDecompression = DecompressionMethods.GZip;
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
using (Stream stream = response.GetResponseStream())
using (StreamReader reader = new StreamReader(stream))
{
html = reader.ReadToEnd();
}
我建議改用HttpClient
var client = new HttpClient();
var response = await client.GetAsync("http://www.contoso.com/");
string responseBody = await response.Content.ReadAsStringAsync();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.