1 . http://localhost:8080/Day28_03/LoginServlet?useName=zhangsan&pwd=123这个链接是登陆之后会先显示登陆成功或者失败信息
2 . 其实和上文从网上下载数据是一个道理,只不过响应的信息不同,这个响应的信息是登陆成功提示的信息,但是从网上下载数据响应的是需要下载的数据.
3 . 步骤和上文基本一样
package com.qf.demo5;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
// get方式 返回相应信息
public class Test2 {
public static void main(String[] args) {
// 1 URL
InputStreamReader reader =null;
BufferedReader reader2 =null;
try {
URL url = new URL("http://localhost:8080/Day28_03/LoginServlet?useName=zhangsan&pwd=123");
// 2
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
// 3 设定请求方式
connection.setRequestMethod("GET");
// 4 建立连接
connection.connect();
// 5
if(connection.getResponseCode() == 200){
// 读取响应内容
reader = new InputStreamReader(connection.getInputStream(),"utf-8");
reader2 = new BufferedReader(reader);
String string = reader2.readLine();
System.out.println("服务端响应的结果"+string);
}
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
if(reader!=null){
try {
reader.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(reader2!=null){
try {
reader2.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}