我试图从网站获取数据C#:
https://secure.lni.wa.gov/verify/Detail.aspx?LIC=1CALLCC871KC
看起来这个网站在页面加载后从ajax调用中获取数据。当我调用此代码时:
using (var client = new WebClient())
{
return client.DownloadString(URL);
}
它获取基本HTML但不处理ajax调用并填写数据。有没有办法在从代码渲染后获得最终页面?
答案 0 :(得分:1)
您可以直接调用 GetBusinessDetails 方法来获取json结果,而不是解析该html内容。
string URL = "https://secure.lni.wa.gov/verify/Controller.aspx/GetBusinessDetails";
using (var client = new WebClient())
{
client.Headers["Content-Type"] = "application/json; charset=UTF-8";
var json = client.UploadString(URL, JsonConvert.SerializeObject(new { License = "1CALLCC871KC", Ubi ="", IrlVilationId="", IsSecured="" }));
dynamic response = JsonConvert.DeserializeObject(json);
Console.WriteLine(response.d.ReturnValue.Contractor.BusinessName.ToString());
}
JsonConvert.SerializeObject
和JsonConvert.DeserializeObject
是Json.Net库