1、获取主机信息

@Test
public void GetDomainInfo() throws UnknownHostException {
String domain = "www.baidu.com";
InetAddress netAddress = InetAddress.getByName(domain);
// 获取主机名
System.out.println(netAddress.getHostName());
// IP地址
System.out.println(netAddress.getCanonicalHostName()); InetAddress ip2domain = InetAddress.getByAddress(new byte[] {
(byte) 115, (byte) 239, (byte) 210, (byte) 26 }); System.out.println(ip2domain.getCanonicalHostName());
System.out.println(ip2domain.getHostName()); /*
* 输出: www.baidu.com 115.239.210.26 115.239.210.26 115.239.210.26
*/
}

  2、URLEncode 跟 URLDecoder

// URLDecoder和URLEncoder
@SuppressWarnings("deprecation")
@Test
public void URLEncode() {
String result = URLEncoder.encode("我是Rhythmk,你呢?");
System.out.println(result);
// 输出:
// %E6%88%91%E6%98%AFRhythmk%2C%E4%BD%A0%E5%91%A2%EF%BC%9F
} @Test
public void URLDecoder() {
String result = java.net.URLDecoder
.decode("%E6%88%91%E6%98%AFRhythmk%2C%E4%BD%A0%E5%91%A2%EF%BC%9F");
System.out.println(result);
/*
* 输出: 我是Rhythmk,你呢?
*/ }

  

3、POST GET 请求

	@Test
public void Get() throws IOException { URLConnection urlConnect;
URL url = new URL("http://www.baidu.com");
urlConnect = url.openConnection();
BufferedReader biStream = null;
try { urlConnect.connect();
Map<String, List<String>> map = urlConnect.getHeaderFields();
for (String key : map.keySet()) {
List<String> listKey = map.get(key);
for (String k : listKey) {
System.out.println("*****************" + k);
} } biStream = new BufferedReader(new InputStreamReader(
urlConnect.getInputStream()));
String html = "";
String line = null;
while ((line = biStream.readLine()) != null) {
html += line;
}
System.out
.println("************************* HTML 内容 ***********************");
System.out.println(html); } catch (Exception e) {
e.printStackTrace();
} finally {
if (biStream != null) {
biStream.close();
}
} } @Test
public void Post() throws IOException { BufferedReader biStream = null;
try { URL url = new URL("http://passport.rhythmk.com/signin"); URLConnection urlConnect = url.openConnection();
// 设置通用的请求属性
urlConnect.setRequestProperty("accept", "*/*");
urlConnect.setRequestProperty("connection", "Keep-Alive");
urlConnect.setRequestProperty("user-agent",
"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)"); // 发送POST请求必须设置如下两行 ,需要在connect 之前设定好
urlConnect.setDoOutput(true);
urlConnect.setDoInput(true);
urlConnect.connect(); // 获取URLConnection对象对应的输出流
PrintWriter out = new PrintWriter(urlConnect.getOutputStream());
// 发送请求参数
out.print("returnUrl=http%3A%2F%2Fwww.ciwong.com%2F&username=a&password=d");
// flush输出流的缓冲
out.flush(); biStream = new BufferedReader(new InputStreamReader(
urlConnect.getInputStream()));
String html = "";
String line = null;
while ((line = biStream.readLine()) != null) {
html += line;
}
System.out
.println("************************POST* HTML 内容 ***********************");
System.out.println(html);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (biStream != null) {
biStream.close();
}
} }

  注意:

如果先

urlConnect.connect();

然后如下设置

urlConnect.setDoOutput(true);
urlConnect.setDoInput(true);

导致异常:

java.lang.IllegalStateException: Already connected

at java.net.URLConnection.setDoOutput(URLConnection.java:849)
at com.rhythmk.filedemo.net_demo1.Post(net_demo1.java:126) ....

