简体   繁体   中英

HTTP post request with data in Java

I am trying to do a simple http post request containing data, but the url I am sending the data to is returning me an error saying that the query must contain the 'code' parameter.

We have a script in Python that works. It simply does this:

import requests

build_data = {'code': '827h46fh38fjg623hd6che6fhw63hf6239k589'}
build_data['workflow.reponame'] = 'test'
result = requests.post('https://SomeUrl', data=build_data, verify=False)

My java code:

public void postReceive()
{
    String payload = "{'code': '827h46fh38fjg623hd6che6fhw63hf6239k589','workflow.reponame': 'test'}"
    makeRequest(payload)
}

private void makeRequest(String payload)
{
    try
    {

        TrustManager[] trustAllCerts = new TrustManager[] {new X509TrustManager() {
            public java.security.cert.X509Certificate[] getAcceptedIssuers() {
            return null;
            }
            public void checkClientTrusted(X509Certificate[] certs, String authType) {
            }
            public void checkServerTrusted(X509Certificate[] certs, String authType) {
            }
        }
        };


        // Install the all-trusting trust manager
        SSLContext sc = SSLContext.getInstance("SSL");

        sc.init(null, trustAllCerts, new java.security.SecureRandom());
        HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());

        // Create all-trusting host name verifier
        HostnameVerifier allHostsValid = new HostnameVerifier() {
            public boolean verify(String hostname, SSLSession session) {
                return true;

            }
        };

        // Install the all-trusting host verifier
        HttpsURLConnection.setDefaultHostnameVerifier(allHostsValid);

        String url = "https://SomeUrl";

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

        con.setRequestMethod("POST");
        con.setRequestProperty("User-Agent", "Mozilla/5.0");
        con.setRequestProperty("Accept-Language", "en-US,en;q=0.5");
        con.setRequestProperty("Content-Type", "application/json; charset=utf8");
        con.setDoOutput(true);

        DataOutputStream wr = new DataOutputStream(con.getOutputStream());
        wr.write(payload.getBytes("UTF-8"));
        wr.flush();
        wr.close();

        int responseCode = con.getResponseCode();

        /*HttpClient client = MySSLSocketFactory.getNewHttpClient();
        HttpPost post = new HttpPost("https://SomeUrl");

        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
        nameValuePairs.add(new BasicNameValuePair("data", payload));
        //nameValuePairs.add(new BasicNameValuePair("verify", false));
        post.setEntity(new UrlEncodedFormEntity(nameValuePairs));

        HttpResponse response = client.execute(post);*/
    }
    catch(Exception e)
    {

    }
}

I commented the code using HttpClient, but it's doing the same thing. Am I doing something wrong?

{'code': '827h46fh38fjg623hd6che6fhw63hf6239k589'} is a python construct, it is not sent "as is" but used by the post method (unwrapped and form-encoded).

instead of :

nameValuePairs.add(new BasicNameValuePair("data", "{'code': '827h46fh38fjg623hd6che6fhw63hf6239k589'}")); 

you should do something like :

nameValuePairs.add(new BasicNameValuePair("code", "827h46fh38fjg623hd6che6fhw63hf6239k589"));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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