如何在 Java 中制作这个 REST get 方法?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7725043/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to make this REST get method in Java?
提问by Buhake Sindi
I have the following REST get Request that works successfully:
我有以下成功运行的 REST get 请求:
The result is a XML document that I then want to parse. I tried the same in Java:
结果是我想要解析的 XML 文档。我在 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.
但是我如何进行身份验证?我试图为值“授权”添加另一个标题字段,但我没有得到相同的结果。
回答by Qwerky
I think you have to create a UsernamePasswordCredentials
, something along the lines of (untested);
我认为你必须创建一个UsernamePasswordCredentials
(未经测试)的东西;
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
见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.
编辑:
刚刚尝试了以下代码并在我们受 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);
}
回答by Buhake Sindi
You can explicitly add Authorization
header to HttpRequestBase
(an abstract class for HttpGet
, HttpPost
, etc.)
你可以明确地添加Authorization
标头HttpRequestBase
(为一个抽象类HttpGet
,HttpPost
等等)
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.
在服务器中,你将不得不做相反的事情(如果你正在做你自己的实现。
回答by Thom
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.
我实现的方法是使用带有标准 HTTP 控制器的会话。他们调用一个带有用户和密码的登录 URL,我对会话进行身份验证。完成后,所有后续 URL 只需检查以确保会话已通过身份验证。
回答by Farhan
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.
支持的身份验证机制将取决于用于托管 Rational Team Concert 的 Web 服务器。Apache Tomcat 服务器使用基本身份验证,而 WebSphere Application Server 支持基本身份验证和基于表单的身份验证。
You can view the detail description here
您可以在此处查看详细说明