I want to create a Java client that follows Ajax calls that are made on the website.

Asked 2 years ago, Updated 2 years ago, 60 views

Below is the Ajax code that I want to perform. The page is the website of Pusan National University and I got the data I wanted from the basic html code, but I couldn't get the information from Ajax call like the subject manual.

So when I looked for it, they said that I should put the parameter in json when parsing, so I tried to implement the code once, but it didn't work.

I don't know if I put json's data wrong or because of the url additional below.

$.ajax({
            url : "/middleware/curriculum/allocationOfPeople/searchPeriod",
            type : 'POST',
            dataType : 'json',
            data : JSON.stringify(paramMap),
            contentType : 'application/json',
            success : function(data) {
                var datas=data.dataset1;
                var isOK=datas[0].Duration;
                var url;
                var type=$("#searchType1").is(":checked");
                var paramMap = {};
                var year=$("#year").val();
                var semester=$("#semester").val();
                var subject_is=$("#subject_is").val();

                if(isOK=="Y"){
                    if(type==true){
                        var refinementAndMajor=$("#refinementAndMajor").val();
                        url="/middleware/curriculum/college/CollegeAssignInfoSearch";
                        paramMap["pName"] = [ "YEAR", "TERM","DEPTCD","CULTCD","GUBUN"];
                        if(subject_is=='3'){
                            paramMap["pValue"] = [ year,semester,"",refinementAndMajor,subject_is];
                        }else{
                            paramMap["pValue"] = [ year,semester,refinementAndMajor,"",subject_is];
                        }
                    }else{
                        var r_type_input=$("#r_type_input").val();
                        url="/middleware/curriculum/college/curriculumCollegeSubjectAssignInfoSearch";
                        paramMap["pName"] = ["YEAR", "TERM", "GUBUN"];
                        paramMap["pValue"] = [ year,semester,subject_is,r_type_input];
                    }
                }else{
                    if(type==true){
                        var refinementAndMajor=$("#refinementAndMajor").val();
                        url="/middleware/curriculum/college/CollegeSearch";
                        paramMap["pName"] = [ "YEAR", "TERM","DEPTCD","CULTCD","GUBUN"];
                        if(subject_is=='3'){
                            paramMap["pValue"] = [ year,semester,"",refinementAndMajor,subject_is];
                        }else{
                            paramMap["pValue"] = [ year,semester,refinementAndMajor,"",subject_is];
                        }
                    }else{
                        var r_type_input=$("#r_type_input").val();
                        url="/middleware/curriculum/college/curriculumCollegeSubjectSearch";
                        paramMap["pName"] = ["YEAR", "TERM", "GUBUN"];
                        paramMap["pValue"] = [ year,semester,subject_is,r_type_input];
                    }
                }
                fn_search(url,paramMap,subject_is,isOK);

            },
            error : function(data, status, er) {
                alert("error: " + data + " status: " + status + " er:" + er);
            }
        });```





Below is the Java implementation to reproduce the above ajax.

    private static String getHttpHTML_POST() {
        String url="http://e-onestop.pusan.ac.kr/menu/class/C03/C03001/";
       String sb="";  
          try {
           URL object=new URL(url);

           HttpURLConnection con = (HttpURLConnection) object.openConnection();

           con.setDoOutput(true);
           con.setDoInput(true);
           con.setRequestProperty("Content-Type", "application/json");
           con.setRequestProperty("Accept", "*/*");
           con.setRequestProperty("X-Requested-With", "XMLHttpRequest");
           con.setRequestMethod("POST");


           JSONObject data = new JSONObject();
           data.put("YEAR","2016");
           data.put("TERM", "10");
           data.put("DEPTCD","346712");
           data.put("CULTCD", "");
           data.put("GUBUN", "1");
           OutputStreamWriter wr= new OutputStreamWriter(con.getOutputStream());

           wr.write(data.toString());
           wr.flush();

           //display what returns the POST request



           int HttpResult =con.getResponseCode(); 

           if(HttpResult ==HttpURLConnection.HTTP_OK){

               BufferedReader br = new BufferedReader(new InputStreamReader(con.getInputStream(),"utf-8"));  

               String line = null;  

               while ((line = br.readLine()) != null) {  
                sb = sb+line + "\n";  
               }  

               br.close();  

               System.out.println(""+sb.toString());  

           }else{
               System.out.println(con.getResponseMessage());  
           }  
          }
          catch (Exception e) {
           e.printStackTrace();
          }
          finally {

          }

          return sb;

    }

java ajax

2022-09-22 21:54

1 Answers

Analyzing the ajax code is good, but I think it would be better to see what code actually comes out on the browser when it works.

First, turn on Chrome-Tools More-Developer Tools and leave the Network tab open. Open http://e-onestop.pusan.ac.kr/menu/class/C03/C03001/ and select the details in basic English and press Search to post to the address below.

http://e-onestop.pusan.ac.kr/middleware/curriculum/college/CollegeAssignInfoSearch

And JSON is in the following form on the payload.

{pName: ["YEAR", "TERM", "DEPTCD", "CULTCD", "GUBUN"], pValue: ["2016", "10", "", "11750", "3"]}

The response comes as follows:

{"dataset1":[{"credit":1.0,"professor number":"111694","school code":"611000,"school name":"Basic English","department name":"Professor" Jang Kyung-chul","Lecture Time":"Wednesday 09:00,"2001,"101,"University,101"College student name:"College student

The JSON format that works in the browser and the Java code you wrote look different. data.put("pName", new JSONARray("YEAR","TERM"...); should be written like this.

For your information, check out this lecture for instructions on how to use developer tools in your Chrome browser.


2022-09-22 21:54

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.