java url api_深入阅读java api之URL

声明: 不是很有空,慢慢完善

1. 若创建的类的实例需要较大的灵活性,则通过反射来进行创建

2. 创建的类需要用指定的类载入器载入进来

3. new URL().openConnection() 若url为http类型的,则实际上就是新建一个HttpURLConnection对象

4. URLConnection对象的getInputStream()方法就是得到HttpURLConnection对象的InputStream成员

5. IO流的打开和关闭对性能影响不大,但是不应同时拥有过多IO流对象

/**

* method in class java.net.URL

*/

static URLStreamHandler getURLStreamHandler(String protocol) {

URLStreamHandler handler = (URLStreamHandler)handlers.get(protocol);

if (handler == null) {

boolean checkedWithFactory = false;

// Use the factory (if any)

if (factory != null) {

handler = factory.createURLStreamHandler(protocol);

checkedWithFactory = true;

}

// Try java protocol handler

if (handler == null) {

String packagePrefixList = null;

packagePrefixList

= java.security.AccessController.doPrivileged(

new sun.security.action.GetPropertyAction(

protocolPathProp,""));

if (packagePrefixList != "") {

packagePrefixList += "|";

}

// REMIND: decide whether to allow the "null" class prefix

// or not.

packagePrefixList += JDK_PACKAGE_PREFIX;

StringTokenizer packagePrefixIter =

new StringTokenizer(packagePrefixList, "|");

while (handler == null &&

packagePrefixIter.hasMoreTokens()) {

String packagePrefix =

packagePrefixIter.nextToken().trim();

// do not try to instantiate the JDK gopher handler

// unless the system property had been explicitly set

if (protocol.equalsIgnoreCase(GOPHER) &&

packagePrefix.equals(JDK_PACKAGE_PREFIX) &&

!enableGopher) {

continue;

}

try {

String clsName = packagePrefix + "." + protocol +

".Handler";

Class cls = null;

try {

cls = Class.forName(clsName);

} catch (ClassNotFoundException e) {

ClassLoader cl = ClassLoader.getSystemClassLoader();

if (cl != null) {

cls = cl.loadClass(clsName);

}

}

if (cls != null) {

handler =

(URLStreamHandler)cls.newInstance();

}

} catch (Exception e) {

// any number of exceptions can get thrown here

}

}

}

synchronized (streamHandlerLock) {

URLStreamHandler handler2 = null;

// Check again with hashtable just in case another

// thread created a handler since we last checked

handler2 = (URLStreamHandler)handlers.get(protocol);

if (handler2 != null) {

return handler2;

}

// Check with factory if another thread set a

// factory since our last check

if (!checkedWithFactory && factory != null) {

handler2 = factory.createURLStreamHandler(protocol);

}

if (handler2 != null) {

// The handler from the factory must be given more

// importance. Discard the default handler that

// this thread created.

handler = handler2;

}

// Insert this handler into the hashtable

if (handler != null) {

handlers.put(protocol, handler);

}

}

}

return handler;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,您可以使用 Java 的 HttpURLConnection 类来发送带有 header 和 JSON 参数的请求。以下是一个示例代码: ```java import java.io.BufferedReader; import java.io.DataOutputStream; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; public class HttpUrlConnectionExample { public static void main(String[] args) { try { URL url = new URL("http://example.com/api/endpoint"); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); // 设置请求方法为 POST connection.setRequestMethod("POST"); // 设置请求头部 connection.setRequestProperty("Content-Type", "application/json"); connection.setRequestProperty("Accept", "application/json"); connection.setRequestProperty("Authorization", "Bearer your_token_here"); // 设置可写入请求数据 connection.setDoOutput(true); // 构建 JSON 请求数据 String jsonData = "{\"name\":\"John Smith\",\"email\":\"john.smith@example.com\"}"; // 写入请求数据 DataOutputStream outputStream = new DataOutputStream(connection.getOutputStream()); outputStream.writeBytes(jsonData); outputStream.flush(); outputStream.close(); // 获取响应数据 int responseCode = connection.getResponseCode(); BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream())); String inputLine; StringBuffer response = new StringBuffer(); while ((inputLine = reader.readLine()) != null) { response.append(inputLine); } reader.close(); // 输出响应数据 System.out.println(response.toString()); } catch (Exception e) { e.printStackTrace(); } } } ``` 在上面的示例中,我们首先创建了一个 URL 对象,并使用它创建了一个 HttpURLConnection 对象。接下来,我们设置了请求方法为 POST,并设置了请求头部,包括 Content-Type、Accept 和 Authorization。然后,我们设置了可写入请求数据,并将 JSON 数据写入请求输出流。最后,我们获取响应数据并输出它。请注意,这只是一个示例代码,您需要将 URL、请求头部和 JSON 数据替换为您自己的数据。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值