this is my code ,but have errors :
public static List<JobInfo> listAllJobs() {
ArrayList<JobInfo> jobList = new ArrayList<JobInfo>();
HttpPost request = new HttpPost(KYLIN_JOB_SERVER_URL +
"/api/jobs");
//HttpPost request = new HttpPost(KYLIN_JOB_SERVER_URL +
"/api/cubes");
try {
HttpClient httpClient = HttpClients.createDefault();
//HttpMethodBase method = new
HttpMethodBase(KYLIN_JOB_SERVER_URL + "/api/jobs") ;
request.addHeader("Content-Type", "application/json");
String basicAuth
=DatatypeConverter.printBase64Binary(("ADMIN" + ":" +
"KYLIN").getBytes());
request.addHeader("Authorization", "Basic " +
basicAuth);
HttpResponse response = httpClient.execute(request);
System.out.println("response.getStatusLine().getStatusCode():"+response.getStatusLine().getStatusCode());
if (response.getStatusLine().getStatusCode() != 200) {
throw new RuntimeException("Failed : HTTP error
code : " + response.getStatusLine().getStatusCode());
}
HttpEntity entity = response.getEntity();
InputStream is = entity.getContent();
byte[] responseBody = IOUtils.toByteArray(is);
String json = new String(responseBody);
log.debug(json);
System.out.println("json:"+json);
// ObjectMapper om = new ObjectMapper();
// jobList.addAll((List<JobInfo>) om.readValue(json, new
TypeReference<List<JobInfo>>() {
// }));
} catch (Throwable t) {
t.printStackTrace();
} finally {
request.releaseConnection();
}
return jobList;
}
console ERRORS output:
18:43:03 RequestAddCookies - CookieSpec selected: best-match
18:43:03 RequestAuthCache - Auth cache not set in the context
18:43:03 PoolingHttpClientConnectionManager - Connection request: [route:
{}->http://192.168.1.12:7070][total kept alive: 0; route allocated: 0 of 2;
total allocated: 0 of 20]
18:43:03 PoolingHttpClientConnectionManager - Connection leased: [id:
0][route: {}->http://192.168.1.12:7070][total kept alive: 0; route allocated: 1
of 2; total allocated: 1 of 20]
18:43:03 MainClientExec - Opening connection {}->http://192.168.1.12:7070
18:43:03 HttpClientConnectionOperator - Connecting to /192.168.1.12:7070
18:43:03 HttpClientConnectionOperator - Connection established
192.168.0.143:65129<->192.168.1.12:7070
18:43:03 MainClientExec - Executing request POST /kylin/api/jobs HTTP/1.1
18:43:03 MainClientExec - Proxy auth state: UNCHALLENGED
18:43:03 LoggingManagedHttpClientConnection - http-outgoing-0 >> POST
/kylin/api/jobs HTTP/1.1
18:43:03 LoggingManagedHttpClientConnection - http-outgoing-0 >> Content-Type:
application/json
18:43:03 LoggingManagedHttpClientConnection - http-outgoing-0 >>
Authorization: Basic QURNSU46S1lMSU4=
18:43:03 LoggingManagedHttpClientConnection - http-outgoing-0 >>
Content-Length: 0
18:43:03 LoggingManagedHttpClientConnection - http-outgoing-0 >> Host:
192.168.1.12:7070
18:43:03 LoggingManagedHttpClientConnection - http-outgoing-0 >> Connection:
Keep-Alive
18:43:03 LoggingManagedHttpClientConnection - http-outgoing-0 >> User-Agent:
Apache-HttpClient/4.3.3 (java 1.5)
18:43:03 LoggingManagedHttpClientConnection - http-outgoing-0 >>
Accept-Encoding: gzip,deflate
18:43:03 Wire - http-outgoing-0 >> "POST /kylin/api/jobs HTTP/1.1[\r][\n]"
18:43:03 Wire - http-outgoing-0 >> "Content-Type: application/json[\r][\n]"
18:43:03 Wire - http-outgoing-0 >> "Authorization: Basic
QURNSU46S1lMSU4=[\r][\n]"
18:43:03 Wire - http-outgoing-0 >> "Content-Length: 0[\r][\n]"
18:43:03 Wire - http-outgoing-0 >> "Host: 192.168.1.12:7070[\r][\n]"
18:43:03 Wire - http-outgoing-0 >> "Connection: Keep-Alive[\r][\n]"
18:43:03 Wire - http-outgoing-0 >> "User-Agent: Apache-HttpClient/4.3.3 (java
1.5)[\r][\n]"
18:43:03 Wire - http-outgoing-0 >> "Accept-Encoding: gzip,deflate[\r][\n]"
18:43:03 Wire - http-outgoing-0 >> "[\r][\n]"
18:43:03 Wire - http-outgoing-0 << "HTTP/1.1 405 Method Not Allowed[\r][\n]"
18:43:03 Wire - http-outgoing-0 << "Server: Apache-Coyote/1.1[\r][\n]"
18:43:03 Wire - http-outgoing-0 << "Set-Cookie:
JSESSIONID=1B48E4EE1F327464EA6B32449346BBB7; Path=/kylin/; HttpOnly[\r][\n]"
18:43:03 Wire - http-outgoing-0 << "Allow: GET[\r][\n]"
18:43:03 Wire - http-outgoing-0 << "Content-Type:
text/html;charset=utf-8[\r][\n]"
18:43:03 Wire - http-outgoing-0 << "Content-Language: en[\r][\n]"
18:43:03 Wire - http-outgoing-0 << "Content-Length: 1047[\r][\n]"
18:43:03 Wire - http-outgoing-0 << "Date: Fri, 08 Jan 2016 10:41:50
GMT[\r][\n]"
18:43:03 Wire - http-outgoing-0 << "[\r][\n]"
18:43:03 Wire - http-outgoing-0 << "<html><head><title>Apache Tomcat/7.0.59 -
Error report</title><style><!--H1
{font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:22px;}
H2
{font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:16px;}
H3
{font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:14px;}
BODY {font-family:Tahoma,Arial,sans-serif;color:black;background-color:white;}
B {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;} P
{font-family:Tahoma,Arial,sans-serif;background:white;color:black;font-size:12px;}A
{color : black;}A.name {color : black;}HR {color : #525D76;}--></style>
</head><body><h1>HTTP Status 405 - Request method 'POST' not supported</h1><HR
size="1" noshade="noshade"><p><b>type</b> Status report</p><p><b>message</b>
<u>Request method 'POST' not supported</u></p><p><b>description</b> <u>The
specified HTTP method is not allowed for the requested resource.</u></p><HR
size="1" noshade="noshade"><h3>Apache Tomcat/7.0.59</h3></body></html>"
18:43:03 LoggingManagedHttpClientConnection - http-outgoing-0 << HTTP/1.1 405
Method Not Allowed
18:43:03 LoggingManagedHttpClientConnection - http-outgoing-0 << Server:
Apache-Coyote/1.1
18:43:03 LoggingManagedHttpClientConnection - http-outgoing-0 << Set-Cookie:
JSESSIONID=1B48E4EE1F327464EA6B32449346BBB7; Path=/kylin/; HttpOnly
18:43:03 LoggingManagedHttpClientConnection - http-outgoing-0 << Allow: GET
18:43:03 LoggingManagedHttpClientConnection - http-outgoing-0 << Content-Type:
text/html;charset=utf-8
18:43:03 LoggingManagedHttpClientConnection - http-outgoing-0 <<
Content-Language: en
18:43:03 LoggingManagedHttpClientConnection - http-outgoing-0 <<
Content-Length: 1047
18:43:03 LoggingManagedHttpClientConnection - http-outgoing-0 << Date: Fri, 08
Jan 2016 10:41:50 GMT
18:43:03 MainClientExec - Connection can be kept alive indefinitely
18:43:03 ResponseProcessCookies - Cookie accepted
[JSESSIONID="1B48E4EE1F327464EA6B32449346BBB7", version:0, domain:192.168.1.12,
path:/kylin/, expiry:null]
response.getStatusLine().getStatusCode():405
java.lang.RuntimeException: Failed : HTTP error code : 40518:43:03
ConnectionHolder - Cancelling request execution
18:43:03 LoggingManagedHttpClientConnection - http-outgoing-0: Shutdown
connection
at
com.britecloud.spark.jobs.KylinJobUtils.listAllJobs(KylinJobUtils.java:63)
at com.britecloud.spark.jobs.KylinJobUtils.main(KylinJobUtils.java:89)
18:43:03 ConnectionHolder - Connection discarded
18:43:03 LoggingManagedHttpClientConnection - http-outgoing-0: Close connection
18:43:03 PoolingHttpClientConnectionManager - Connection released: [id:
0][route: {}->http://192.168.1.12:7070][total kept alive: 0; route allocated: 0
of 2; total allocated: 0 of 20]
------------------ Original ------------------
From: "yu feng"<[email protected]>;
Date: Fri, Jan 8, 2016 06:12 PM
To: "dev"<[email protected]>;
Subject: Re: restful interface
You can add authentication infomation like this :
protected void addHttpHeaders(HttpMethodBase method) {
method.addRequestHeader("Accept", "application/json, text/plain,
*/*");
method.addRequestHeader("Content-Type", "application/json");
String basicAuth =
DatatypeConverter.printBase64Binary((this.username + ":" +
this.password).getBytes());
method.addRequestHeader("Authorization", "Basic " + basicAuth);
}
In curl command, you can execute command like this : curl -H
"Authorization:Basic auth-information-content" -H "Content-Type:
application/json" "http://localhost:7070/kylin/api/xxx", and
auth-information-content
is value of username + ":" + password encoding with base64. for example
"ADMIN:KYLIN" will encode to "QURNSU46S1lMSU4="
2016-01-08 17:37 GMT+08:00 王琳 <[email protected]>:
> hi
> I have a restful interface problems need to consult about:
>
> Java call restful interface for user authentication how to control
> this piece?
>
>
> Thanks