url对象java(js url对象)
华为云服务器特价优惠火热进行中! 2核2G2兆仅需 38 元;4核4G3兆仅需 79 元。购买时间越长越优惠!更多配置及优惠价格请咨询客服。
合作流程: |
今天给各位分享url对象java的知识,其中也会对js url对象进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!
微信号:cloud7591如需了解更多,欢迎添加客服微信咨询。
复制微信号
本文目录一览:
- 1、java构造方法URL(URL urlobj,String urlSpecifier)是什么意思?
- 2、java中如何根据一个网址获得该网页的源代码?
- 3、关于java调用url接口方法的问题
- 4、java的URL对象是不是只能是一个网站的域名,比如"http://www.baidu.com",而不能是“119.75.217.109”?
java构造方法URL(URL urlobj,String urlSpecifier)是什么意思?
有参构造:
意思是你使用这个构造方法创建对象的时候需要给他传递两个参数。一个参数是URL类型(对象本身的类型),另一个字符串类型。列如 URL rul = new URL(new URL(),"str");
源码:
public URL(URL context, String spec) throws MalformedURLException {
this(context, spec, null);
}
两个参数的构造里调用了三个参数的构造。
具体三参数的构造可以参考源码
/**
* Creates a URL by parsing the given spec with the specified handler
* within a specified context. If the handler is null, the parsing
* occurs as with the two argument constructor.
*
* @param context the context in which to parse the specification.
* @param spec the {@code String} to parse as a URL.
* @param handler the stream handler for the URL.
* @exception MalformedURLException if no protocol is specified, or an
* unknown protocol is found, or {@code spec} is {@code null}.
* @exception SecurityException
* if a security manager exists and its
* {@code checkPermission} method doesn't allow
* specifying a stream handler.
* @see java.net.URL#URL(java.lang.String, java.lang.String,
* int, java.lang.String)
* @see java.net.URLStreamHandler
* @see java.net.URLStreamHandler#parseURL(java.net.URL,
* java.lang.String, int, int)
*/
public URL(URL context, String spec, URLStreamHandler handler)
throws MalformedURLException
{
String original = spec;
int i, limit, c;
int start = 0;
String newProtocol = null;
boolean aRef=false;
boolean isRelative = false;
// Check for permission to specify a handler
if (handler != null) {
SecurityManager sm = System.getSecurityManager();
if (sm != null) {
checkSpecifyHandler(sm);
}
}
try {
limit = spec.length();
while ((limit 0) (spec.charAt(limit - 1) = ' ')) {
limit--; //eliminate trailing whitespace
}
while ((start limit) (spec.charAt(start) = ' ')) {
start++; // eliminate leading whitespace
}
if (spec.regionMatches(true, start, "url:", 0, 4)) {
start += 4;
}
if (start spec.length() spec.charAt(start) == '#') {
/* we're assuming this is a ref relative to the context URL.
* This means protocols cannot start w/ '#', but we must parse
* ref URL's like: "hello:there" w/ a ':' in them.
*/
aRef=true;
}
for (i = start ; !aRef (i limit)
((c = spec.charAt(i)) != '/') ; i++) {
if (c == ':') {
String s = spec.substring(start, i).toLowerCase();
if (isValidProtocol(s)) {
newProtocol = s;
start = i + 1;
}
break;
}
}
// Only use our context if the protocols match.
protocol = newProtocol;
if ((context != null) ((newProtocol == null) ||
newProtocol.equalsIgnoreCase(context.protocol))) {
// inherit the protocol handler from the context
// if not specified to the constructor
if (handler == null) {
handler = context.handler;
}
// If the context is a hierarchical URL scheme and the spec
// contains a matching scheme then maintain backwards
// compatibility and treat it as if the spec didn't contain
// the scheme; see 5.2.3 of RFC2396
if (context.path != null context.path.startsWith("/"))
newProtocol = null;
if (newProtocol == null) {
protocol = context.protocol;
authority = context.authority;
userInfo = context.userInfo;
host = context.host;
port = context.port;
file = context.file;
path = context.path;
isRelative = true;
}
}
if (protocol == null) {
throw new MalformedURLException("no protocol: "+original);
}
// Get the protocol handler if not specified or the protocol
// of the context could not be used
if (handler == null
(handler = getURLStreamHandler(protocol)) == null) {
throw new MalformedURLException("unknown protocol: "+protocol);
}
this.handler = handler;
i = spec.indexOf('#', start);
if (i = 0) {
ref = spec.substring(i + 1, limit);
limit = i;
}
/*
* Handle special case inheritance of query and fragment
* implied by RFC2396 section 5.2.2.
*/
if (isRelative start == limit) {
query = context.query;
if (ref == null) {
ref = context.ref;
}
}
handler.parseURL(this, spec, start, limit);
} catch(MalformedURLException e) {
throw e;
} catch(Exception e) {
MalformedURLException exception = new MalformedURLException(e.getMessage());
exception.initCause(e);
throw exception;
}
}
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("", "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);
}
}
}
根据具体问题类型,进行步骤拆解/原因原理分析/内容拓展等。
具体步骤如下:/导致这种情况的原因主要是……

关于java调用url接口方法的问题
对于具体的数据请求方式、请求方式、响应数据格式要看你的接口要求,这是通用代码:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
public class Test {
public static void main(String[] args) throws IOException {
System.out.println(getData());
}
public static String getData() throws IOException {
// 创建指定url的url对象,这里的地址是:淘宝商品搜索建议
URL url = new URL(";q=电脑callback=cb");
// 创建http链接对象
HttpURLConnection con = (HttpURLConnection) url.openConnection();
// 设置请求方式
con.setRequestMethod("POST");
// 打开链接,上一步和该步骤作用相同,可以省略
con.connect();
// 获取请求返回内容并设置编码为UTF-8
BufferedReader reader = new BufferedReader(new InputStreamReader(con.getInputStream(), "UTF-8"));
// 将返回数据拼接为字符串
StringBuffer sb = new StringBuffer();
// 临时字符串
String temp = null;
// 获取数据
while ((temp = reader.readLine()) != null) {
sb.append(temp);
}
// 关闭流
reader.close();
return sb.toString();
}
}
结果:
java的URL对象是不是只能是一个网站的域名,比如"http://www.baidu.com",而不能是“119.75.217.109”?
URL myWeb=new URL(" ");
"" 可以写成""
ip比域名更快点(省去域名解析)
我的猜想是:你的URL格式没写正确。
url对象java的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于js url对象、url对象java的信息别忘了在本站进行查找喔。
