TestLoginWeb.java
---------------------------------------------------------------------------
package com.yitai.test;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
public class TestLoginWeb {
public static void main(String args[]) throws IOException {
URL url = new URL("http://localhost:8080/backgroundH/login.jsp");
URL url1 = new URL("http://localhost:8080/backgroundH/execute.jsp");
HttpURLConnection huc = (HttpURLConnection) url.openConnection();
// 设置允许output
huc.setDoOutput(true);
// 设置为post方式
huc.setRequestMethod("POST");
huc.setRequestProperty("user-agent", "mozilla/4.7 [en] (win98; i)");
OutputStream os = huc.getOutputStream();
os.write("name=gaolei".getBytes("gbk"));
os.close();
BufferedReader br = new BufferedReader(new InputStreamReader(huc
.getInputStream()));
huc.connect();
String line = br.readLine();
while (line != null) {
System.out.println(line);
line = br.readLine();
}
String cookieval = huc.getHeaderField("set-cookie");
System.out.println(cookieval);
String sessionid = null;
if (cookieval != null) {
sessionid = cookieval.substring(0, cookieval.indexOf(";"));
}
huc.disconnect();
huc = null;
HttpURLConnection huc1 = (HttpURLConnection) url1.openConnection();
huc1.setRequestProperty("cookie", sessionid);
// 设置允许output
huc1.setDoOutput(true);
// 设置为post方式
huc1.setRequestMethod("POST");
huc1.setRequestProperty("user-agent", "mozilla/4.7 [en] (win98; i)");
OutputStream os1 = huc1.getOutputStream();
os1.write("value=1234567890".getBytes("gbk"));
os1.close();
BufferedReader br1 = new BufferedReader(new InputStreamReader(huc1
.getInputStream()));
huc1.connect();
line = br1.readLine();
while (line != null) {
System.out.println(line);
line = br1.readLine();
}
huc1.disconnect();
}
}
---------------------------------------------------------------------------
package com.yitai.test;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
public class TestLoginWeb {
public static void main(String args[]) throws IOException {
URL url = new URL("http://localhost:8080/backgroundH/login.jsp");
URL url1 = new URL("http://localhost:8080/backgroundH/execute.jsp");
HttpURLConnection huc = (HttpURLConnection) url.openConnection();
// 设置允许output
huc.setDoOutput(true);
// 设置为post方式
huc.setRequestMethod("POST");
huc.setRequestProperty("user-agent", "mozilla/4.7 [en] (win98; i)");
OutputStream os = huc.getOutputStream();
os.write("name=gaolei".getBytes("gbk"));
os.close();
BufferedReader br = new BufferedReader(new InputStreamReader(huc
.getInputStream()));
huc.connect();
String line = br.readLine();
while (line != null) {
System.out.println(line);
line = br.readLine();
}
String cookieval = huc.getHeaderField("set-cookie");
System.out.println(cookieval);
String sessionid = null;
if (cookieval != null) {
sessionid = cookieval.substring(0, cookieval.indexOf(";"));
}
huc.disconnect();
huc = null;
HttpURLConnection huc1 = (HttpURLConnection) url1.openConnection();
huc1.setRequestProperty("cookie", sessionid);
// 设置允许output
huc1.setDoOutput(true);
// 设置为post方式
huc1.setRequestMethod("POST");
huc1.setRequestProperty("user-agent", "mozilla/4.7 [en] (win98; i)");
OutputStream os1 = huc1.getOutputStream();
os1.write("value=1234567890".getBytes("gbk"));
os1.close();
BufferedReader br1 = new BufferedReader(new InputStreamReader(huc1
.getInputStream()));
huc1.connect();
line = br1.readLine();
while (line != null) {
System.out.println(line);
line = br1.readLine();
}
huc1.disconnect();
}
}