I am absolutely tearing my hair out over this one.
I'm trying to make a HTTP POST request in Java, but it keeps sending it as a GET instead. I followed several different ways to send a POST request, but all of them just end up sending as a GET.
This is what I currently have:
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();
I read on one post on StackOverflow that conn.setDoOuput(true) is supposed to make the connection a POST automatically; however, when I debug through the code, the protected member conn.method stays as GET.
I also tried casting url.openConnection() as a HttpURLConnection and called .setRequestMethod("POST") on it, which did absolutely nothing to change conn.method.
As a bonus, I'm trying to do soundcloud authentication and basically every way I've thought of doing it has been an utter failure... Maybe someone has a better solution for this?
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
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.