基于JAVA的网站监测api调用代码实例

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

基于JAVA的网站监测api调用代码实例代码描述:基于JAVA的网站监测api调用代码实例代码平台:聚合数据importjava.io.BufferedReader;importjava.io.DataOutputStream;importjava.io.IOException;importjava.io.InputStream;importjava.io.InputStreamReader;importjava.io.UnsupportedEncodingException;importjava.net.HttpURLConnection;importjava.net.URL;importjava.net.URLEncoder;importjava.util.HashMap;importjava.util.Map;importnet.sf.json.JSONObject;/***网站监测调用示例代码-聚合数据*在线接口文档:**/publicclassJuheDemo{publicstaticfinalStringDEF_CHATSET=UTF-8;publicstaticfinalintDEF_CONN_TIMEOUT=30000;publicstaticfinalintDEF_READ_TIMEOUT=30000;publicstaticStringuserAgent=Mozilla/5.0(WindowsNT6.1)AppleWebKit/537.36(KHTML,likeGecko)Chrome/29.0.1547.66Safari/537.36;//配置您申请的KEYpublicstaticfinalStringAPPKEY=*************************;//1.创建任务publicstaticvoidgetRequest1(){Stringresult=null;Stringurl=请求接口地址Mapparams=newHashMap();//请求参数params.put(key,APPKEY);//您申请到的APPKEYparams.put(url,);//要监测的网站的根域名,注意:必须是根域名,其他不支持params.put(dtype,);//返回的数据格式,json或xml,默认jsonparams.put(contactPhone,);//try{result=net(url,params,GET);JSONObjectobject=JSONObject.fromObject(result);if(object.getInt(error_code)==0){System.out.println(object.get(result));}else{System.out.println(object.get(error_code)+:+object.get(reason));}}catch(Exceptione){e.printStackTrace();}}//2.获取监测任务列表publicstaticvoidgetRequest2(){Stringresult=null;Stringurl=请求接口地址Mapparams=newHashMap();//请求参数params.put(key,APPKEY);//您申请到的APPKEYparams.put(dtype,);//返回的数据格式,json或xml,默认jsonparams.put(page,);//页码params.put(pageSize,);//每页的个数try{result=net(url,params,GET);JSONObjectobject=JSONObject.fromObject(result);if(object.getInt(error_code)==0){System.out.println(object.get(result));}else{System.out.println(object.get(error_code)+:+object.get(reason));}}catch(Exceptione){e.printStackTrace();}}//3.获取监测网站实时数据publicstaticvoidgetRequest3(){Stringresult=null;Stringurl=请求接口地址Mapparams=newHashMap();//请求参数params.put(key,APPKEY);//您申请到的APPKEYparams.put(dtype,);//返回的数据格式,json或xml,默认jsonparams.put(siteId,);//网站的id,请在任务列表API里查询params.put(industry,);//行业id,请在任务列表API里查询try{result=net(url,params,GET);JSONObjectobject=JSONObject.fromObject(result);if(object.getInt(error_code)==0){System.out.println(object.get(result));}else{System.out.println(object.get(error_code)+:+object.get(reason));}}catch(Exceptione){e.printStackTrace();}}//4.获取监测网站实时ping响应publicstaticvoidgetRequest4(){Stringresult=null;Stringurl=请求接口地址Mapparams=newHashMap();//请求参数params.put(key,APPKEY);//您申请到的APPKEYparams.put(siteId,);//网站URL唯一标识,请在任务列表API里查询params.put(industry,);//行业代码,请在任务列表API里查询try{result=net(url,params,GET);JSONObjectobject=JSONObject.fromObject(result);if(object.getInt(error_code)==0){System.out.println(object.get(result));}else{System.out.println(object.get(error_code)+:+object.get(reason));}}catch(Exceptione){e.printStackTrace();}}//5.获取监测网站实时http响应publicstaticvoidgetRequest5(){Stringresult=null;Stringurl=请求接口地址Mapparams=newHashMap();//请求参数params.put(key,APPKEY);//您申请到的APPKEYparams.put(siteId,);//网站URL唯一标识,请在任务列表API里查询params.put(industry,);//行业代码,请在任务列表API里查询try{result=net(url,params,GET);JSONObjectobject=JSONObject.fromObject(result);if(object.getInt(error_code)==0){System.out.println(object.get(result));}else{System.out.println(object.get(error_code)+:+object.get(reason));}}catch(Exceptione){e.printStackTrace();}}//6.获取监测网站实时运营商数据publicstaticvoidgetRequest6(){Stringresult=null;Stringurl=请求接口地址Mapparams=newHashMap();//请求参数params.put(key,APPKEY);//您申请到的APPKEYparams.put(siteId,);//网站URL唯一标识,请在任务列表API里查询try{result=net(url,params,GET);JSONObjectobject=JSONObject.fromObject(result);if(object.getInt(error_code)==0){System.out.println(object.get(result));}else{System.out.println(object.get(error_code)+:+object.get(reason));}}catch(Exceptione){e.printStackTrace();}}publicstaticvoidmain(String[]args){}/****@paramstrUrl请求地址*@paramparams请求参数*@parammethod请求方法*@return网络请求字符串*@throwsException*/publicstaticStringnet(StringstrUrl,Mapparams,Stringmethod)throwsException{HttpURLConnectionconn=null;BufferedReaderreader=null;Stringrs=null;try{StringBuffersb=newStringBuffer();if(method==null||method.equals(GET)){strUrl=strUrl+?+urlencode(params);}URLurl=newURL(strUrl);conn=(HttpURLConnection)url.openConnection();if(method==null||method.equals(GET)){conn.setRequestMethod(GET);}else{conn.setRequestMethod(POST);conn.setDoOutput(true);}conn.setRequestProperty(User-agent,userAgent);conn.setUseCaches(false);conn.setConnectTimeout(DEF_CONN_TIMEOUT);conn.setReadTimeout(DEF_READ_TIMEOUT);conn.setInstanceFollowRedirects(false);conn.connect();if(params!=null&&method.equals(POST)){try{DataOutputStreamout=newDataOutputStream(conn.getOutputStream());out.writeBytes(urlencode(params));}catch(Exceptione){//TODO:handleexception}}InputStreamis=conn.getInputStream();reade

1 / 7
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功