java 解析域名得到host

java

// 形如https://www.baidu.com 或 www.baidu.com, 判断这两种情况,并解析前者去掉http头,传入domain host

// 方案1:正则表达式 + URI解析方法

import java.io.IOException;

import java.net.URI;

import java.net.URL;

import java.util.regex.Pattern;

public class Main {

public static void main(String[] args) {

System.out.println("Hello World!");

// parseDomain();

String url = "www.baidu.com";

String domain = parseDomain(url);

System.out.println("+++domain: " + domain);

}

public static boolean verifyDomain(String url) {

String regex = "^([hH][tT]{2}[pP]:/*|[hH][tT]{2}[pP][sS]:/*|[fF][tT][pP]:/*)(([A-Za-z0-9-~]+).)+([A-Za-z0-9-~\\/])+(\\?{0,1}(([A-Za-z0-9-~]+\\={0,1})([A-Za-z0-9-~]*)\\&{0,1})*)$";

Pattern pattern = Pattern.compile(regex);

if (pattern.matcher(url).matches()) {

System.out.println("是正确的网址");

return true;

} else {

System.out.println("非法网址");

return false;

}

}

private static String parseDomain(String url) {

String regex = "^([hH][tT]{2}[pP]:/*|[hH][tT]{2}[pP][sS]:/*|[fF][tT][pP]:/*)(([A-Za-z0-9-~]+).)+([A-Za-z0-9-~\\/])+(\\?{0,1}(([A-Za-z0-9-~]+\\={0,1})([A-Za-z0-9-~]*)\\&{0,1})*)$";

Pattern pattern = Pattern.compile(regex);

if (pattern.matcher(url).matches()) {

       return URI.create(url).getHost();

} else {

       return url;

}

}

public static void parseDomain() throws IOException {

String urlAddress = "http://www.roseindia.net/jsf/JSFLoginApplication.shtml";

urlAddress = "http://www.baidu.com";

URL url = new URL(urlAddress);

System.out.println("url.getHost() = " + url.getHost());

System.out.println("url.getPath() = " + url.getPath());

System.out.println("url.getPort() = " + url.getPort());

System.out.println("url.getProtocol() = " + url.getProtocol());

}

}

// 方案2: String的split(), substring(), 拆解出子串来

一般的URL的结构:

[ 协议名 ]:// [ 域名 ] : [ 端口号 ] / [ 路 ] ... [ 径 ]/ [ 文件名 ] ? [ 参 ]& [ 数 ] & [ 部 ]& [ 分 ] # [ 锚部分 ]

js代码如下:

function parseURL(url){

var urlObj={};

urlObj.scheme = url.substring(0,url.indexOf(":")); //协议头

var temp1=url.substring(url.indexOf("//")+2); //去掉协议头后剩下部分

var temp2=temp1.substring(0,temp1.indexOf("/"))//域名+端口号,substring(start,stop) stop要比最后一个提取的字符位置多1

urlObj.domaine = temp2.substring(0,temp1.indexOf(":"));

urlObj.port = temp2.substring(temp1.indexOf(":")+1);

if(temp1.indexOf("?")!=-1){

urlObj.path =temp1.substring(temp1.indexOf("/")+1,temp1.indexOf("?"))//路径+文件名

emp1.IndexOf("#")!=-1? urlObj.params=temp1.substring(temp1.indexOf("?")+1,temp1.IndexOf("#"))

:urlObj.params=temp1.substring(temp1.indexOf("?")+1)

}else{

urlObj.path =temp1.substring(temp1.indexOf("/")+1);

}

urlObj.fragment=temp1.substring(temp1.indexOf("#")+1);

return urlObj;

}

以上是 java 解析域名得到host 的全部内容, 来源链接: utcz.com/z/394506.html

回到顶部