BillingClient
BillingClient is a client for the BILLING service of the transaction system. It provides developers with a series of methods for interacting with the BILLING service, including bill-related operations. To initiate an HTTP request for the BILLING service using the Java SDK, you need to initialize a BillingClient instance and modify the default configuration items of BosClientConfiguration as required.
Create a BillingClient using AK/SK
Users can refer to the following code to create a BillingClient to access Billing with AK/SK:
1 public class Sample {
2 public static void main(String[] args) {
3 String ACCESS_KEY_ID = <your-access-key-id>; // User’s Access Key ID
4 String SECRET_ACCESS_KEY = <your-secret-access-key>;// User’s Secret Access Key
5 // Initialize a BillingClient
6 BillingClientConfiguration config = new BillingClientConfiguration();
7 config.setCredentials(new DefaultBceCredentials(ACCESS_KEY_ID, SECRET_ACCESS_KEY));
8 config.setEndpoint("https://billing.baidubce.com");
9 BillingClient client = new BillingClient(config);
10 }
11 }
In the code above, [ACCESS_KEY_ID](Reference/Retrieve AK and SK/How to Obtain AKSK.md) corresponds to “Access Key ID” in the console. [SECRET_ACCESS_KEY](Reference/Retrieve AK and SK/How to Obtain AKSK.md) corresponds to “Access Key Secret” in the console. For the method to retrieve them, refer to the Guide - [Manage ACCESSKEY](Reference/Retrieve AK and SK/How to Obtain AKSK.md).
Configure HTTPS protocol for Billing access
Billing supports the HTTPS transmission protocol. You can access the Billing service via HTTPS in the Billing Java SDK through the following two methods:
-
Specify HTTPS in the endpoint:
Plain Text1 String endpoint = "http://billing.baidubce.com"; 2 String ak = "ak"; 3 String sk = "sk"; 4 BillingClientConfiguration config = new BillingClientConfiguration(); 5 config.setCredentials(new DefaultBceCredentials(ak, sk)); 6 BillingClient client = new BillingClient(config); -
Configure HTTPS by calling setProtocol:
Plain Text1 String endpoint = "billing.baidubce.com"; // endpoint without protocol 2 String ak = "ak"; 3 String sk = "sk"; 4 BillingClientConfiguration config = new BillingClientConfiguration(); 5 config.setCredentials(new DefaultBceCredentials(ak, sk)); 6 config.setEndpoint(ENDPOINT); 7 config.setProtocol(Protocol.HTTPS); // Defaults to HTTP if unspecified 8 BillingClient client = new BillingClient(config);Note: If the endpoint already includes a protocol, the one in endpoint takes effect, and the setProtocol() method will be ignored.
Configure BillingClient
If the user needs to configure some detailed parameters of BillingClient, they can specify the BillingClientConfiguration object when constructing BillingClient. BillingClientConfiguration is the configuration class for the BILLING service, which can configure parameters such as proxy and maximum count of connections for the client.
Use a proxy
The following code snippet enables the client to access BILLING service using a proxy:
1String ACCESS_KEY_ID = <your-access-key-id>; // User’s Access Key ID
2 String SECRET_ACCESS_KEY = <your-secret-access-key>;// User’s Secret Access Key
3 String ENDPOINT = <domain-name>; // User-defined domain name
4 // Create a BillingClientConfiguration instance
5 BillingClientConfiguration config = new BillingClientConfiguration();
6 // Configure proxy to local port 8080
7 config.setProxyHost("127.0.0.1");
8 config.setProxyPort(8080);
9 // Create a Billing client
10 config.setCredentials(new DefaultBceCredentials(ACCESS_KEY_ID,SECRET_ACCESS_KEY));
11 config.setEndpoint(ENDPOINT);
12 BillingClientclient = new BillingClient (config);
All client operations will then route through the proxy at 127.0.0.1:8080. For verified proxies, configure credentials:
1// Create a BillingClientConfiguration instance
2 BillingClientConfiguration config = new BillingClientConfiguration ();
3 // Configure proxy to local port 8080
4 config.setProxyHost("127.0.0.1");
5 config.setProxyPort(8080);
6 // Set username and password
7 config.setProxyUsername(<username>); // Username
8 config.setProxyPassword(<password>); // Password
Users may set the basic network parameters with BillingClientConfiguration:
1 BillingClientConfiguration config = new BillingClientConfiguration();
2
3 // Set maximum number of HTTP connections to 10
4 config.setMaxConnections(10);
5
6 // Set TCP connection timeout to 5,000 milliseconds
7 config.setConnectionTimeout(5000);
8
9 // Set timeout for Socket data transmission to 2,000 milliseconds
10 config.setSocketTimeout(2000);
Parameter description
The following parameters can be configured via BillingClientConfiguration:
| Parameters | Description |
|---|---|
| UserAgent | User agent, refers to HTTP’s User-Agent header |
| Protocol | Connection protocol type |
| ProxyDomain | Windows domain for NTLM-verified proxy |
| ProxyHost | Proxy server host address |
| ProxyPort | Proxy server port |
| ProxyUsername | Proxy verification username |
| ProxyPassword | Proxy verification password |
| ProxyPreemptiveAuthenticationEnabled | Enable user agent verification or not |
| ProxyWorkstation | NTLM proxy workstation name |
| LocalAddress | Local address |
| ConnectionTimeoutInMillis | Timeout for establishing TCP connections (unit: ms) |
| SocketTimeoutInMillis | Timeout for socket data transmission (unit: ms) |
| MaxConnections | Maximum allowable HTTP connections |
| RetryPolicy | Retry policy for connections |
| SocketBufferSizeInBytes | Buffer size for socket operations |
| StreamBufferSize | Stream file buffer size |
