如何用java写一个12306抢票小程序
时间: 2024-05-29 10:03:43 浏览: 150
12306抢票程序JAVA版.zip
写一个12306抢票小程序,需要以下步骤:
1. 登录12306网站并获取会话cookie
2. 查询车票信息并获取列车信息
3. 提交订单并获取订单信息
4. 验证订单信息并确认订单
5. 完成支付流程
下面是一个简单的示例程序:
```java
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
public class TrainTicket {
private static final String LOGIN_URL = "https://kyfw.12306.cn/otn/login/init";
private static final String QUERY_URL = "https://kyfw.12306.cn/otn/leftTicket/query";
private static final String SUBMIT_URL = "https://kyfw.12306.cn/otn/leftTicket/submitOrderRequest";
private static final String CHECK_URL = "https://kyfw.12306.cn/otn/confirmPassenger/checkOrderInfo";
private static final String PAY_URL = "https://kyfw.12306.cn/otn/pay/payGateway";
private static final Map<String, String> headers = new HashMap<>();
private static final String USER_AGENT = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36";
public static void main(String[] args) throws Exception {
// 登录12306网站并获取会话cookie
String cookie = login();
// 查询车票信息并获取列车信息
String trainInfo = queryTrain(cookie);
// 提交订单并获取订单信息
String orderInfo = submitOrder(cookie, trainInfo);
// 验证订单信息并确认订单
String token = checkOrder(cookie, orderInfo);
// 完成支付流程
payOrder(cookie, token);
}
private static String login() throws Exception {
URL url = new URL(LOGIN_URL);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setRequestProperty("User-Agent", USER_AGENT);
BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line;
String cookie = null;
while ((line = in.readLine()) != null) {
if (line.startsWith("Set-Cookie:")) {
cookie = line.split(";")[0].split(": ")[1];
}
}
in.close();
return cookie;
}
private static String queryTrain(String cookie) throws Exception {
String date = "2022-01-01";
String from = "BJP";
String to = "SHH";
URL url = new URL(QUERY_URL + "?" + "leftTicketDTO.train_date=" + date +
"&leftTicketDTO.from_station=" + from +
"&leftTicketDTO.to_station=" + to +
"&purpose_codes=ADULT");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setRequestProperty("User-Agent", USER_AGENT);
conn.setRequestProperty("Cookie", cookie);
BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line;
StringBuilder result = new StringBuilder();
while ((line = in.readLine()) != null) {
result.append(line);
}
in.close();
return result.toString();
}
private static String submitOrder(String cookie, String trainInfo) throws Exception {
// TODO: 提交订单
return null;
}
private static String checkOrder(String cookie, String orderInfo) throws Exception {
// TODO: 验证订单信息并确认订单
return null;
}
private static void payOrder(String cookie, String token) throws Exception {
// TODO: 完成支付流程
}
}
```
注意,这只是一个简单的示例程序,实际的抢票流程可能比这个复杂得多。此外,12306网站有反爬虫机制,需要注意一些细节,比如请求频率等。
阅读全文