1. 用HttpClient访问网络API获取网页内容:
Uri uri = new Uri(“http://api.k780.com:88/?app=phone.get&phone=13800138000&appkey=10003&sign=b59bc3ef6191eb9f747dd4e83c99f2a4&format=xml”);//Uri页面为Json字符串或XML字符串
HttpClient client = new HttpClient();
string result = await client.GetStringAsync(uri);//Json或XML字符串
或者
Uri uri = new Uri(“”);//Uri页面为Json字符串或XML字符串
HttpClient client = new HttpClient();
HttpResponseMessage response = await httpClient.GetAsync(uri);
response.EnsureSuccessStatusCode();//如果出现错误,则抛出异常
string result = await response.Content.ReadAsStringAsync();////Json或XML字符串
附异常处理:
if (response.StatusCode == HttpStatusCode.OK)
{
//
}
或: