如何在Java中实现这个REST get方法?
我有以下成功运行的 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:
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我认为你必须创建一个
UsernamePasswordCredentials
,类似于(未经测试)的东西;请参阅 http://hc.apache.org/httpcomponents-client- ga/tutorial/html/authentication.html
编辑:
刚刚尝试了以下代码,并在我们受 BASIC 保护的开发环境中成功调用了 REST 服务。
I think you have to create a
UsernamePasswordCredentials
, something along the lines of (untested);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.
您可以将
Authorization
标头显式添加到HttpRequestBase
(HttpGet
、HttpPost
等的抽象类)此示例显示基本授权值是如何产生的。
在服务器中,您将必须执行相反的操作(如果您正在执行自己的实现。
You can explicitly add
Authorization
header toHttpRequestBase
(an abstract class forHttpGet
,HttpPost
, etc.)This example shows how Basic Authorization value is made.
In the server, you will have to do the reverse (if you're doing your own implementation.
我实现此方法的方法是将会话与标准 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.
支持的身份验证机制将取决于用于托管 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