HTTP接続に別のクラスを使用してHTTP接続を非表示にし、このコードを減らしたいと考えました。誰でもこのことを手伝ってもらえますか?Restapi Javaコードを使用して呼び出し、別のコードで接続を書き込む必要がある
public class ExportGroups {
public static void main(String[] args) {
try {
String username = "username";
String password = "[email protected]";
String userPassword = username + ":" + password;
String encoding = new sun.misc.BASE64Encoder().encode(userPassword.getBytes());
URL url = new URL("https://AgilePointServer/Extension/ExportGroups");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setDoOutput(true);
conn.setRequestProperty("Authorization", "Basic " + encoding);
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type", "application/json");
String input = "{\"groupNames\": [\"Accounts Payable\"]}";
OutputStream os = conn.getOutputStream();
os.write(input.getBytes());
os.flush();
BufferedReader br = new BufferedReader(new InputStreamReader((conn.getInputStream())));
String output;
System.out.println("Output from Server .... \n");
while ((output = br.readLine()) != null) {
System.out.println(output);
}
conn.disconnect();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
コードの問題は何ですか...? – jackarms
あなたの*具体的な質問は何ですか? StackOverflowの概要と詳細については、[ツアー]と[ヘルプ]をお読みください。 –
私はコードを最適化したいと思いました。私のコードは別のプログラムから呼び出されるべきhttp接続を隠すべきです。私はこれらの接続を一つのクラスでラップし、メソッドを使って呼び出したいと思った。 – Mounika