0

Rest API を使用して、Java からセキュリティで保護された nexus リポジトリからアーティファクトを取得しようとしています。応答として 401 Unauthorized が返されます。

自分自身を承認するために何をする必要がありますか?

String url = "http://myNexus.com/service/local/artifact/maven/redirect?r=my-repo&g=my.group&a=my-artifact&v=LATEST";

HttpClient client = new DefaultHttpClient();
HttpGet request = new HttpGet(url);

HttpResponse response = null;

response = client.execute(request);

System.out.println("Response Code : "
       + response.getStatusLine().getStatusCode());
4

2 に答える 2

0

Java を使用して GET メソッドをシミュレートしました。3 番目のパラメーター「auth」は、基本的な http 認証の例です。
http://en.wikipedia.org/wiki/Basic_access_authentication
http://en.wikipedia.org/wiki/BASE64

    public String sendGet(String url, String param, String auth) {
    String result = "";
    BufferedReader in = null;
    try {
        String urlName = url + "?" + param;
        URL realUrl = new URL(urlName);
        URLConnection conn = realUrl.openConnection();
        conn.setRequestProperty("accept", "*/*");
        conn.setRequestProperty("connection", "Keep-Alive");
        conn.setRequestProperty("user-agent",
                "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)");
        conn.setRequestProperty("Accept", "application/json");
        conn.addRequestProperty("Authorization", "Basic " + auth);
        conn.connect();
        in = new BufferedReader(
                new InputStreamReader(conn.getInputStream()));
        String line;
        while ((line = in.readLine()) != null) {
            result += line;
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    finally {
        try {
            if (in != null) {
                in.close();
            }
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
    return result;
}
于 2014-01-09T10:06:13.700 に答える