0

我正在尝试使用 Rest API 从 java 的安全关系存储库中获取工件。我收到 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

I would suggest to use the Nexus Client library rather than writing your own Java wrapper for the REST API. More details and links to examples are in the Nexus book.

于 2013-11-08T20:59:16.607 回答
0

我用java来模拟GET方法。第三个参数“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 回答