跨服务器登录验证(单点登录SSO)的过程和Java实现(3)

发布时间:2021-06-05

"ru=http%3A%2F%%2F3rdServices%2F50hero&login_submit=%E7%99%BB%E5%BD%95&username="

+ URLEncoder.encode(username, "UTF-8") + "&password=" + URLEncoder.encode(password, "UTF-8");

sb.append("Content-Length: " + data.getBytes("UTF-8").length + "\r\n");

sb.append("\r\n");

sb.append(data);

OutputStream os = socket.getOutputStream();

os.write(sb.toString().getBytes("UTF-8"));

os.flush();

InputStream is = socket.getInputStream();

BufferedReader reader = new BufferedReader(new

InputStreamReader(is, "UTF-8")); // 读取结果

String line;

StringBuilder cookieSb = new StringBuilder();

int index;

while ((line = reader.readLine()) != null) {

if (line.startsWith("Set-Cookie:")) {

if (!line.contains("=deleted;")) {

index = line.indexOf(";");

if (index > 12) {

cookieSb.append(line.substring(12, index + 1)); }

}

} else if (line.startsWith("location:")) {

if (line.contains("error_code")) {

return null;

}

}

}

is.close();

reader.close();

return cookieSb.toString();

} finally {

socket.close();

}

}

private static String getLogin2(String cookie) throws Exception { Socket socket = new Socket("", 80);

try {

StringBuilder sb = new StringBuilder();

sb.append("GET

/signin?ak=50hero&ru=http%3A%2F%%2Fpassport.php

精彩图片

热门精选

大家正在看