How to send parameters via java post using Httpurlconnection?

Asked

Viewed 4,570 times

2

I am having a problem sending through a POST method in java parameters using Httpurlconnection. I’ve been trying a few ways and researched a lot, but no way it worked for me. I’ll leave the code below for you to indicate to me possible modifications that can be made.

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;

public class HttpTeste {

    private final String USER_AGENT = "Mozilla/5.0";

    public void sendPost(String url) throws Exception {

        URL obj = new URL(url);
        HttpsURLConnection con = (HttpsURLConnection) obj.openConnection();

        //add request header
        con.setRequestMethod("POST");
        con.setRequestProperty("User-Agent", USER_AGENT);
        con.setRequestProperty("Accept-Language", "en-US,en;q=0.5");

        String urlParameters = "sn=C02G8416DRJM&cn=&locale=&caller=&num=12345";

        // Send post request
        con.setDoOutput(true);
        DataOutputStream wr = new DataOutputStream(con.getOutputStream());
        wr.writeBytes(urlParameters);
        wr.flush();
        wr.close();

        int responseCode = con.getResponseCode();
        System.out.println("\nSending 'POST' request to URL : " + url);
        System.out.println("Post parameters : " + urlParameters);
        System.out.println("Response Code : " + responseCode);

        BufferedReader in = new BufferedReader(
                new InputStreamReader(con.getInputStream()));
        String inputLine;
        StringBuffer response = new StringBuffer();

        while ((inputLine = in.readLine()) != null) {
            response.append(inputLine);
        }
        in.close();

        //print result
        System.out.println(response.toString());

    }


}
  • 1

    What mistake it makes?

  • @Isac gives no error, it simply does not return me anything, when in fact it should return me the result of my post request, which in my case would be a string in JSON format.

1 answer

1

Take a look but I think q is missing in just this line in your code:

conn.setDoInput(true);

But I’ve always used this code below:

URL url = new URL(URL_STRING);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setReadTimeout(15000);
conn.setConnectTimeout(15000);
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
conn.setDoInput(true);
conn.setDoOutput(true);


OutputStream os = conn.getOutputStream();
BufferedWriter writer = new BufferedWriter(
                        new OutputStreamWriter(os, "UTF-8"));

writer.write(PARAMETROS.toString());

writer.flush();
writer.close();
os.close();
  • thanks for replying Robson, I tried your suggestion to put the conn.setDoInput(true), but it still didn’t work. Does this second code you posted get back data from a post request with parameters? What format should I put the parameters in PARAMETROS.toString() ?

  • Yes it returns data. Face with this line shows the type of data that is received in the webservice. conn.setRequestProperty("Content-Type", "application/json; charset=UTF-8"); i receive the parameters in the web service in json format. I would have to change to see the type of data you receive.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.