导航:首页 > IDC知识 > java获取url中的域名

java获取url中的域名

发布时间:2020-10-12 20:06:10

1、Java请求一个URL。获取网站返回的数据。

public static String SendGET(String url,String param){
   String result="";//访问返回结果
   BufferedReader read=null;//读取访问结果
   
   try {
    //创建url
    URL realurl=new URL(url+"?"+param);
    //打开连接
    URLConnection connection=realurl.openConnection();
     // 设置通用的请求属性
             connection.setRequestProperty("accept", "*/*");
             connection.setRequestProperty("connection", "Keep-Alive");
             connection.setRequestProperty("user-agent",
                     "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
             //建立连接
             connection.connect();
          // 获取所有响应头字段
             Map<String, List<String>> map = connection.getHeaderFields();
             // 遍历所有的响应头字段,获取到cookies等
             for (String key : map.keySet()) {
                 System.out.println(key + "--->" + map.get(key));
             }
             // 定义 BufferedReader输入流来读取URL的响应
             read = new BufferedReader(new InputStreamReader(
                     connection.getInputStream(),"UTF-8"));
             String line;//循环读取
             while ((line = read.readLine()) != null) {
                 result += line;
             }
   } catch (IOException e) {
    e.printStackTrace();
   }finally{
    if(read!=null){//关闭流
     try {
      read.close();
     } catch (IOException e) {
      e.printStackTrace();
     }
    }
   }
    
   return result; 
 }

2、java获取URL

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

public class GetLinks {
private String webSource;
private String url;

public GetLinks(String url) throws MalformedURLException, IOException {
this.url = Complete(url);
webSource = getWebCon(this.url);
}

private String getWebCon(String strURL) throws MalformedURLException,
IOException {
StringBuffer sb = new StringBuffer();
java.net.URL url = new java.net.URL(strURL);
BufferedReader in = new BufferedReader(new InputStreamReader(url
.openStream()));
String line;
while ((line = in.readLine()) != null) {
sb.append(line);
}
in.close();
return sb.toString();
}

private String Complete(String link)throws MalformedURLException{
URL url1 = new URL(link);
URL url2 = new URL(link+"/");
String handledUrl = link;
try{
StringBuffer sb1 = new StringBuffer();
BufferedReader in1 = new BufferedReader(new InputStreamReader(url1
.openStream()));
String line1;
while ((line1 = in1.readLine()) != null) {
sb1.append(line1);
}
in1.close();

StringBuffer sb2 = new StringBuffer();
BufferedReader in2 = new BufferedReader(new InputStreamReader(url2
.openStream()));
String line2;
while ((line2 = in2.readLine()) != null) {
sb2.append(line2);
}
in1.close();

if(sb1.toString().equals(sb2.toString())){
handledUrl = link+"/";
}
}catch(Exception e){
handledUrl = link;
}
return handledUrl;

}

/**
* 处理链接的相对路径
* @param link 相对路径或绝对路径
* @return 绝对路径
*/
private String urlHandler(String link) {
if (link == null)
return null;
link = link.trim();

if (link.toLowerCase().startsWith("http://")
|| link.toLowerCase().startsWith("https://")) {
return link;
}
String pare = url.trim();
if (!link.startsWith("/")) {
if (pare.endsWith("/")) {
return pare + link;
}

if (url.lastIndexOf("/") == url.indexOf("//") + 1 || url.lastIndexOf("/") == url.indexOf("//") + 2) {
return pare + "/" + link;
} else {
int lastSeparatorIndex = url.lastIndexOf("/");
return url.substring(0, lastSeparatorIndex + 1) + link;
}
}else{
if (url.lastIndexOf("/") == url.indexOf("//") + 1 || url.lastIndexOf("/") == url.indexOf("//") + 2) {
return pare + link;
}else{
return url.substring(0,url.indexOf("/", url.indexOf("//")+3)) + link;
}
}
}

public List<String> getAnchorTagUrls() {
if (webSource == null) {
System.out.println("没有网页源代码");
return null;
}
ArrayList<String> list = new ArrayList<String>();
int index = 0;
while (index != -1) {
index = webSource.toLowerCase().indexOf("<a ", index);
if (index != -1) {
int end = webSource.indexOf(">", index);
String str = webSource.substring(index, end == -1 ? webSource
.length() : end);
str = str.replaceAll("\\s*=\\s*", "=");
if (str.toLowerCase().matches("^<a.*href\\s*=\\s*[\'|\"]?.*")) {// "^<a\\s+\\w*\\s*href\\s*=\\s*[\'|\"]?.*"
int hrefIndex = str.toLowerCase().indexOf("href=");
int leadingQuotesIndex = -1;
if ((leadingQuotesIndex = str.indexOf("\"", hrefIndex
+ "href=".length())) != -1) { // 形如<a
// href=".....">
int TrailingQuotesIndex = str.indexOf("\"",
leadingQuotesIndex + 1);
TrailingQuotesIndex = TrailingQuotesIndex == -1 ? str
.length() : TrailingQuotesIndex;
str = str.substring(leadingQuotesIndex + 1,
TrailingQuotesIndex);
str = urlHandler(str);
list.add(str);
System.out.println(str);
index += "<a ".length();
continue;
}

if ((leadingQuotesIndex = str.indexOf("\'", hrefIndex
+ "href=".length())) != -1) { // 形如<a
// href='.....'>
int TrailingQuotesIndex = str.indexOf("\'",
leadingQuotesIndex + 1);
TrailingQuotesIndex = TrailingQuotesIndex == -1 ? str
.length() : TrailingQuotesIndex;
str = str.substring(leadingQuotesIndex + 1,
TrailingQuotesIndex);
str = urlHandler(str);
System.out.println(str);
list.add(str);
index += "<a ".length();
continue;
}

int whitespaceIndex = str.indexOf(" ", hrefIndex
+ "href=".length()); // 形如<a href=
// http://www.网络.com >
whitespaceIndex = whitespaceIndex == -1 ? str.length()
: whitespaceIndex;
str = str.substring(hrefIndex + "href=".length(),
whitespaceIndex);
str = urlHandler(str);
list.add(str);
System.out.println(str);

}
index += "<a ".length();
}
}
return list;
}

public static void main(String[] args) throws Exception {
GetLinks gl = new GetLinks("http://www.网络.com");
List<String> list = gl.getAnchorTagUrls();
for(String str:list) {
System.out.println(str);
}
}
}

3、给你一个url,用java怎获取url里的属性???请各位大侠帮个忙,最好有代码

把能找到的参数都付给argsarr数组,将来可以用argsarr[i]进行相应的访问,方法不错,太长,参数不容易记忆。
//提取URL中的参数
function getArgs()
{
//定义一个数组,用于存放取出来的字符串参数。
var argsArr = new Object();

//获取URL中的查询字符串参数
var query = window.location.search;
query = query.substring(1);

//这里的pairs是一个字符串数组
//name=myname&password=1234&sex=male&address=nanjing
var pairs = query.split("&");

for(var i=0;i<pairs.length;i++)
{
var sign = pairs[i].indexOf("=");

//如果没有找到=号,那么就跳过,跳到下一个字符串(下一个循环)。
if(sign == -1)
{
continue;

}

var aKey = pairs[i].substring(0,sign);
var aValue = pairs[i].substring(sign+1);

argsArr[aKey] = aValue;
}

return argsArr;
}

4、怎么用java获取浏览器中url的值,比如自己编写一个程序,当前浏览器显示为百度,如何获取url详细一点

这个页面的路径都是自己指定的,那有获取的

5、java中 如何获取客户端请求的url

在servlet中的request对象中有url,可以用方法 getRequestURI().
如果在程序中得不到该请求的request对象 那就得不到。

所以得到url的 关键是 先得到 request

6、java中如何实现URL类?

java中实现URL类,可以使用java工具类中的URL的类,实例如下:

import java.io.*;
import java.net.*;
public class URLTest
{
public static void main(String[] args)
{
try
{
URL url=new URL("http://sports.163.com:80/nba/");//创建资源类型
String protocol=url.getProtocol();//获取资源类型
String host=url.getHost();//获取域名
int port=url.getPort();//获取端口
String file=url.getFile();//获取路径
System.out.println("url地址的资源类型为:"+protocol+"域名为:"+host+"端口为:"+port+"路径为:"+file);
InputStream is=url.openStream();//获取页面信息流
BufferedReader bfr=new BufferedReader(new InputStreamReader(is));//封装成字符流
String len;
while((len=bfr.readLine())!=null)
{
System.out.println(len);
}
bfr.close();
is.close();
}
catch(MalformedURLException e)
{
System.out.println("创建URL对象发生异常");
}
catch(IOException e)
{
System.out.println("发生IO操作异常");
}
}
}

7、谁有截取url中 一级域名的 java代码

String url = request.getScheme()+"://"; //请求协议 http 或 https
url+=request.getHeader("host"); // 请求服务器
url+=request.getRequestURI(); // 工程名
if(request.getQueryString()!=null) //判断请求参数是否为空
url+="?"+request.getQueryString(); // 参数

8、java 获取url 中的参数请问以下代码中的 url地址该怎么写

String
url
=
request.getScheme()+"://";
//请求协议
http

https
url+=request.getHeader("host");
//
请求服务器
url+=request.getRequestURI();
//
工程名
if(request.getQueryString()!=null)
//判断请求参数是否为空
url+="?"+request.getQueryString();
//
参数

9、java的substring取得url地址

public class StringTest {

/**
* @param args
*/
public static void main(String[] args) {
String str = "http://.网络.com/new?word=java%E7%9A%84substring%E5%8F%96%E5%BE%97url%E5%9C%B0%E5%9D%80";

System.out.println(str.indexOf("/", 8));
System.out.println(str.substring(0, str.indexOf("/", 8)));
}

}

10、java提取网站内部所有URL

||import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

public class GetLinks {
private String webSource;
private String url;

public GetLinks(String url) throws MalformedURLException, IOException {
this.url = Complete(url);
webSource = getWebCon(this.url);
}

private String getWebCon(String strURL) throws MalformedURLException,
IOException {
StringBuffer sb = new StringBuffer();
java.net.URL url = new java.net.URL(strURL);
BufferedReader in = new BufferedReader(new InputStreamReader(url
.openStream()));
String line;
while ((line = in.readLine()) != null) {
sb.append(line);
}
in.close();
return sb.toString();
}

private String Complete(String link)throws MalformedURLException{
URL url1 = new URL(link);
URL url2 = new URL(link+"/");
String handledUrl = link;
try{
StringBuffer sb1 = new StringBuffer();
BufferedReader in1 = new BufferedReader(new InputStreamReader(url1
.openStream()));
String line1;
while ((line1 = in1.readLine()) != null) {
sb1.append(line1);
}
in1.close();

StringBuffer sb2 = new StringBuffer();
BufferedReader in2 = new BufferedReader(new InputStreamReader(url2
.openStream()));
String line2;
while ((line2 = in2.readLine()) != null) {
sb2.append(line2);
}
in1.close();

if(sb1.toString().equals(sb2.toString())){
handledUrl = link+"/";
}
}catch(Exception e){
handledUrl = link;
}
return handledUrl;

}

/**
* 处理链接的相对路径
* @param link 相对路径或绝对路径
* @return 绝对路径
*/
private String urlHandler(String link) {
if (link == null)
return null;
link = link.trim();

if (link.toLowerCase().startsWith("http://")
|| link.toLowerCase().startsWith("https://")) {
return link;
}
String pare = url.trim();
if (!link.startsWith("/")) {
if (pare.endsWith("/")) {
return pare + link;
}

if (url.lastIndexOf("/") == url.indexOf("//") + 1 || url.lastIndexOf("/") == url.indexOf("//") + 2) {
return pare + "/" + link;
} else {
int lastSeparatorIndex = url.lastIndexOf("/");
return url.substring(0, lastSeparatorIndex + 1) + link;
}
}else{
if (url.lastIndexOf("/") == url.indexOf("//") + 1 || url.lastIndexOf("/") == url.indexOf("//") + 2) {
return pare + link;
}else{
return url.substring(0,url.indexOf("/", url.indexOf("//")+3)) + link;
}
}
}

public List<String> getAnchorTagUrls() {
if (webSource == null) {
System.out.println("没有网页源代码");
return null;
}
ArrayList<String> list = new ArrayList<String>();
int index = 0;
while (index != -1) {
index = webSource.toLowerCase().indexOf("<a ", index);
if (index != -1) {
int end = webSource.indexOf(">", index);
String str = webSource.substring(index, end == -1 ? webSource
.length() : end);
str = str.replaceAll("\\s*=\\s*", "=");
if (str.toLowerCase().matches("^<a.*href\\s*=\\s*[\'|\"]?.*")) {// "^<a\\s+\\w*\\s*href\\s*=\\s*[\'|\"]?.*"
int hrefIndex = str.toLowerCase().indexOf("href=");
int leadingQuotesIndex = -1;
if ((leadingQuotesIndex = str.indexOf("\"", hrefIndex
+ "href=".length())) != -1) { // 形如<a
// href=".....">
int TrailingQuotesIndex = str.indexOf("\"",
leadingQuotesIndex + 1);
TrailingQuotesIndex = TrailingQuotesIndex == -1 ? str
.length() : TrailingQuotesIndex;
str = str.substring(leadingQuotesIndex + 1,
TrailingQuotesIndex);
str = urlHandler(str);
list.add(str);
System.out.println(str);
index += "<a ".length();
continue;
}

if ((leadingQuotesIndex = str.indexOf("\'", hrefIndex
+ "href=".length())) != -1) { // 形如<a
// href='.....'>
int TrailingQuotesIndex = str.indexOf("\'",
leadingQuotesIndex + 1);
TrailingQuotesIndex = TrailingQuotesIndex == -1 ? str
.length() : TrailingQuotesIndex;
str = str.substring(leadingQuotesIndex + 1,
TrailingQuotesIndex);
str = urlHandler(str);
System.out.println(str);
list.add(str);
index += "<a ".length();
continue;
}

int whitespaceIndex = str.indexOf(" ", hrefIndex
+ "href=".length()); // 形如<a href=
// http://www.baidu.com >
whitespaceIndex = whitespaceIndex == -1 ? str.length()
: whitespaceIndex;
str = str.substring(hrefIndex + "href=".length(),
whitespaceIndex);
str = urlHandler(str);
list.add(str);
System.out.println(str);

}
index += "<a ".length();
}
}
return list;
}

public static void main(String[] args) throws Exception {
GetLinks gl = new GetLinks("http://www.baidu.com");
List<String> list = gl.getAnchorTagUrls();
for(String str:list) {
System.out.println(str);
}
}
}

与java获取url中的域名相关的知识