convertcurlcommandintojavaHttpGet-创新互联
如何把命令
文章名称:convertcurlcommandintojavaHttpGet-创新互联
转载来源:http://hbruida.cn/article/djjiop.html
curl -k -X GET -H "Accept: Application/json" -H "Content-Type: application/json" -u username:password https://someURL/api/cusa/customer/v1/85267198615
专业从事成都网站设计、成都做网站、外贸网站建设,高端网站制作设计,小程序定制开发,网站推广的成都做网站的公司。优秀技术团队竭力真诚服务,采用HTML5建站+CSS3前端渲染技术,成都响应式网站建设,让网站在手机、平板、PC、微信下都能呈现。建站过程建立专项小组,与您实时在线互动,随时提供解决方案,畅聊想法和感受。转换为 java 程序发送 ?
--------------------------------------------------
红色部分为关键代码
先用 url 构造一个 HttpGet , 然后加两个 header , 注意大小写, username 和 password 要用蓝字部分的代码加到 header 里面
--------------------------------------------------
参考文章
https://stackoverflow.com/questions/19797601/apache-http-basicscheme-authenticate-deprecated
search 关键字
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 "";
}
}
文章名称:convertcurlcommandintojavaHttpGet-创新互联
转载来源:http://hbruida.cn/article/djjiop.html