假设一个地址为:”http://locolhost:8080/test/”,表单提交后的action为”login.action”。
String urlString = “http://locolhost:8080/test/login.action”;
1,首先创建URLConnection对象
URL(urlString);
url.openConnection();
2,建立用于输出的连接
3,获取输出流
PrintWriter(conn.getOutputStream());
4,输出post的参数,nameValuePairs为用户名与密码的参数对
nameValuePairs.entrySet())
first = false;
out.print(‘&’);
= pair.getKey();
= pair.getValue();
out.print(name);
out.print(‘=’);
out.print(URLEncoder.encode(value,”UTF-8″));
5,获取输入流
StringBuilder();
Scanner(conn.getInputStream());
instanceof HttpURLConnection)) throw
e;
//此处为了获取错误页面,如404等
err = ((HttpURLConnection)conn).getErrorStream();
null) throw e;
Scanner(err);
6,得到输出结果
response.append(in.nextLine());
response.append(“\n”);
response.toString();