I'd like to parse the post page with Java Http URL Connection function.

Asked 2 years ago, Updated 2 years ago, 27 views

Yesterday, I asked you about the address of the moving page and how to input the parameters in order to parse the page implemented by ajax. And "GreppTod" gave me a good answer. I modified the Java code based on the above answer, but I couldn't get the dynamic part because I got java.io.IOException: Premature EOF error while reading the html code. That's why I'm writing this to get the help of the masters. Shortcut to previous question page

First of all, this is the JSON form that goes into the payload obtained from the previous answer.

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

Below is the code that modified the parameter part by looking at the json form above.


private static String getHttpHTML_POST() {
        String url="http://e-onestop.pusan.ac.kr/middleware/curriculum/college/CollegeAssignInfoSearch";
        String result="";  
          try {
           URL object=new URL(url);

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

           con.setDoOutput(true);
           con.setDoInput(true);
           con.setRequestProperty("Cache-Control", "no-cache");
           con.setRequestProperty("Content-Type", "application/json");
           con.setRequestProperty("Accept", "application/json");

           con.setRequestProperty("Accept", "*/*");
           con.setRequestProperty("X-Requested-With", "XMLHttpRequest");
           con.setRequestMethod("POST");


           JSONObject json = new JSONObject();
           JSONArray data1 = new JSONArray();
           JSONArray data2 = new JSONArray();
           data1.add("YEAR");
           data1.add("TERM");
           data1.add("DEPTCD");
           data1.add("CULTCD");
           data1.add("GUBUN");
           json.put("pName",data1);
           data2.add("2016");
           data2.add("10");
           data2.add("");
           data2.add("11750");
           data2.add("1");
           json.put("pValue",data2);

           OutputStreamWriter wr= new OutputStreamWriter(con.getOutputStream());
           wr.write(json.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;  
               line = br.readLine();

               while ((line = br.readLine())!= null) {  
                   result = result+line + "\n"; 
                   System.out.println(line+"\n");
               }  
               br.close();  
           }else{
               System.out.println(con.getResponseMessage());  
            }  
          }
          catch (Exception e) {
           e.printStackTrace();
          }
          finally {

          }

          return result;

    }

java

2022-09-22 13:14

1 Answers

If you want to simulate the post in Java, I think you should use the Apache library. here I see a way. It works well with a little modification of the code.

Go to apache and run the HttpClient and HttpCore libraries after download and run them well.

import java.util.ArrayList;
import java.util.List;
import org.apache.http.Consts;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
public class Main {

    public static void main(String[] args) {
        String url = "http://httpbin.org/post";
        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httpPost = new HttpPost(url);

        // // set the parameters
        List <NameValuePair> nvps = new ArrayList<NameValuePair>();
        nvps.add(new BasicNameValuePair("username", "edwin"));
        nvps.add(new BasicNameValuePair("password", "dodol"));

        // // set the encoding
        httpPost.setEntity(new UrlEncodedFormEntity(nvps, Consts.UTF_8));

        System.out.println("ABC");

        // // send the http request and get the http response
        try {
            HttpResponse response = httpclient.execute(httpPost);
            HttpEntity resEntity = response.getEntity();
            System.out.println(EntityUtils.toString(resEntity));
        }catch(Exception e) {
            e.printStackTrace();
        }
    }
}

The test was conducted at http://httpbin.org. Send a request to http://httpbin.org/post to return Post data.

Additional Answer

If you want to put the value as an array in one key, you can do this.

Then the request "username": [ "edwin", "abc" ] It goes out like this.


2022-09-22 13:14

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.