我的xamarin代码有system.net.webeexception: '在WebClient请求期间发生异常。的误差
NameValueCollection postCollection = new NameValueCollection();
postCollection.Add("q", city);
postCollection.Add("appid", ApiKey);
WebClient postClient = new WebClient();
var postResult = postClient.UploadValues(
"https://samples.openweathermap.org/data/2.5/weather",
"GET",
postCollection); //this line has error
您几乎肯定会得到以下错误:
不能发送带有此动词类型的内容体。
这是因为UploadValues
打算表示POST或PUT样式的请求,因此postCollection
被序列化为请求体,这对于GET请求是不允许的。
Download*
系列方法之一,例如DownloadString
,用于GET请求:
var url = $"https://samples.openweathermap.org/data/2.5/weather?q={city}&appid={ApiKey}"
var response = postClient.DownloadString(url);