public class httpclientdemo {
private static final logger logger = loggerfactory.getlogger(httpclientdemo.class);
public static void main(string[] args) throws exception {
// create a new request.
request httpclientrequest = new request();
try {
// set the request parameters.
// appkey, appsecrect, method and url are required parameters.
// directly writing ak/sk in code is risky. for security, encrypt your ak/sk and store them in the configuration file or environment variables.
// in this example, the ak/sk are stored in environment variables for identity authentication.
// before running this example, set environment variables huaweicloud_sdk_ak and huaweicloud_sdk_sk.
httpclientrequest.setkey(system.getenv("huaweicloud_sdk_ak"));
httpclientrequest.setsecret(system.getenv("huaweicloud_sdk_sk"));
httpclientrequest.setmethod("post");
// set a request url in the format of https://{endpoint}/{uri}.
httpclientrequest.set;
httpclientrequest.addheader("content-type", "text/plain");
// set a body for http request.
httpclientrequest.setbody("put your request body here");
} catch (exception e) {
logger.error(e.getmessage());
return;
}
closeablehttpclient client = null;
try {
// sign the request.
httprequestbase signedrequest = client.sign(httpclientrequest, constant.signature_algorithm_sdk_hmac_sha256);
if (constant.do_verify) {
// creat httpclient and verify ssl certificate
hostname.seturlhostname(httpclientrequest.gethost());
client = (closeablehttpclient) sslciphersuiteutil.createhttpclientwithverify(constant.international_protocol);
} else {
// creat httpclient and do not verify ssl certificate
client = (closeablehttpclient) sslciphersuiteutil.createhttpclient(constant.international_protocol);
}
httpresponse response = client.execute(signedrequest);
// print the body of the response.
httpentity resentity = response.getentity();
if (resentity != null) {
logger.info("processing body with name: {} and value: {}", system.getproperty("line.separator"),
entityutils.tostring(resentity, "utf-8"));
}
} catch (exception e) {
logger.error(e.getmessage());
} finally {
if (client != null) {
client.close();
}
}
}
}
运行httpclientdemo.java,对请求进行签名、访问api并打印结果。
示例结果如下:
[main] info com.huawei.apig.sdk.demo.httpclientdemo - print the authorization: [authorization: sdk-hmac-sha256 access=3afe***ba29, signedheaders=host;x-sdk-date, signature=26b2***dbf6]
[main] info com.huawei.apig.sdk.demo.httpclientdemo - print the status line of the response: http/1.1 200 ok
[main] info com.huawei.apig.sdk.demo.httpclientdemo - processing header with name: date and value: fri, 26 aug 2022 08:58:51 gmt
[main] info com.huawei.apig.sdk.demo.httpclientdemo - processing header with name: content-type and value: application/json
[main] info com.huawei.apig.sdk.demo.httpclientdemo - processing header with name: transfer-encoding and value: chunked
[main] info com.huawei.apig.sdk.demo.httpclientdemo - processing header with name: connection and value: keep-alive
[main] info com.huawei.apig.sdk.demo.httpclientdemo - processing header with name: server and value: api-gateway
[main] info com.huawei.apig.sdk.demo.httpclientdemo - processing header with name: x-request-id and value: 10955c5346b9512d23f3fd4c1bf2d181
[main] info com.huawei.apig.sdk.demo.httpclientdemo - processing body with name:
and value: {"200": "sdk success"}