Note that there are some explanatory texts on larger screens.

plurals
  1. POset methods aren't working for HttpsURLConnection
    primarykey
    data
    text
    <p>I'm trying to make a rest request in java. I tested the web service using RestClient in Firefox and it works great.</p> <p>When i try to modify the HttpsUrlConnection instance in java the values aren't changing and i get a 500 response code.</p> <p>Here's my code: </p> <pre><code> public String getAuthToken() throws IOException { URL url =new URL("https://webserviceurl"); // webservice url is the url of the webservice String data = URLEncoder.encode("username") + "=" + URLEncoder.encode("myusername","UTF-8"); data+= "&amp;" + URLEncoder.encode("password") + "=" + URLEncoder.encode("pass","UTF-8"); HttpsURLConnection conn =(HttpsURLConnection) url.openConnection(); conn.setUseCaches(false); conn.setHostnameVerifier(new AllowAllHostnameVerifier()); //this works HostName verifier changes conn.setRequestMethod("POST"); // this doens't work. requestMethod is still set to GET conn.setDoOutput(true); // this doesnt work. DoOutput still set on false conn.setRequestProperty("Content-Type", "application/json"); // doens't work either OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream(),"UTF-8"); wr.write(data); wr.flush(); wr.close(); //conn has a 500 response code if (conn.getResponseCode()==200) { InputStream is = conn.getInputStream(); InputStreamReader isr = new InputStreamReader(is); BufferedReader rd = new BufferedReader(isr); String token = rd.readLine(); rd.close(); return token; } else return null; } </code></pre> <p>I'm stucked at this point and cannot find anything to make this work.</p> <p>Thank you!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload