用JAVA代码访问一段URL地址是否可用怎么写?
[size=medium]首先、介绍一下get方式:[/size]
[size=medium]第二,介绍一个post方式:[/size]
[size=medium]首先、介绍一下get方式:[/size]
String mainDomain = agentFacade.getVirtualHostService().getMainDomainName(domain);
String webServer =this.getVzzjzManageService().getVZZJZWebSiteByDomain(domain);
String httpUrl="http://mydns2.xinnet.com/"+"cgi-bin/dnsapi_add?+name=" + mainDomain + "&recordname=" + webServer+ "&type=CNAME&content=" + domain + "&ttl=3600&prio=5";
URL url = new URL(httpUrl);
HttpURLConnection httpUrlCon = (HttpURLConnection) url
.openConnection();
httpUrlCon.setDoOutput(true);
httpUrlCon.setRequestMethod("GET");
BufferedReader xinnetIn = new BufferedReader(new InputStreamReader(
httpUrlCon.getInputStream()));
String back = xinnetIn.readLine();
if(null != back && back.contains("ret=100")) {
log.debug(" zhixiang successfully!");
}
else {
log.debug("connect error sitebuild zhixiang error!");
put("error","网络错误,请重试");
throw new MyBizLayerException("自助建站开通成功,指向错误!");
}
xinnetIn.close();
httpUrlCon.disconnect();
[size=medium]第二,介绍一个post方式:[/size]
String clubBack = null;
String sendParam = "domainName=" + newDomainName ;
String httpVhost = "http://www.xinnet.com/club/vhost/checkVhost.jsp";
URL url = new URL(httpVhost);
HttpURLConnection httpUrlCon = (HttpURLConnection) url
.openConnection();
httpUrlCon.setDoOutput(true);
httpUrlCon.setRequestMethod("POST");
httpUrlCon.getOutputStream()
.write(sendParam.toString().getBytes());
httpUrlCon.getOutputStream().flush();
httpUrlCon.getOutputStream().close();
BufferedReader xinnetIn = new BufferedReader(new InputStreamReader(
httpUrlCon.getInputStream()));
clubBack = xinnetIn.readLine();
xinnetIn.close();
httpUrlCon.disconnect();
if (clubBack == null) {
log.debug("connect error!");
put("error","网络错误,请重试");
return "vzzjzupdatedomainname";
}
if("1".equals(clubBack)){
log.debug(clubBack);
put("error","您的域名已开通自助建站产品,请更换其他域名绑定!");
return "vzzjzupdatedomainname";
}