Richiesta Post HTTP in Java .Sethequestmethod () non fa nulla
-
15-11-2019 - |
Domanda
Sto assolutamente strappando i capelli su questo.
Sto cercando di creare una richiesta di post http in Java, ma continua a inviarlo come invece.Ho seguito diversi modi per inviare una richiesta di post, ma tutti finiscono per inviare come ottenere.
Questo è ciò che attualmente ho:
URL url = new URL("https://api.soundcloud.com/oauth2/token");
URLConnection conn = url.openConnection();
conn.setDoOutput(true);
OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
wr.write(data);
wr.close();
// Get the response
BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line;
while ((line = rd.readLine()) != null) {
resp += line;
}
rd.close();
.
Leggi su un post su Stackoverflow che Conn.setdouput (true) dovrebbe rendere la connessione automaticamente un post;Tuttavia, quando debug attraverso il codice, il membro protetto Conn.Method rimane come ottenere.
Ho anche provato il casting url.openconnection () come httpurlconnection e ha chiamato .sethequestmethod ("post") su di esso, che non ha assolutamente nulla da cambiare conn.method.
Come bonus, sto cercando di fare autenticazione soundcloud e fondamentalmente in ogni modo che ho pensato di farlo è stato un fallimento totale ... Forse qualcuno ha una soluzione migliore per questo?
Soluzione
Have you tried using SoundCloud's Java SDK?
https://github.com/soundcloud/java-api-wrapper
This should take most of the HTTP/OAuth2 away from your code.
But even if you want more control over what you are doind, it is usually a good idea to use a proper HTTP client library, like Apache's, instead of trying to fight agains the very basic Java SE support for it. Check out: http://hc.apache.org/httpcomponents-client-ga/index.html