Как записать капчу с помощью Jsoup android - PullRequest
0 голосов
/ 22 февраля 2019

Я новичок в Android, пытаюсь разработать небольшое приложение, получая данные с сайта, запрашивая регистрационный номер и код безопасности, отображая содержимое после отправки.Однако я застрял на странице отправки на самой странице, так как после отправки формы ответ с веб-сайта:

код сайта

<tbody>
    <tr>
        <td></td>
    </tr>
    <tr id="vehiclesearchstatus:j_id_jsp_664471437_14">
        <td id="vehiclesearchstatus:j_id_jsp_664471437_15">
            <label class="wiproStyleText">Vehicle No:</label>
        </td>
        <td id="vehiclesearchstatus:j_id_jsp_664471437_17">
            <input id="vehiclesearchstatus:regn_no1_exact" type="text"
                   name="vehiclesearchstatus:regn_no1_exact" autocomplete="off" value=""
                   class="wiproStyleinput" maxlength="10"
                   onblur="checkLength('regn_no1_exact',3)" onkeypress="return AlphaNumericOnly(event, '');" size="12" style="width:90px">
        </td>
    </tr>

    <tr>
        <td></td>
    </tr>
    <tr id="vehiclesearchstatus:j_id_jsp_664471437_19">
        <td id="vehiclesearchstatus:j_id_jsp_664471437_20">
            <label id="vehiclesearchstatus:lbl_RANDOM_NUMBER" class="wiproStyleText">Verification Code:</label>
        </td>
        <td id="vehiclesearchstatus:j_id_jsp_664471437_22">
            <img alt="capatcha" src="/nrservices/cap_img.jsp" style="width: auto; height:30px;">
            <img
                    id="vehiclesearchstatus:imageProcessBankNewImage"
                    src="/nrservices/images/refresh.bmp" onclick="window.location.reload()"
                    style="width: auto; height: 25px;"></td>
        <td id="vehiclesearchstatus:j_id_jsp_664471437_24">
            <input
                id="vehiclesearchstatus:txt_ALPHA_NUMERIC" type="text"
                name="vehiclesearchstatus:txt_ALPHA_NUMERIC" autocomplete="off" value=""
                maxlength="10" size="8" style="width:90px;">
        </td>
    </tr>
</tbody>

mycode

что я пробовал

 inner class FetchFromVahan : AsyncTask<String, Void, String>() {

    override fun doInBackground(vararg params: String): String? {
        val userAgent =
            "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.2357.130 Safari/537.36"
        var loginForm: Connection.Response? = null
        try {

     val img = document.select("img#vehiclesearchstatus:imageProcessBankNewImage").first()
                ?: throw  RuntimeException("Unable to locate image in ")

            loginForm = Jsoup.connect("https://vahan.nic.in/nrservices/faces/user/jsp/SearchStatus.jsp")
                .userAgent(userAgent)
                .ignoreContentType(true)
                .method(Connection.Method.GET)
                .execute()
        } catch (e: IOException) {
            e.printStackTrace()
        }

        loginForm = Jsoup.connect("https://vahan.nic.in/nrservices/faces/user/jsp/SearchStatus.jsp")
            .cookies(loginForm!!.cookies())
            .ignoreContentType(true)
            .data("vehiclesearchstatus:regn_no1_exact", regN)
            .data("vehiclesearchstatus:txt_ALPHA_NUMERIC", reC)
            .userAgent(userAgent)
            .method(Connection.Method.POST)
            .followRedirects(true)
            .execute()

        var document: Document? = null
        if (loginForm != null) {
            try {
                document = Jsoup.connect("https://vahan.nic.in/nrservices/faces/user/jsp/SearchStatus.jsp")
                    .cookies(loginForm.cookies())
                    .userAgent(userAgent)
                    .get()
                System.out.println(document)



            } catch (e: IOException) {
                e.printStackTrace()
            }

        }

        return document!!.toString()
    }

    override fun onPostExecute(result: String) {
        super.onPostExecute(result)
    }
}

загрузка капчи

 val imgSrc = img.absUrl("https://vahan.nic.in/nrservices/cap_img.jsp")
            val bitmaps = BitmapFactory.decodeStream(URL(imgSrc).content as InputStream)
        image.setImageBitmap(bitmaps)

 button.setOnClickListener {
        FetchFromVahan().execute()
    }
...