简体   繁体   English

如何使用HttpURLConnection通过Java中的post发送参数?

[英]How to send parameters via post in java using HttpURLConnection?

I am not able to send through a POST method in java parameters using the HttpURLConnection. 我无法使用HttpURLConnection通过Java参数中的POST方法发送。 I've been trying in a few ways and researched a lot, but no way worked for me. 我一直在尝试几种方法并进行了很多研究,但对我没有任何帮助。 I will leave the code below for you to indicate 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 = "idRoute=1";

        // 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());

    }
}

As you can see, I tried to send the parameters of my request through urlParameters , but it still did not work. 如您所见,我尝试通过urlParameters发送请求的参数,但仍然无法正常工作。 The code runs and does not acknowledge any errors, but it does not return a response that should return. 该代码将运行并且不会确认任何错误,但是不会返回应返回的响应。 I also tried to send the parameters through String urlParameters = URLEncoder.encode ("idRoute", "UTF-8") + "=" + URLEncoder.encode ("1", "UTF-8"); wr.writeBytes(urlParameters); 我还尝试通过String urlParameters = URLEncoder.encode ("idRoute", "UTF-8") + "=" + URLEncoder.encode ("1", "UTF-8"); wr.writeBytes(urlParameters); String urlParameters = URLEncoder.encode ("idRoute", "UTF-8") + "=" + URLEncoder.encode ("1", "UTF-8"); wr.writeBytes(urlParameters); But it also did not work, thank you for the collaboration of all! 但这也没有用,谢谢大家的合作!

Does it have to work with HttpURLConnection? 它必须与HttpURLConnection一起使用吗? If not and you just want to send a POST request I would recommend to use OkHttp3 如果没有,并且您只想发送POST请求,我建议使用OkHttp3

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM