基于C#的网站可用性监测接口调用代码实例代码描述:基于C#的网站可用性监测接口调用代码实例代码平台:聚合数据usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Net;usingSystem.IO;usingXfrog.Net;usingSystem.Diagnostics;usingSystem.Web;//----------------------------------//网站监测调用示例代码-聚合数据//在线接口文档:代码中JsonObject类下载地址:{classProgram{staticvoidMain(string[]args){stringappkey=*******************;//配置您申请的appkey//1.创建任务stringurl1==newDictionarystring,string();parameters1.Add(key,appkey);//你申请的keyparameters1.Add(url,);//要监测的网站的根域名,注意:必须是根域名,其他不支持parameters1.Add(dtype,);//返回的数据格式,json或xml,默认jsonparameters1.Add(contactPhone,);//stringresult1=sendPost(url1,parameters1,get);JsonObjectnewObj1=newJsonObject(result1);StringerrorCode1=newObj1[error_code].Value;if(errorCode1==0){Debug.WriteLine(成功);Debug.WriteLine(newObj1);}else{//Debug.WriteLine(失败);Debug.WriteLine(newObj1[error_code].Value+:+newObj1[reason].Value);}//2.获取监测任务列表stringurl2==newDictionarystring,string();parameters2.Add(key,appkey);//你申请的keyparameters2.Add(dtype,);//返回的数据格式,json或xml,默认jsonparameters2.Add(page,);//页码parameters2.Add(pageSize,);//每页的个数stringresult2=sendPost(url2,parameters2,get);JsonObjectnewObj2=newJsonObject(result2);StringerrorCode2=newObj2[error_code].Value;if(errorCode2==0){Debug.WriteLine(成功);Debug.WriteLine(newObj2);}else{//Debug.WriteLine(失败);Debug.WriteLine(newObj2[error_code].Value+:+newObj2[reason].Value);}//3.获取监测网站实时数据stringurl3==newDictionarystring,string();parameters3.Add(key,appkey);//你申请的keyparameters3.Add(dtype,);//返回的数据格式,json或xml,默认jsonparameters3.Add(siteId,);//网站的id,请在任务列表API里查询parameters3.Add(industry,);//行业id,请在任务列表API里查询stringresult3=sendPost(url3,parameters3,get);JsonObjectnewObj3=newJsonObject(result3);StringerrorCode3=newObj3[error_code].Value;if(errorCode3==0){Debug.WriteLine(成功);Debug.WriteLine(newObj3);}else{//Debug.WriteLine(失败);Debug.WriteLine(newObj3[error_code].Value+:+newObj3[reason].Value);}//4.获取监测网站实时ping响应stringurl4==newDictionarystring,string();parameters4.Add(key,appkey);//你申请的keyparameters4.Add(siteId,);//网站URL唯一标识,请在任务列表API里查询parameters4.Add(industry,);//行业代码,请在任务列表API里查询stringresult4=sendPost(url4,parameters4,get);JsonObjectnewObj4=newJsonObject(result4);StringerrorCode4=newObj4[error_code].Value;if(errorCode4==0){Debug.WriteLine(成功);Debug.WriteLine(newObj4);}else{//Debug.WriteLine(失败);Debug.WriteLine(newObj4[error_code].Value+:+newObj4[reason].Value);}//5.获取监测网站实时http响应stringurl5==newDictionarystring,string();parameters5.Add(key,appkey);//你申请的keyparameters5.Add(siteId,);//网站URL唯一标识,请在任务列表API里查询parameters5.Add(industry,);//行业代码,请在任务列表API里查询stringresult5=sendPost(url5,parameters5,get);JsonObjectnewObj5=newJsonObject(result5);StringerrorCode5=newObj5[error_code].Value;if(errorCode5==0){Debug.WriteLine(成功);Debug.WriteLine(newObj5);}else{//Debug.WriteLine(失败);Debug.WriteLine(newObj5[error_code].Value+:+newObj5[reason].Value);}//6.获取监测网站实时运营商数据stringurl6==newDictionarystring,string();parameters6.Add(key,appkey);//你申请的keyparameters6.Add(siteId,);//网站URL唯一标识,请在任务列表API里查询stringresult6=sendPost(url6,parameters6,get);JsonObjectnewObj6=newJsonObject(result6);StringerrorCode6=newObj6[error_code].Value;if(errorCode6==0){Debug.WriteLine(成功);Debug.WriteLine(newObj6);}else{//Debug.WriteLine(失败);Debug.WriteLine(newObj6[error_code].Value+:+newObj6[reason].Value);}}///summary///Http(GET/POST)////summary///paramname=url请求URL/param///paramname=parameters请求参数/param///paramname=method请求方法/param///returns响应内容/returnsstaticstringsendPost(stringurl,IDictionarystring,stringparameters,stringmethod){if(method.ToLower()==post){HttpWebRequestreq=null;HttpWebResponsersp=null;System.IO.StreamreqStream=null;try{req=(HttpWebRequest)WebRequest.Create(url);req.Method=method;req.KeepAlive=false;req.ProtocolVersion=HttpVersion.Version10;req.Timeout=5000;req.ContentType=application/x-=utf-8;byte[]postData=Encoding.UTF8.GetBytes(BuildQuery(parameters,utf8));reqStream=req.GetRequestStream();reqStream.Write(postData,0,postData.Length);rsp=(HttpWebResponse)req.GetResponse();Encodingencoding=Encoding.GetEncoding(rsp.CharacterSet);returnGetResponseAsString(rsp,encoding);}catch(Exceptionex){returnex.Message;}finally{if(reqStream!=null)reqStream.Close();if(rsp!=null)rsp.Close();}}else{//创建请求HttpWebRequestrequest=(HttpWebRequest)WebRequest.Create(url+?+BuildQuery(parameters,utf8));//GET请求request.Method=GET;request.ReadWriteTimeout=5000;request.ContentType=text/html;charset=UTF-8;HttpWebResponseresponse=(HttpWebResponse)request.GetResponse();StreammyResponseStream