導航:首頁 > 編程語言 > java取網址

java取網址

發布時間:2023-01-11 07:32:56

1. 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);
}
}
}

2. java怎麼獲得web應用的網址

比如這個路徑

http://localhost/servlet/DemoServlet?name=test
String scheme = request.getScheme();//獲取請求協議-http
int serverPort = request.getServerPort();//獲取服務埠號 -8080
String serverName = request.getServerName();//獲取服務域名(主機名) -localhost
String requestURI = request.getRequestURI();//獲取請求uri路徑 -/servlet/DemoServlet
String servletPath = request.getServletPath();//獲取servlet路徑 -/DemoServlet
String contextPath = request.getContextPath();//獲取上下文路徑 -/servlet
String queryString = request.getQueryString();//獲取uri請求參數 -/name=test
StringBuffer requestURL = request.getRequestURL();//獲取url路徑 -http://localhost/servlet/DemoServlet

3. 用java實現獲取當前網址的來路(即是訪問當前網址是從哪裡接入的)

有兩種方式:
一種是通過後台請求獲取:request.getHeader("referer");
另一種是通過腳本js獲取:document.referrer

4. java中如何根據一個網址獲得該網頁的源代碼

package test;

import java.io.BufferedReader;

import java.io.InputStreamReader;

import java.net.HttpURLConnection;

import java.net.URL;

public class HttpTest {

private String u;

private String encoding;

public static void main(String[] args) throws Exception {

HttpTest client = new HttpTest("http://www..com/", "UTF-8");

client.run();

}

public HttpTest(String u, String encoding) {

this.u = u;

this.encoding = encoding;

}

public void run() throws Exception {

URL url = new URL(u);// 根據鏈接(字元串格式),生成一個URL對象

HttpURLConnection urlConnection = (HttpURLConnection) url

.openConnection();// 打開URL

BufferedReader reader = new BufferedReader(new InputStreamReader(

urlConnection.getInputStream(), encoding));// 得到輸入流,即獲得了網頁的內容

String line; // 讀取輸入流的數據,並顯示

while ((line = reader.readLine()) != null) {

System.out.println(line);

}

}

}

5. java如何獲取瀏覽器的訪問網址及其內容

通過request倆獲取,以下是request的方法介紹:
getParameterNames():取得客戶端所發出的請求參數名稱.
getParameter():可以讓您指定請求參數名稱,以取得對應的設定值.
getServerName():請求的伺服器.
getProtocol():使用協議.
getMethod():請求方法.
getServerPort():請求埠號.
getContextPath():Context路徑.
getServletPath(): Servlet路徑.
getRequestURI():URI路徑.
getQueryString():查詢字元串.
getRemoteAddr():使用者主機IP.
getRemotePort():使用者使用埠號.

6. java正則表達式提取網址

  1. 用字元串的split方法

    var ip = '127.111.1.112:8080';

    var addr = ip.split(':')[0];

    var port = ip.split(':')[1];

  2. 用正則

    var reg=/(d{1,3}.d{1,3}.d{1,3}.d{1,3}):(d{1,4})/;

    var ip = '127.111.1.112:8080';

    var addr = ip.replace(reg,'$1');

    var port = ip.replace(reg,'$2');

  3. 還可以間接使用字元串其他的方法,或者是數組的

7. java中怎樣獲取百度短網址

HtmlParser hp=new HtmlParser("www.sina.com.cn");

閱讀全文

與java取網址相關的資料

熱點內容
怎麼升級手機android 瀏覽:922
php權威編程pdf 瀏覽:994
扣扣加密技巧 瀏覽:720
蘋果如何創建伺服器錯誤 瀏覽:495
軟考初級程序員大題分值 瀏覽:473
js壓縮視頻文件 瀏覽:578
linux如何通過命令創建文件 瀏覽:989
應用加密app還能訪問應用嘛 瀏覽:433
安卓怎麼用支付寶交違章罰款 瀏覽:665
php面向對象的程序設計 瀏覽:504
數據挖掘演算法書籍推薦 瀏覽:894
投訴聯通用什麼app 瀏覽:150
web伺服器變更ip地址 瀏覽:954
java正則表達式驗證郵箱 瀏覽:361
成熟商務男裝下載什麼軟體app 瀏覽:609
加密2h代表長度是多少厘米 瀏覽:23
拍賣程序員 瀏覽:103
電腦的圖片放在哪個文件夾 瀏覽:276
unsignedintjava 瀏覽:218
編譯器下載地址 瀏覽:44