「java中的url」Java中的url

博主:adminadmin 2023-01-27 08:12:08 655

本篇文章给大家谈谈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("");//创建资源类型

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操作异常");

}

}

}

用java怎么写URL接口

在java中,调用http请求接口,主要通过流的方式进行调用,示例接口如下:

/**

* 程序中访问http数据接口

*/

public String searchLoginService(String urlStr) {

/** 网络的url地址 */

URL url = null;

/** http连接 */

HttpURLConnection httpConn = null;

/**//** 输入流 */

BufferedReader in = null;

StringBuffer sb = new StringBuffer();

try{

url = new URL(urlStr);

in = new BufferedReader( new InputStreamReader(url.openStream(),"UTF-8") );

String str = null;

while((str = in.readLine()) != null) {

sb.append( str );

}

} catch (Exception ex) {

logger.error(ex.getMessage(), ex);

} finally{

try{

if(in!=null) {

in.close();

}

}catch(IOException ex) {

logger.error(ex.getMessage(), ex);

}

}

String result =sb.toString();

System.out.println(result);

return result;

}

如何使用java调用url接口

原贴地址

一、在java中调用url,并打开一个新的窗口 

Java代码

String url="";  

String cmd = "cmd.exe /c start " + url;   

  

try {   

 Process proc = Runtime.getRuntime().exec(cmd);   

 proc.waitFor();   

}   

catch (Exception e)   

{   

 e.printStackTrace();  

}

二、在java中调用url,后台调用。并取得返回值 

Java代码

URL U = new URL("");  

URLConnection connection = U.openConnection();  

   connection.connect();  

    

   BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));  

   String line;  

   while ((line = in.readLine())!= null)  

   {  

    result += line;  

   }  

   in.close();

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 ListString getAnchorTagUrls() {

if (webSource == null) {

System.out.println("没有网页源代码");

return null;

}

ArrayListString list = new ArrayListString();

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=

//

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("");

ListString list = gl.getAnchorTagUrls();

for(String str:list) {

System.out.println(str);

}

}

}

java中的url的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于Java中的url、java中的url的信息别忘了在本站进行查找喔。