转换卷曲请求转换的URLConnection(convert curl request into U

2019-10-22 19:41发布

我有这样的cURL要求:

curl -H 'Accept: application/vnd.twitchtv.v3+json' -H 'Authorization: OAuth <access_token>' \
-X PUT https://api.twitch.tv/kraken/users/<bot_name>/follows/channels/<channel_name>

我需要把它变成一个Java URLConnection请求。 这是我到目前为止有:

String url = "https://api.twitch.tv/kraken/?oauth_token=" + bot.botOAuth.substring("oauth:".length());

URL obj = new URL(url);
HttpURLConnection conn = (HttpURLConnection) obj.openConnection();

conn.setRequestProperty("Content-Type", "application/json");
conn.setDoOutput(true);

conn.setRequestMethod("PUT");

OutputStreamWriter out = new OutputStreamWriter(conn.getOutputStream());
out.write("https://api.twitch.tv/kraken/users/" + bot.botName + "/follows/channels/" + gamrCorpsTextField.getText());
out.close();

new InputStreamReader(conn.getInputStream());

任何帮助将不胜感激!

Answer 1:

你准备在这个代码打开URL:

String url = "https://api.twitch.tv/kraken/?oauth_token=" + bot.botOAuth.substring("oauth:".length());

不符合您的curl请求URL:

https://api.twitch.tv/kraken/users/<bot_name>/follows/channels/<channel_name>

你似乎想更多的东西是这样的:

URL requestUrl = new URL("https://api.twitch.tv/kraken/users/" + bot.botName
        + "/follows/channels/" + gamrCorpsTextField.getText());
HttpURLConnection connection = (HttpUrlConnection) requestUrl.openConnection();

connection.setRequestMethod("PUT");
connection.setRequestProperty("Accept", "application/vnd.twitchtv.v3+json");
connection.setRequestProperty("Authorization", "OAuth <access_token>");
connection.setDoInput(true);
connection.setDoOutput(false);

这设置了一个“ URLConnection请求,”相当于一个curl命令将发出,如要求。 从那里,你通过得到的回应码,读取响应头和身体,等connection对象。



文章来源: convert curl request into URLConnection
标签: java http curl put