一 通过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();
}
}
|
本文转自 pangfc 51CTO博客,原文链接:http://blog.51cto.com/983836259/1716705,如需转载请自行联系原作者