如何在Java中实现这个REST get方法?

发布于 2024-12-09 03:01:05 字数 1262 浏览 0 评论 0原文

我有以下成功运行的 REST get 请求:

在此处输入图像描述

结果是一个 XML 文档,然后我想要解析。我在Java中尝试了同样的操作:

我使用以下代码:

public void getRootService() throws ClientProtocolException, IOException {

    HttpGet httpGet = new HttpGet("https://localhost:9443/ccm/rootservices");
    httpGet.setHeader("Accept", "text/xml");
    HttpResponse response = client.execute(httpGet);
    HttpEntity entity = response.getEntity();
    InputStream in = entity.getContent();
    String projectURL = XMLDocumentParser.parseDocument(in);

    System.out.println(projectURL);
    HttpGet getProjectsRequest = new HttpGet("https://localhost:9443/ccm/process/project-areas");
    getProjectsRequest.setHeader("Content-Type", "application/xml;charset=UTF-8");
    getProjectsRequest.setHeader("Accept-Charset", "UTF-8");
    getProjectsRequest.setHeader("Accept", "application/xml");


    ResponseHandler<String> handler = new BasicResponseHandler();
    String projectResponse = client.execute(getProjectsRequest, handler);
    //String projectResponse = client.execute(getProjectsRequest, handler);

    System.out.println(projectResponse);

}

但是我该如何进行身份验证?我尝试为值“Authorization”添加另一个标头字段,但没有得到相同的结果。

I have the following REST get Request that works successfully:

enter image description here

The result is a XML document that I then want to parse. I tried the same in Java:

I use the following code:

public void getRootService() throws ClientProtocolException, IOException {

    HttpGet httpGet = new HttpGet("https://localhost:9443/ccm/rootservices");
    httpGet.setHeader("Accept", "text/xml");
    HttpResponse response = client.execute(httpGet);
    HttpEntity entity = response.getEntity();
    InputStream in = entity.getContent();
    String projectURL = XMLDocumentParser.parseDocument(in);

    System.out.println(projectURL);
    HttpGet getProjectsRequest = new HttpGet("https://localhost:9443/ccm/process/project-areas");
    getProjectsRequest.setHeader("Content-Type", "application/xml;charset=UTF-8");
    getProjectsRequest.setHeader("Accept-Charset", "UTF-8");
    getProjectsRequest.setHeader("Accept", "application/xml");


    ResponseHandler<String> handler = new BasicResponseHandler();
    String projectResponse = client.execute(getProjectsRequest, handler);
    //String projectResponse = client.execute(getProjectsRequest, handler);

    System.out.println(projectResponse);

}

But how can I do the authentication? I tried to just add another header field for the value "Authorization" but then I don't get the same result.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

橘虞初梦 2024-12-16 03:01:05

我认为你必须创建一个UsernamePasswordCredentials,类似于(未经测试)的东西;

CredentialsProvider credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials(
    new AuthScope("somehost", AuthScope.ANY_PORT), 
    new UsernamePasswordCredentials("username", "password"));

HttpClient httpclient = new DefaultHttpClient();
httpclient.setCredentialsProvider(credsProvider);

请参阅 http://hc.apache.org/httpcomponents-client- ga/tutorial/html/authentication.html

编辑:
刚刚尝试了以下代码,并在我们受 BASIC 保护的开发环境中成功调用了 REST 服务。

public static void main(String[] args) throws ClientProtocolException, IOException {
    CredentialsProvider credsProvider = new BasicCredentialsProvider();
    credsProvider.setCredentials(
        new AuthScope("dev.*******.com", AuthScope.ANY_PORT), 
        new UsernamePasswordCredentials("*****", "******"));


    DefaultHttpClient client = new DefaultHttpClient();
    client.setCredentialsProvider(credsProvider);

    String url = "http://dev.******.com:18081/path/to/service/id.xml";

    HttpGet get = new HttpGet(url);
    ResponseHandler<String> handler = new BasicResponseHandler();
    String resp = client.execute(get, handler);

    System.out.println(resp);
  }

I think you have to create a UsernamePasswordCredentials, something along the lines of (untested);

CredentialsProvider credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials(
    new AuthScope("somehost", AuthScope.ANY_PORT), 
    new UsernamePasswordCredentials("username", "password"));

HttpClient httpclient = new DefaultHttpClient();
httpclient.setCredentialsProvider(credsProvider);

See http://hc.apache.org/httpcomponents-client-ga/tutorial/html/authentication.html

Edit:
Just tried the following code and successfully called a REST service on our dev environment that is BASIC protected.

public static void main(String[] args) throws ClientProtocolException, IOException {
    CredentialsProvider credsProvider = new BasicCredentialsProvider();
    credsProvider.setCredentials(
        new AuthScope("dev.*******.com", AuthScope.ANY_PORT), 
        new UsernamePasswordCredentials("*****", "******"));


    DefaultHttpClient client = new DefaultHttpClient();
    client.setCredentialsProvider(credsProvider);

    String url = "http://dev.******.com:18081/path/to/service/id.xml";

    HttpGet get = new HttpGet(url);
    ResponseHandler<String> handler = new BasicResponseHandler();
    String resp = client.execute(get, handler);

    System.out.println(resp);
  }
忆伤 2024-12-16 03:01:05

您可以将 Authorization 标头显式添加到 HttpRequestBaseHttpGetHttpPost 等的抽象类)

此示例显示基本授权值是如何产生的。

String unhashedString = userName + ":" + password;
String hashedString = Base64.encode(unhashedString); //This class doesn't exist, it's for demonstration purpose only.

HttpGet.setHeader("Authorization", "Basic " + hashedString);

在服务器中,您将必须执行相反的操作(如果您正在执行自己的实现。

You can explicitly add Authorization header to HttpRequestBase (an abstract class for HttpGet, HttpPost, etc.)

This example shows how Basic Authorization value is made.

String unhashedString = userName + ":" + password;
String hashedString = Base64.encode(unhashedString); //This class doesn't exist, it's for demonstration purpose only.

HttpGet.setHeader("Authorization", "Basic " + hashedString);

In the server, you will have to do the reverse (if you're doing your own implementation.

痴情换悲伤 2024-12-16 03:01:05

我实现此方法的方法是将会话与标准 HTTP 控制器一起使用。他们调用一个登录 URL,并发布用户名和密码,然后我对会话进行身份验证。完成后,接下来的所有 URL 只需检查以确保会话已通过身份验证。

The way I implemented this is to use the session with a standard HTTP controller. They call a login URL with user and password posted and I authenticate the session. Once that's done then all following URLs simply check to make sure the session is authenticated.

痞味浪人 2024-12-16 03:01:05

支持的身份验证机制将取决于用于托管 Rational Team Concert 的 Web 服务器。 Apache Tomcat 服务器使用基本身份验证,而 WebSphere Application Server 支持基本身份验证和基于表单的身份验证。

您可以查看详细说明 这里

The authentication mechanism supported will depend on which web server is used to host Rational Team Concert. The Apache Tomcat server uses basic authentication, whereas, WebSphere Application Server supports both basic authentication and form-based authentication.

You can view the detail description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文