簡體   English   中英

從桌面應用程序登錄網站

[英]Log in to website from desktop application

我想從Java桌面應用程序登錄網站。 我必須使用OutputStreamWriter編寫UsernamePassword 我可以從我的應用程序成功登錄到其他網站,但不能真正登錄。 在分析了該網站的頁面源之后,我發現Username & password這兩個文本框的ID隨每次請求和每次頁面刷新而改變。

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

id ,最后5位數字每次都會更改,因此我決定閱讀頁面源代碼,檢索這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

查看此問題的答案- 讀取輸入后無法寫輸出。簡短的版本是,基礎HttpURLConnection(conn)無法重用,您需要打開一個新的。

通常,如果對HTTP代碼使用更好的庫(例如HTTPComponents) ,則對yuo來說會更容易

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM