私はJavaのSlack APIを使っていて、以下のコードブロックのように使用できるHTTPメソッドを取得しようとしています。そのコードブロックは機能しますが、問題には200応答コードも含める必要があり、動作させる方法を理解できません。Java HTTPポストヘルプ
基本的に、私はJavaでHTTP POSTを送信し、URLとコンテンツを使用して200ステータスコードにタグを付けることはできますか?
現在のコード:
public void httpRequest(URL url, String content) {
try {
byte[] contentBytes = content.getBytes("UTF-8");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setRequestProperty("Accept-Charset", "UTF-8");
connection.setRequestProperty("Content-Type", "application/json");
connection.setRequestProperty("Content-Length", Integer.toString(contentBytes.length));
connection.setRequestProperty("Status", Integer.toString(200));
OutputStream requestStream = connection.getOutputStream();
requestStream.write(contentBytes, 0, contentBytes.length);
requestStream.close();
String response = "";
BufferedReader responseStream;
response = "" + ((HttpURLConnection) connection).getResponseCode();
try {
if (((HttpURLConnection) connection).getResponseCode() == 200) {
responseStream = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8"));
} else {
responseStream = new BufferedReader(new InputStreamReader(((HttpURLConnection) connection).getErrorStream(), "UTF-8"));
}
response = responseStream.readLine();
responseStream.close();
} catch (NullPointerException ignored) {
}
} catch (IOException e) {
e.printStackTrace();
}
}
は、サーバーからの200の応答をチェックしようとしていますか?何とかあなたを送ろうとしているように、ちょっと聞こえました。 * "URLとコンテンツを使用して200ステータスコードにタグを付ける" *は非常に混乱します。 –
また、HTTPライブラリではなく、 'HttpURLConnection'クラスを使う必要がありますか?それははるかに難しいです。 –
@AndrewReganいいえ、私はそれをチェックしていません、私は送信しているメッセージに1つを追加しようとしています。あなたが話しているHTTPライブラリのドキュメントに私をリンクすることはできますか? – StaticShadow