convertcurlcommandintojavaHttpGet-創(chuàng)新互聯

如何把命令 convertcurlcomma
ndintojavaHttpGet

curl -k -X GET -H "Accept: Application/json" -H "Content-Type: application/json" -u username:password https://someURL/api/cusa/customer/v1/85267198615

專業(yè)從事成都網站設計、成都做網站、外貿網站建設,高端網站制作設計,小程序定制開發(fā),網站推廣的成都做網站的公司。優(yōu)秀技術團隊竭力真誠服務,采用HTML5建站+CSS3前端渲染技術,成都響應式網站建設,讓網站在手機、平板、PC、微信下都能呈現。建站過程建立專項小組,與您實時在線互動,隨時提供解決方案,暢聊想法和感受。

轉換為 java 程序發(fā)送 ?

--------------------------------------------------

紅色部分為關鍵代碼

先用 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-創(chuàng)新互聯
鏈接分享:http://muchs.cn/article8/djjiop.html

成都網站建設公司_創(chuàng)新互聯,為您提供網站內鏈、網站設計公司、靜態(tài)網站、搜索引擎優(yōu)化網站改版、關鍵詞優(yōu)化

廣告

聲明:本網站發(fā)布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯

外貿網站建設