curl -k -X GET -H "Accept: Application/json" -H "Content-Type: application/json" -u username:password https://someURL/api/cusa/customer/v1/85267198615
專(zhuān)業(yè)從事成都網(wǎng)站設(shè)計(jì)、成都做網(wǎng)站、外貿(mào)網(wǎng)站建設(shè),高端網(wǎng)站制作設(shè)計(jì),小程序定制開(kāi)發(fā),網(wǎng)站推廣的成都做網(wǎng)站的公司。優(yōu)秀技術(shù)團(tuán)隊(duì)竭力真誠(chéng)服務(wù),采用HTML5建站+CSS3前端渲染技術(shù),成都響應(yīng)式網(wǎng)站建設(shè),讓網(wǎng)站在手機(jī)、平板、PC、微信下都能呈現(xiàn)。建站過(guò)程建立專(zhuān)項(xiàng)小組,與您實(shí)時(shí)在線(xiàn)互動(dòng),隨時(shí)提供解決方案,暢聊想法和感受。轉(zhuǎn)換為 java 程序發(fā)送 ?
--------------------------------------------------
紅色部分為關(guān)鍵代碼
先用 url 構(gòu)造一個(gè) HttpGet , 然后加兩個(gè) header , 注意大小寫(xiě), username 和 password 要用藍(lán)字部分的代碼加到 header 里面
--------------------------------------------------
參考文章
https://stackoverflow.com/questions/19797601/apache-http-basicscheme-authenticate-deprecated
search 關(guān)鍵字
addHeader(BasicScheme.authenticate
java httpget authen
java httpget set parameters
public String authenticate(String strRequest) {
CloseableHttpResponse httpResponse= null;
CloseableHttpClient httpClient= null;
JSONObject requestJson= null;
try {
requestJson= new JSONObject(strRequest);
if (Utility.validateMsisdn(requestJson.getString("b_party")) == false) {
requestJson.put("status", ServiceAuthentication.Incomplete_Msisdn);
return requestJson.toString();
}
PoolingHttpClientConnectionManager cm= new PoolingHttpClientConnectionManager();
cm.setMaxTotal(200);
cm.setDefaultMaxPerRoute(20);
RequestConfig defaultRequestConfig= RequestConfig.custom()
.setSocketTimeout(Configuration.get("service.timeout.socket", 20000))
.setConnectTimeout(Configuration.get("service.timeout.connect", 20000))
.setConnectionRequestTimeout(Configuration.get("service.timeout.connectrequest", 20000)).build();
JSONObject responseJson= new JSONObject();
ContentBasedRetryStrategy retryStrategy= new ContentBasedRetryStrategy();
httpClient= HttpClients.custom().setServiceUnavailableRetryStrategy(retryStrategy)
.setDefaultRequestConfig(defaultRequestConfig).setConnectionManager(cm).build();
//*************************重要
HttpGet request= new HttpGet(
Configuration.get("service.authentication.url", "") + requestJson.getString("b_party"));
request.addHeader("Content-type", "application/json");
request.addHeader("Accept", "application/json");
UsernamePasswordCredentials creds = new UsernamePasswordCredentials(Configuration.get("service.username", ""),Configuration.get("service.password", ""));
Header header = new BasicScheme(StandardCharsets.UTF_8).authenticate(creds , request, null);
request.addHeader( header);
//************************************
this.logger.info(String.format(Configuration.get("service.authentication.url", "") + requestJson.getString("b_party")));
Header [] headers= request.getAllHeaders();
for (int i = 0;i < headers.length; i++ ) {
Header tmp= headers[i];
this.logger.info(String.format("Headers Name: %s Value:%s
", tmp.getName(), tmp.getValue()));
}
////////////////// temp
// JSONObject subscriberProfileNode = new JSONObject();
//
// subscriberProfileNode.put("type", 8);
// subscriberProfileNode.put("is_allowed", true);
// requestJson.getJSONObject("function").put("subscriber_profile", subscriberProfileNode);
//
// requestJson.put("status", ServiceAuthentication.Success);
//
// JSONObject operatorProfileNode = new JSONObject();
// operatorProfileNode.put("operator", "hkg.csl");
// requestJson.getJSONObject("function").put("operator_profile", operatorProfileNode);
//
// return requestJson.toString();
//////////////////// end temp
httpResponse= httpClient.execute(request);
responseJson= retryStrategy.getResponseObject();
this.logger.info(String.format("HTTP Response: %d, Body: %s", httpResponse.getStatusLine().getStatusCode(),
responseJson.toString()));
if (responseJson != null) {
if (responseJson.getJSONObject("status") != null && responseJson.getJSONObject("status").getInt("code") == 0) {
int subType = 99;
subType= responseJson.getJSONObject("profile").getInt("brand");
JSONObject subscriberProfileNode= new JSONObject();
subscriberProfileNode.put("type", subType);
subscriberProfileNode.put("is_allowed", subType == 8 ? true : false);
boolean bSaved = true;
if (bSaved) {
if (subType != 8) {
requestJson.put("status", ServiceAuthentication.Subscriber_Is_Postpaid);
}else {
requestJson.put("status", ServiceAuthentication.Success);
}
}
requestJson.getJSONObject("function").put("subscriber_profile", subscriberProfileNode);
JSONObject operatorProfileNode= new JSONObject();
operatorProfileNode.put("operator", "hk.csl");
requestJson.getJSONObject("function").put("operator_profile", operatorProfileNode);
}else if (responseJson.getJSONObject("status") != null && responseJson.getJSONObject("status").getInt("code") == 1002) {
JSONObject operatorProfileNode= new JSONObject();
operatorProfileNode.put("operator", "hk.csl");
requestJson.getJSONObject("function").put("operator_profile", operatorProfileNode);
requestJson.put("status", ServiceAuthentication.Subscriber_Not_Found);
}
return requestJson.toString();
}
}catch (Exception e) {
logger.error("Failed to complete ServiceAuthen request: " + e.getMessage(), e);
}finally {
try {
httpClient.close();
}catch (Exception e) {
}
}
return getSubcriberInfoFromCache(requestJson, requestJson.getString("b_party"));
}
private String getSubcriberInfoFromCache(JSONObject requestJson, String msisdn) {
return "";
}
}