Rhythmk 一步一步学 JAVA (22) JAVA 网络编程

时间:2014-04-29 16:18:55   收藏:0   阅读:553

1、获取主机信息

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
@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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
// 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 请求

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
@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 网络编程,码迷,mamicode.com

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!