Java中的Get和Post请求,使用万网接口判断域名是否已被注册

人生之路不会是一帆风顺的,我们会遇上顺境,也会遇上逆境,在所有成功路上折磨你的,背后都隐藏着激励你奋发向上的动机,人生没有如果,只有后果与结果,成熟,就是用微笑来面对一切小事。

导读:本篇文章讲解 Java中的Get和Post请求,使用万网接口判断域名是否已被注册,希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com,来源:原文

一  通过Get请求验证域名是否已被注册

万网提供了一个免费的接口来查询域名是否已被注册,http://panda.www.net.cn/cgi-bin/check.cgi?area_domain=域名

比如说我们在浏览器中输入:http://panda.www.net.cn/cgi-bin/check.cgi?area_domain=aliyun.com

最后返回:

1

2

3

4

5

6

<?xml version=”1.0″ encoding=”gb2312″?>

<property>

<returncode>200</returncode>

<key>aliyun.com</key>

<original>211 : Domain exists</original>

</property>

<original>211 : Domain exists</original> 这一行中的211就是我们判断一个域名是否注册的关键所在了。通过这个接口进行查询,在这里只会返回两种状态,210或者211,210表示可以使用,211则是已经被注册了

比如我们查询:http://panda.www.net.cn/cgi-bin/check.cgi?area_domain=aliyunbaidu.com

最后返回:

1

2

3

4

5

6

<?xml version=”1.0″ encoding=”gb2312″?>

<property>

<returncode>200</returncode>

<key>aliyunbaidu.com</key>

<original>210 : Domain name is available</original>

</property>

可以看出,aliyunbaidu.com这个域名还没有被注册

下面开始贴代码,一些关键的地方已经有注释了:

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

/**

     * 判断一个域名是否已被注册,用的万网的接口

     * 这里采用的单线程,因为是免费的接口,所以线程过大容易被封IP

     * @param domain 待检测域名,如:baidu.com

     * @return 是否可以注册

     * */

    public boolean domainIsAvailable(String domain){

        boolean isAvailable = false;  //该域名是否可用

        try {

            URL url = new URL(“http://panda.www.net.cn/cgi-bin/check.cgi?area_domain=” + domain);

            HttpURLConnection connection = (HttpURLConnection) url.openConnection();

            connection.setRequestMethod(“GET”);

            connection.setConnectTimeout(10000);  //毫秒

            connection.setReadTimeout(5000);

            

            InputStream inputStream = new BufferedInputStream(connection.getInputStream());

            

            BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));

            String line = “”;  //每次读取一行数据

            String reg = “<original>(.*?)</original>”;  //正则

            while((line = reader.readLine()) != null){

                if(line.matches(reg)){

//                    System.out.println(line);

                    //只有两种状态,210表示可用,211表示不可用

                    String state = line.substring(10, 13);

                    if(“210”.equals(state))

                        isAvailable = true;                    

                }

            }

            

        }  catch (IOException e) {

            e.printStackTrace();

        }        

        return isAvailable;

    }

下面进行实例调用:

1

2

3

4

        String domain = “aliyunbaidu.com”;

        Test test = new Test();

        boolean check = test.domainIsAvailable(domain);

        System.out.println(domain + ” 是否可用:” + check);

返回结果如下:

aliyunbaidu.com 是否可用:true

二 Post请求简单实例

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

/**

     * POST请求测试

     * */

    public void POSTTest(){

        try {

            URL url = new URL(“http://localhost:8080/Demo/jsp/show_get”);

            HttpURLConnection connection = (HttpURLConnection) url.openConnection();

            connection.setRequestMethod(“POST”);

            connection.setRequestProperty(“Content-Type”, “application/x-www-form-urlencoded”);

            connection.setDoOutput(true);  //这里因为post请求有数据传输,一定要是true

            connection.setDoInput(true);

            connection.setUseCaches(false);

            

            String str = “str=aaavc”;

            OutputStream outputStream = connection.getOutputStream();

            outputStream.write(str.getBytes(“UTF-8”));

            outputStream.flush();

            outputStream.close();

            

            System.out.println(connection.getResponseCode());

            if(connection.getResponseCode() == 200){

                InputStream inputStream = connection.getInputStream();

                BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));

                StringBuffer stringBuffer = new StringBuffer();

                String line = “”;

                while((line = reader.readLine()) != null){

                    stringBuffer.append(line + “\n”);

                }

                System.out.println(stringBuffer.toString());

                

            }

                    

        } catch (MalformedURLException e) {

            e.printStackTrace();

        } catch (IOException e) {

            e.printStackTrace();

        }

    }

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/124471.html

(0)
飞熊的头像飞熊bm

相关推荐

发表回复

登录后才能评论
极客之音——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!