URLConnection 的基本用法

简介: URLConnection 的基本用法

添加有代理

@Test
  public void testURLConnection() {
    try {
      String urlString = "http://11.10.113.8:8080/hiapi/paas/asset/db";
      URL url = new URL(urlString); //代表了一个网址
      //首先创建HTTP代理,指定代理的地址和端口
      Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("11.10.113.8", 8080));
      /**
       * 首先打开一个连接对象
       * 可以通过这个对象,在真正发起请求之前,设置一些其它的信息
       * 比如:代理服务器等
       */
      URLConnection conn = url.openConnection(proxy);
      InputStream is = conn.getInputStream(); //获得网页的内容
      //将InputStream转换为Reader,并使用缓冲读取,提高效率,同时可以按行
      BufferedReader br = new BufferedReader(new
          InputStreamReader(is, "UTF-8"));
      String line = null;
      while ((line = br.readLine()) != null) {
        System.out.println(line);
      }
      is.close();
    } catch (Exception e) {
      e.printStackTrace();
    }
  }

没有添加代理

@Test
  public void testURLConnection() {
    try {
      String urlString = "http://66.10.113.8:8080/hiapi/paas/asset/db";
      URL url = new URL(urlString); //代表了一个网址
      URLConnection conn = url.openConnection();
      InputStream is = conn.getInputStream(); //获得网页的内容
      //将InputStream转换为Reader,并使用缓冲读取,提高效率,同时可以按行
      BufferedReader br = new BufferedReader(new
          InputStreamReader(is, "UTF-8"));
      String line = null;
      while ((line = br.readLine()) != null) {
        System.out.println(line);
      }
      is.close();
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
运行结果
Connected to the target VM, address: '127.0.0.1:58065', transport: 'socket'
Disconnected from the target VM, address: '127.0.0.1:58065', transport: 'socket'
[{"sign":"paas","deptId":103,"hostName":"66.3.125.80,66.3.125.81,66.3.125.82","port":"1521","dbId":88,"dbType":"ORACLE","dbName":"nnsptf","isCapture":null,"dbaUser":"system","dbaPwd":"oracle","dbAlias":"压测1"},{"sign":"paas","deptId":103,"hostName":"66.3.125.80,66.3.125.81,66.3.125.82","port":"1521","dbId":96,"dbType":"ORACLE","dbName":"cjecgc","isCapture":null,"dbaUser":"system","dbaPwd":"oracle","dbAlias":"测试ora"},{"sign":"paas","deptId":105,"hostName":"66.3.125.80,66.3.125.81,66.3.125.82","port":"1521","dbId":157,"dbType":"ORACLE","dbName":"uujarl","isCapture":null,"dbaUser":"system","dbaPwd":"oracle","dbAlias":"溧水库"},{"sign":"paas","deptId":105,"hostName":"66.3.125.80,66.3.125.81,66.3.125.82","port":"1521","dbId":164,"dbType":"ORACLE","dbName":"qwxutc","isCapture":null,"dbaUser":"system","dbaPwd":"oracle","dbAlias":"溧水数据库2"},{"sign":"paas","deptId":103,"hostName":"66.3.125.80,66.3.125.81,66.3.125.82","port":"1521","dbId":298,"dbType":"ORACLE","dbName":"vosrtz","isCapture":null,"dbaUser":"system","dbaPwd":"oracle","dbAlias":"zijin001"},{"sign":"paas","deptId":103,"hostName":"66.3.125.80,66.3.125.81,66.3.125.82","port":"1521","dbId":299,"dbType":"ORACLE","dbName":"pffklf","isCapture":null,"dbaUser":"system","dbaPwd":"oracle","dbAlias":"zijin002"}]
Process finished with exit code 0


相关文章
|
2月前
|
JSON 安全 算法
JAVA网络编程中的URL与URLConnection:那些你不知道的秘密!
在Java网络编程中,URL与URLConnection是连接网络资源的两大基石。本文通过问题解答形式,揭示了它们的深层秘密,包括特殊字符处理、请求头设置、响应体读取、支持的HTTP方法及性能优化技巧,帮助你掌握高效、安全的网络编程技能。
106 9
|
8月前
|
安全 搜索推荐 Java
Java的URL与URLConnection技术深度解析
Java的URL与URLConnection技术深度解析
687 0
|
7月前
|
数据采集 Java 开发者
JAVA网络编程深度探索:URL与URLConnection的精湛技艺
Java网络编程核心在于URL和URLConnection。URL是资源的唯一标识,用于定位网络资源,支持解析、编码解码及参数操作。URLConnection则实现数据交换,允许GET/POST请求,可定制请求头、设置超时,是网络交互的关键。两者结合,适用于网络爬虫等场景,深入学习能提升编程技巧并揭示网络编程奥秘。
|
8月前
|
Java
Java的url与urlconnection的使用
Java的url与urlconnection的使用
|
8月前
HttpURLConnection 的用法
HttpURLConnection 的用法
|
Java
Java:HttpURLConnection发送GET和POST请求
Java:HttpURLConnection发送GET和POST请求
267 0
|
XML 缓存 IDE
Android网络 | URL和URLConnection详解及其实战案例
Android网络 | URL和URLConnection详解及其实战案例
|
应用服务中间件
Servlet第四篇【request对象常用方法、应用】(二)
HttpServletRequest对象代表客户端的请求,当客户端通过HTTP协议访问服务器时,HTTP请求头中的所有信息都封装在这个对象中,开发人员通过这个对象的方法,可以获得客户这些信息。
111 0
Servlet第四篇【request对象常用方法、应用】(二)
|
JSON Java 数据格式
Java 基于原生HttpURLConnection ,调用GET 和 POST请求 工具类
Java 基于原生HttpURLConnection ,调用GET 和 POST请求 工具类
411 0