1

Well the best vote for adding parameters to HttpURLConnection is in this post

But the answer of it is explain about how to adding parameter that have the form something like this --> "username=usernameValue?password=passwordValue"<--

REMEMBER the parameter can have the form like JSON Object and the answer form the link can make you lost of direction if you dont know about JSON Parameter for POST.

Then this is the best answer from me that i can provide.

     URL url = new URL("http://yoururl.com");
     HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();
     conn.setReadTimeout(10000);
     conn.setConnectTimeout(15000);
     conn.setRequestMethod("POST");
     conn.setDoInput(true);
     conn.setDoOutput(true);

     JSONObject urlParameter = new JSONObject();
     urlParameter.put("username", usernameValue);
     urlParameter.put("password", passwordValue);


     OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
     wr.write(urlParameter.toString());
     wr.flush();
     wr.close();

     int responseCode = conn.getResponseCode();
     StringBuilder sb;
     sb = new StringBuilder();
     if (responseCode == HttpURLConnection.HTTP_OK) {
        String line;
        BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()));
        while ((line = br.readLine()) != null) {
           sb.append(line).append("\n");
        }
        } else {
           System.out.println(conn.getResponseMessage());
        }
        String result = sb.toString();

Please enlighten me if you have better solution. TY

Community
  • 1
  • 1

0 Answers0