gpt4 book ai didi

java - 从桌面应用程序登录网站

转载 作者:行者123 更新时间:2023-11-30 11:37:20 24 4
gpt4 key购买 nike

我想从我的 Java 桌面应用程序登录网站。我必须使用 OutputStreamWriter 编写 Username & Password。我能够从我的应用程序成功登录到其他网站,但不是我真正想要的网站。在分析该网站的页面源后,我发现 Username & password 这两个文本框的 ID 会随着每次请求和每次页面刷新而变化。

<input type="text" name="UserName_88515" id="UserName_88515" />

id 中,最后 5 位数字每次都会更改,所以我决定读取页面源代码,检索这五位数字,然后写入凭据以登录该网站。

public class LoginHandler {

static boolean isLoggedIn = false;
static String responseText, myText;

public void login(String usrname, String password, String cookys, String sessionCode) {

try {

URL url = new URL("http://www.somewebsite.com/home.php?session="
+ sessionCode);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setDoOutput(true);
conn.setRequestMethod("GET");
conn.setRequestProperty("User-Agent",
"Mozilla/5.0 (Windows NT 6.1; WOW64; rv:17.0) Gecko/20100101 Firefox/17.0");
conn.setRequestProperty("Cookie", cookys);
conn.setDoOutput(true);

final BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));

final StringBuilder response = new StringBuilder();
myText = response.toString();
String line;

while ((line = rd.readLine()) != null) {
response.append(line);
}
rd.close();

final String text1 = response.toString();
final int starIndex = text1.indexOf("UserName_");
final int endIndex = starIndex + 15;
System.err.println("This is starIndex" + starIndex);
System.err.println("This is endIndex" + endIndex);
final String avc = text1.substring(starIndex, endIndex);
System.err.println("This is avc\n" + avc);
final String fin = avc.substring(10, 15);
System.err.println("This is fin\n" + fin);

final String data1 = URLEncoder.encode("MessageLength", "UTF-8")
+ "=" + URLEncoder.encode("140", "UTF-8") + "&"
+ URLEncoder.encode("UserName_" + fin, "UTF-8") + "="
+ URLEncoder.encode("username", "UTF-8") + "&"
+ URLEncoder.encode("Password_" + fin, "UTF-8") + "="
+ URLEncoder.encode("password", "UTF-8") + "&"
+ URLEncoder.encode("LoginNowbtnDiv", "UTF-8") + "="
+ URLEncoder.encode("Login Now", "UTF-8") + "&"
+ URLEncoder.encode("LoginNow", "UTF-8") + "="
+ URLEncoder.encode("Login Now", "UTF-8");

System.err.println("THIS IS Data1:\n " + data1);

final OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
wr.write(data1);
wr.flush();

final BufferedReader rd1 = new BufferedReader(new InputStreamReader(conn.getInputStream()));

final StringBuilder response1 = new StringBuilder();
String line1;

while ((line1 = rd1.readLine()) != null) {
response1.append(line1);
}

final String text2 = response1.toString();
System.err.println("This is second response\n" + text2);
rd1.close();
} catch (Exception ex) {
ex.printStackTrace();
}
}
}

为此,我编写了以下代码,但出现了以下错误:

Cannot write output after reading input

最佳答案

查看此问题的答案 - Cannot write output after reading input简短的版本是底层的 HttpURLConnection (conn) 不能被重用,你需要打开一个新的。

不过,一般来说,如果你为你的 http 代码使用更好的库,你会更容易,比如 HTTPComponents

关于java - 从桌面应用程序登录网站,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14088557/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com