2016-10-01 44 views
0

我需要設置這個代碼TLSv1.2工作連接上的HttpClient設置TLSv1.2工作:使用的PostMethod

public String login(){ 
    HttpClient client = new HttpClient(); 

    log.trace("#LOGIN"); 
    log.trace("url : " +this.url_login); 
    log.trace("grant_type : " +this.grant_type); 
    log.trace("customer_secret : " +this.customer_secret); 
    log.trace("username : " +this.username); 
    log.trace("customer_key : " +this.customer_key); 

    PostMethod post = new PostMethod(this.url_login); 

    post.setRequestHeader("content-Type", "application/x-www-form-urlencoded"); 
    NameValuePair[] data = { 
      new NameValuePair("grant_type", this.grant_type), 
      new NameValuePair("client_id", this.customer_key), 
      new NameValuePair("client_secret", this.customer_secret), 
      new NameValuePair("username", this.username), 
      new NameValuePair("password", this.password+this.security_token) 
    }; 
    post.setRequestBody(data); 
    try { 
     client.executeMethod(post); 
     String response = post.getResponseBodyAsString(); 
     JSONObject json = new JSONObject(response); 
     ......... 

我怎樣才能做到這一點? 我看到使用SSLContext是可能的,但我不想重寫代碼(它不是我的項目) 關心。

回答

0

如果您不想更改代碼,您可以執行的操作受到限制。有幾件事你可以嘗試,最簡單的是切換到默認打開TLS1.1和1.2的Java 8。

你可以嘗試設置一個命令行開關:

的Java -Dhttps.protocols = TLSv1.1,TLSv1.2工作...

有幾個例子here,但以我的經驗,他們只適用於非常特殊的情況。

+0

還有其他解決方案嗎? – stefanOM

+0

不幸的是,如果你不能切換到Java 8,你很可能需要使用自定義的SSLContext打開TLS 1.1和1.2。我必須做類似的事情,因爲我的生產環境中的OS級別無法支持Java 8.您確實需要Java 7,因爲這是支持TLS 1.1和1.2的第一個JVM版本。我不相信他們支持Java 6。 – Mike