Rhythmk 一步一步学 JAVA (22) JAVA 网络编程的更多相关文章

  1. Java学习之网络编程实例

    转自:http://www.cnblogs.com/springcsc/archive/2009/12/03/1616413.html 多谢分享 网络编程 网络编程对于很多的初学者来说,都是很向往的一 ...

  2. Java进阶之网络编程

    网络编程 网络编程对于很多的初学者来说,都是很向往的一种编程技能,但是很多的初学者却因为很长一段时间无法进入网络编程的大门而放弃了对于该部分技术的学习. 在 学习网络编程以前,很多初学者可能觉得网络编 ...

  3. Java学习:网络编程总结

    Java网络编程总结 一.概述 计算机网络是通过传输介质.通信设施和网络通信协议,把分散在不同地点的计算机设备互连起来,实现资源共享和数据传输的系统.网络编程就就是编写程序使联网的两个(或多个)设备( ...

  4. 2018-2019-2-20175323 java实验五 网络编程与安全

    20175323 java实验五 网络编程与安全 任务一 ①编写MyBC.java实现中缀表达式转后缀表达式的功能 ②编写MyDC.java实现从上面功能中获取的表达式中实现后缀表达式求值的功能 基本 ...

  5. 黑马程序员:Java基础总结----网络编程

    黑马程序员:Java基础总结 网络编程   ASP.Net+Android+IO开发 . .Net培训 .期待与您交流! 网络编程 网络通讯要素 . IP地址 . 网络中设备的标识 . 不易记忆,可用 ...

  6. 第84节:Java中的网络编程(中)

    第84节:Java中的网络编程(中) 实现客户端和服务端的通信: 客户端需要的操作,创建socket,明确地址和端口,进行键盘录入,获取需要的数据,然后将录入的数据发送给服务端,为socket输出流, ...

  7. 第78节:Java中的网络编程(上)

    第78节:Java中的网络编程(上) 前言 网络编程涉及ip,端口,协议,tcp和udp的了解,和对socket通信的网络细节. 网络编程 OSI开放系统互连 网络编程指IO加网络 TCP/IP模型: ...

  8. 第62节:探索Java中的网络编程技术

    前言 感谢! 承蒙关照~ 探索Java中的网络编程技术 网络编程就是io技术和网络技术的结合,网络模型的定义,只要共用网络模型就可以两者连接.网络模型参考. 一座塔有七层,我们需要闯关. 第一层物理层 ...

  9. java第九节 网络编程的基础知识

    /** * * 网络编程的基础知识 * 网络协议与TCP/IP * IP地址和Port(端口号) * 本地回路的IP地址:127.0.0.1 * 端口号的范围为0-65535之间,0-1023之间的端 ...

  10. 20165324 Java实验五 网络编程与安全

    20165324 Java实验五 网络编程与安全 一.实验报告封面 课程:Java程序设计 班级:1653班 姓名:何春江 学号:20165324 指导教师:娄嘉鹏 实验日期:2018年5月28日 实 ...

随机推荐

  1. ajax方法携带授权标识

    $.ajax({ type: "post", url: "/api/login", data: { username: getusername, passwor ...

  2. 050——VUE中使用js库控制vue过渡动作

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  3. HDU 1069 Monkey and Banana 基础DP

    题目链接:Monkey and Banana 大意:给出n种箱子的长宽高.每种不限个数.可以堆叠.询问可以达到的最高高度是多少. 要求两个箱子堆叠的时候叠加的面.上面的面的两维长度都严格小于下面的. ...

  4. delphi7完全关闭一个窗体

    如果一个工程中有若干个form,在程序运行中若要彻底关闭其中的一个窗体 除了点击右上角的小叉叉外,也可以在form的close事件中添加一句话 procedure TLockScreen.FormCl ...

  5. ASPX 后台调用前台Js

    1.UpdatePanel 使用中 protected void Button1_Click(object sender, EventArgs e) { this.Label1.Text = &quo ...

  6. 2018.12.21 Cmos- RF

    1 PSK 趋肤效应 50欧匹配 smith s参数 :在datasheet里面会有 Information coding Bluetooth : Continous Variable Slope D ...

  7. javascript 与vbscript 互相调用

    在 VBScript 的function 中可以直接编写 javascript 代码 但是只能返回一个字符串给变量 <html><head> <script type=& ...

  8. Winform开发之SqlCommand常用属性和方法

    SqlCommand类表示要对 SQL Server 数据库执行的一个 Transact-SQL 语句或存储过程,有若干个属性和若干个方法,具体的各类方法使用可以从msdn上找到. 这里介绍几个常用东 ...

  9. ieee80211 phy1: Failed to select rate control algorithm

    /************************************************************************ * ieee80211 phy1: Failed t ...

  10. BZOJ3298: [USACO 2011Open]cow checkers(佐威夫博弈)

    3298: [USACO 2011Open]cow checkers Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 195  Solved: 96[S ...