2015-02-19 3 views
2

Я пытаюсь разработать небольшое приложение на основе Java-GUI, выбирая капчу из моего URL-адреса академиков, спрашивая пользователя о его имени пользователя, пароле и captcha, показывая контент после входа в систему. Однако я застрял в войти непосредственно на странице, как после подачи форме ответа от сети являетсяПолучение изображения Captcha с помощью jsoup

alert('Please enter correct code.'); window.history.go(-1); 

Код

public Map cookies; 
public void downloadCaptcha()throws Exception { 
Connection.Response response = Jsoup.connect("https://academics.ddn.upes.ac.in/upes/") 
.timeout(300000) 
.userAgent("Mozilla/5.0") 
.method(Connection.Method.GET).execute(); 
cookies = response.cookies(); 
Connection.Response resultImageResponse = Jsoup.connect("https://academics.ddn.upes.ac.in/upes/modules/create_image.php") 
.cookies(cookies) 
.ignoreContentType(true) 
.method(Connection.Method.GET).timeout(30000).execute(); 
FileOutputStream out = (new FileOutputStream(new java.io.File("F:\\abc.jpg"))); 
out.write(resultImageResponse.bodyAsBytes()); 
out.close(); 
System.out.println("Captcha Fetched"); 

}

После загрузки Captcha

public static void getData(String captacha)throws Exception{ 
Connection.Response response = Jsoup.connect("https://academics.ddn.upes.ac.in/upes/index.php") 
.userAgent("Mozilla/5.0") 
.cookies(cookies) 
.data("username",username) 
.data("passwd",password) 
.data("txtCaptcha",captacha) 
.data("submit","Login") 
.data("option","login") 
.data("op2","login") 
.data("lang","english") 
.data("return","https://academics.ddn.upes.ac.in/upes/index.php?option=com_content&task=view&id=53&Itemid=6420") 
.data("message","0") 
.data("j1643f05a0c7fc7910424fb3fc4fbbb6f","1") 
.timeout(0) 
.method(Connection.Method.POST) 
.execute(); 
cookies = response.cookies(); 
System.out.println(response.cookies()); 
Document doc= response.parse(); 
FileWriter fr = new FileWriter("F:\\response.html"); 
PrintWriter pw= new PrintWriter(fr); 
pw.println(doc.toString()); 
pw.close(); 
fr.close(); 
} 

resonse.cookies() дает OUTPUT {PHPSESSID=ai0r017bmb55gv0m4ikeu6jfc6, 61c78a27855d239ae8682ff6befaa989=5ae2e5baf548bc293c943d3416e7d400}

Сайт https://academics.ddn.upes.ac.in/upes/index.php

Пожалуйста, укажите на мои ошибки.

+0

привет fonkap пожалуйста, ответьте мне, у меня есть такая же проблема http://stackoverflow.com/questions/34549600/how-to-login-an-aspx-page -with-security-image-capture-field-using-jsoup – Farenhite

ответ

2

Вам необходимы два изменения для вашего кода для работы:

1 - Вы должны забрать печенье, возвращаемое второго вызовом (загрузка образа) и добавить его к предыдущему печенье.

2 - Если вы видите поле «j1643f05a0c7fc7910424fb3fc4fbbb6f», очень подозрительно, на самом деле это поле переменной, вам нужно будет выбрать скрытый ввод в форме и использовать его.

3 (за дополнительную плату) - Это не тот случай, но некоторые серверы жалуются, если не отправить некоторые заголовки, как Accept, Accept-Encoding, Accept-Language ...

Когда я использую свой код те изменения, которые я получаю:

<script>alert('Incorrect username or password. Please try again.'); window.history.go(-1);</script> 

Конечно, у меня нет пользователя/прохода, я думаю, вы получите нужную страницу.

код с neccesary изменений:

public class SO_28619161 { 


    public Map cookies; 
    private String username = "u"; 
    private String password = "p"; 

    public HashMap<String,String> downloadCaptcha()throws Exception { 
     Connection.Response response = Jsoup.connect("https://academics.ddn.upes.ac.in/upes/") 
       .timeout(300000) 
       .userAgent("Mozilla/5.0") 
       .method(Connection.Method.GET).execute(); 

     //nice 
     cookies = response.cookies(); 

     //now we will load form's inputs 
     Document doc = response.parse(); 
     Elements fields = doc.select("form input"); 
     HashMap<String,String> formFields = new HashMap<String, String>(); 
     for (Element field : fields){ 
      formFields.put(field.attr("name"), field.attr("value")); 
     } 

     Connection.Response resultImageResponse = Jsoup.connect("https://academics.ddn.upes.ac.in/upes/modules/create_image.php") 
       .cookies(cookies) 
       .ignoreContentType(true) 
       .method(Connection.Method.GET).timeout(30000).execute(); 

     //we will need these cookies also! 
     cookies.putAll(resultImageResponse.cookies()); 

     FileOutputStream out = (new FileOutputStream(new java.io.File("abc.jpg"))); 
     out.write(resultImageResponse.bodyAsBytes()); 
     out.close(); 

     System.out.println("Captcha Fetched"); 

     return formFields; 
    } 

    public void getData(HashMap<String, String> formFields) throws Exception{ 
     Connection conn = Jsoup.connect("https://academics.ddn.upes.ac.in/upes/index.php") 
       .userAgent("Mozilla/5.0 (Windows NT 6.1; WOW64; rv:35.0) Gecko/20100101 Firefox/35.0") 
       //not neccesary but these extra headers won't hurt 
       .header("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8") 
       .header("Accept-Encoding", "gzip, deflate") 
       .header("Accept-Language", "es-ES,es;q=0.8,en-US;q=0.5,en;q=0.3") 
       .header("Host", "academics.ddn.upes.ac.in") 
       .header("Referer", "https://academics.ddn.upes.ac.in/upes/index.php") 
       .cookies(cookies) 
       .timeout(0) 
       .method(Connection.Method.POST); 

     //we send the fields 
     conn.data(formFields); 

     Response response = conn.execute(); 
     cookies = response.cookies(); 
     System.out.println(response.cookies()); 
     Document doc= response.parse(); 
     FileWriter fr = new FileWriter("response.html"); 
     PrintWriter pw= new PrintWriter(fr); 
     pw.println(doc.toString()); 
     System.out.println(doc.toString()); 
     pw.close(); 
     fr.close(); 
    } 

    private void run() throws Exception, IOException { 
     HashMap<String, String> formFields = downloadCaptcha(); 

     BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 
     String captcha = br.readLine(); 

     //we set user/pass and captcha 
     formFields.put("username", username); 
     formFields.put("passwd", password); 
     formFields.put("txtCaptcha", captcha); 

     getData(formFields); 
    } 

    public static void main(String[] args) throws Exception { 
     SO_28619161 main = new SO_28619161(); 
     main.run(); 
    } 

} 
+0

Спасибо !!! Я также могу получить правильный код с кодом. Хотя я сам смог ее решить. Ошибка была в том, что я искал неправильный код. Лучшая часть вашего ответа заключается в том, что комментарий - «не обязательно, но эти дополнительные заголовки не повредят» :) Отмечено как ответ. – Aviverma

+0

И да, что «j1643f05a0c7fc7910424fb3fc4fbbb6f» очень подозрительно. Мне очень больно! : '( – Aviverma

Смежные вопросы