|
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
public class Test {
public static void main(String[] args) {
String email = "****";
String password = "*****";
String point = "12";
String sessionId = "";
try {
URL url = new URL("**********");
URLConnection urlC = url.openConnection();
HttpURLConnection httpUrlConnection = (HttpURLConnection) urlC;
// 设置是否向httpUrlConnection读入
httpUrlConnection.setDoInput(true);
// 设置是否向httpUrlConnection输出
httpUrlConnection.setDoOutput(true);
// post请求时,不能使作缓存
httpUrlConnection.setUseCaches(false);
// 设定传输的内容是否是可序列化java对象
// httpUrlConnection.setRequestProperty("User-Agent",
// "Mozilla/4.0 (compatible; MSIE 8.0; Windows vista)");
httpUrlConnection.setRequestMethod("POST");
// 连接
httpUrlConnection.connect();
// String cookieVal =
// httpUrlConnection.getHeaderField("Set-Cookie");
// System.out.println(cookieVal);
// String sessionId = "";
// if(cookieVal != null){
// sessionId = cookieVal.substring(cookieVal.indexOf("=")+1,
// cookieVal.indexOf(";"));
// }
// System.out.println(sessionId);
// httpUrlConnection.setRequestProperty("Cookie", sessionId);
//httpUrlConnection.setRequestProperty("connection", "Keep-Alive");
StringBuffer sb = new StringBuffer();
//sb.append("point=" + point);
sb.append("email=" + email);
sb.append("&password=" + password);
//sb.append("&login_type=" + "1001");
// post信息
OutputStream os = httpUrlConnection.getOutputStream();
os.write(sb.toString().getBytes("utf-8"));
os.close();
BufferedReader br = new BufferedReader(new InputStreamReader(
httpUrlConnection.getInputStream()));
String str = br.readLine();
while (str != null) {
System.out.println(new String(str.getBytes(), "utf-8"));
str = br.readLine();
}
System.out.println(httpUrlConnection.getResponseCode());
System.out.println("-------------------------------------------------------");
String key = "";
if (httpUrlConnection != null) {
for (int i = 1; (key = httpUrlConnection.getHeaderFieldKey(i)) != null; i++) {
if (key.equalsIgnoreCase("set-cookie")) {
sessionId = httpUrlConnection.getHeaderField(key);
sessionId = sessionId.substring(0, sessionId.indexOf(";"));
}
}
}
httpUrlConnection.disconnect();
URL url01 = new URL("*******");
HttpURLConnection connection = (HttpURLConnection)url01.openConnection();
connection.setRequestProperty("Cookie",sessionId);
connection.setDoInput(true);
// 设置是否向httpUrlConnection输出
connection.setDoOutput(true);
// post请求时,不能使作缓存
connection.setUseCaches(false);
connection.connect();
StringBuffer sb01 = new StringBuffer();
//sb.append("point=" + point);
sb01.append("post_key=" + "aeefb46687be44ee606a58106bdbe398");
sb01.append("&mode=" + "finish");
sb01.append("&diary_title=" + "88");
sb01.append("&diary_body=" + "88");
OutputStream os01 = connection.getOutputStream();
os01.write(sb01.toString().getBytes("utf-8"));
os01.close();
BufferedReader br01 = new BufferedReader(new InputStreamReader(
connection.getInputStream()));
String str01 = br01.readLine();
while (str01 != null) {
System.out.println(new String(str01.getBytes(), "utf-8"));
str01 = br01.readLine();
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
} |
|