URLConnection 的基本用法

这篇博客展示了如何在Java中通过代理服务器连接URL获取数据。示例代码详细解释了如何创建HTTP代理,设置代理地址和端口,并使用URLConnection类打开连接,从输入流中读取响应内容。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

添加有代理

@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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

飞翔的佩奇

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值