我要完成功能是模拟登录久游网
https://passport.9you.com/login.php
说明:
a) 根据 https://login.passport.9you.com/identifyingCode.jsp 下载图片并记录他的cookie 这个值要填入下一个请求的地址中
b) 组织以下数据并提交的https://login.passport.9you.com/checkCode
这个时候X和Y的值无法确定,也不能随便填一个
我的QQ19739257 可以随时联系我
以下的可执行代码,运行的时候在c盘下面建立文件夹CheckCode 验证码放到这个文件夹里
import java.awt.image.BufferedImage;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.File;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.Scanner;
import javax.imageio.ImageIO;
import javax.net.ssl.HttpsURLConnection;
public class tempClass {
public static void Login() {
try {
// 下载验证码到本地
URL url = new URL("https://login.passport.9you.com/identifyingCode.jsp");
HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
connection.setUseCaches(false);
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setConnectTimeout(5000);
connection.setReadTimeout(10000);
((HttpURLConnection) connection).setRequestMethod("POST");
connection.setRequestProperty("User-Agent",
"Mozilla/4.0 (compatible; MSIE 6.0; Windows 2000)");
connection.connect();
String cookies = connection.getHeaderField("Set-Cookie");
if (cookies.indexOf(";") != -1) {
cookies = cookies.substring(0, cookies.indexOf(";"));
}
File imgCodeFile = new File("c:\\CheckCode\\"
+ System.currentTimeMillis() + ".gif");
BufferedImage image = ImageIO.read(connection.getInputStream());
ImageIO.write(image, "gif", imgCodeFile);
String checkCode = GetCheckCode();
System.out.println(checkCode);
// 登录
//隐藏参数
String id = "SSO_PAY";
String s = "5384a672b08ac3e96ad534ac67e30442";
String userIp = "60.191.73.11";//这个是我的ip地址 到时候根据你自己的ip来填写 可以从form里查看值
String nextUrl = "http://pay.9you.com/funpay/checkstat.php";
nextUrl = URLEncoder.encode(nextUrl, "GBK");
//用户要输入值
String userName = "jiuyoumoni";
String password = "111111";
String identifyingCode = checkCode;
//生成地址https://login.passport.9you.com/checkCode?id=SSO_PAY&s=5384a672b08ac3e96ad534ac67e30442&userIp=60.191.73.11&userName=zhao88zhao8&password=458458&identifyingCode=5pre
String paramStr = "?" + "id=" + id + "&s=" + s + "&userIp=" + userIp + "&userName=" + userName + "&password=" + password + "&identifyingCode=" + identifyingCode;
String loginUrl = "https://login.passport.9you.com/checkCode" + paramStr;
System.out.println("请求地址:" + loginUrl);
//根据
url = new URL("https://login.passport.9you.com/checkCode");
connection = (HttpsURLConnection) url.openConnection();
connection.setUseCaches(false);
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setConnectTimeout(20000);
connection.setReadTimeout(20000);
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type",
"application/x-www-form-urlencoded");
connection.setRequestProperty("Content-Length", String.valueOf(paramStr.length()));
connection.setRequestProperty("Cookie", cookies);
connection.setRequestProperty("User-Agent",
"Mozilla/4.0 (compatible; MSIE 6.0; Windows 2000)");
connection.connect();
DataOutputStream dos = new DataOutputStream(connection.getOutputStream());
dos.writeBytes(paramStr);
dos.flush();
dos.close();
int res = connection.getResponseCode();
if (res == 200) {
BufferedReader in = new BufferedReader(new InputStreamReader(
connection.getInputStream(), "GBK"));
String retVal;
while ((retVal = in.readLine()) != null) {
System.out.println(retVal);
}
}
} catch (Exception e) {
System.out.println("code error");
}
}
private static String GetCheckCode() {
Scanner sc = new Scanner(System.in);
System.out.print("验证码在C:\\ImageCode目录下 ,请你查看并输入:");
String checkCode = sc.next();
System.out.println("您输入的验证码是:" + checkCode);
return checkCode;
}
public static void main(String[] args) {
tempClass.Login();
}
}