Rhythmk 一步一步学 JAVA (22) JAVA 网络编程
时间:2022-03-21 06:20
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 网络编程,gxlsystem