2017-07-14 56 views
0

我試圖從salesforce使用下面的java(版本1.7)代碼片段獲取訪問令牌,但我得到內部服務器錯誤(錯誤代碼:500)和銷售團隊錯誤ID爲1366385420-22703(1478489697)salesforce:錯誤ID:1366385420-22703(1478489697) - >內部服務器錯誤500

但是我在Java 1.8中嘗試使用相同的代碼,它的工作正常,我得到了正確的響應。

我使用的Jars是「httpclient-4.3.3.jar,httpcore-4.3.2.jar」。

import java.io.BufferedReader; 
import java.io.InputStream; 
import java.io.InputStreamReader; 
import java.util.ArrayList; 
import java.util.List; 

import javax.net.ssl.SSLContext; 

import org.apache.http.HttpEntity; 
import org.apache.http.HttpHost; 
import org.apache.http.HttpResponse; 
import org.apache.http.client.config.RequestConfig; 
import org.apache.http.client.entity.UrlEncodedFormEntity; 
import org.apache.http.client.methods.HttpPost; 
import org.apache.http.conn.ssl.SSLConnectionSocketFactory; 
import org.apache.http.conn.ssl.SSLContexts; 
import org.apache.http.impl.client.CloseableHttpClient; 
import org.apache.http.impl.client.HttpClientBuilder; 
import org.apache.http.impl.client.HttpClients; 
import org.apache.http.impl.conn.DefaultProxyRoutePlanner; 
import org.apache.http.message.BasicNameValuePair; 

public class ChatterHelper 
{ 

    public static void main(String[] args) { 


     postOnChatterGroup(); 
    } 


    public static void postOnChatterGroup() 
    { 
     HttpHost proxy; 
     DefaultProxyRoutePlanner routePlanner; 
     RequestConfig requestConfig; 
     CloseableHttpClient httpClient = null; 
     InputStream is; 


     try 
     { 
      String clientId ="3MVG9**********************************************"; 
      String clientSecret ="*****************"; 
      String environment = "https://*****.salesforce.com"; 
      String authURL = "/services/oauth2/token"; 
      String userName = "**********************************"; 
      String pwd = "*******"; 
      StringBuffer sbf = new StringBuffer(); 
      String baseUrl = environment+authURL; 
      System.setProperty("https.protocols", "TLSv1.1,SSLv3"); 
      proxy = new HttpHost("***.***.com", 80); 
      routePlanner = new DefaultProxyRoutePlanner(proxy); 
      requestConfig = RequestConfig.custom().setConnectTimeout(20000) 
        .setConnectionRequestTimeout(10000).setSocketTimeout(20000) 
        .build(); 
      httpClient = HttpClientBuilder.create().setDefaultRequestConfig(
        requestConfig).setRoutePlanner(routePlanner).build(); 

      HttpPost oauthPost = new HttpPost(baseUrl); 
      oauthPost.setConfig(requestConfig); 
      List<BasicNameValuePair> parametersBody = new ArrayList<BasicNameValuePair>(); 
      // keep this as it is 
      parametersBody.add(new BasicNameValuePair("grant_type", "password")); 
      parametersBody.add(new BasicNameValuePair("username", userName)); 
      parametersBody.add(new BasicNameValuePair("password", pwd)); 
      parametersBody.add(new BasicNameValuePair("client_id", clientId)); 
      parametersBody.add(new BasicNameValuePair("client_secret", clientSecret)); 
      oauthPost.setEntity(new UrlEncodedFormEntity(parametersBody)); 
      // Execute the request. 
      HttpResponse response = httpClient.execute(oauthPost); 
      HttpEntity entity = response.getEntity(); 
      int code = response.getStatusLine().getStatusCode(); 
      System.out.println( ", Result Code >> " + code); 
      if (entity != null) 
      { 
       BufferedReader rd = new BufferedReader(new InputStreamReader(
         entity.getContent(), "UTF-8")); 
       String line = ""; 
       while ((line = rd.readLine()) != null) 
       { 
        sbf.append(line); 
        System.out.println(line); 
       } 
      } 


     } 
     catch (Exception e) 
     { 
      e.printStackTrace(); 
     } 
    } 
} 

下面是從段的輸出結果: 結果代碼>> 500

, Result Code >> 500 
 

 

 

 

 

 

 
<html> 
 
<head><title>An internal server error has occurred</title></head> 
 
<body> 
 

 

 
<div style="display:none;" id="errorTitle">An internal server error has occurred</div> 
 
<div style="display:none;" id="errorDesc">An error has occurred while processing your request. The salesforce.com support team has been notified of the problem. If you believe you have additional information that may be of help in reproducing or correcting the error, please contact <a href="https://help.salesforce.com/apex/hthome">Salesforce Support</a>. Please indicate the URL of the page you were requesting, any error id shown on this page as well as any other related information. We apologize for the inconvenience. <br/><br/>Thank you again for your patience and assistance. And thanks for using salesforce.com!</div> 
 
<table cellspacing=10> 
 
<tr><td><span style="font-weight: bold; font-size: 12pt;">An internal server error has occurred</span></td></tr> 
 
<tr><td> 
 
An error has occurred while processing your request. The salesforce.com support team has been notified of the problem. If you believe you have additional information that may be of help in reproducing or correcting the error, please contact <a href="https://help.salesforce.com/apex/hthome">Salesforce Support</a>. Please indicate the URL of the page you were requesting, any error id shown on this page as well as any other related information. We apologize for the inconvenience. <br/><br/>Thank you again for your patience and assistance. And thanks for using salesforce.com! 
 
<br><br> 
 
Error ID: 1099658790-8511 (1478489697) 
 
</td> 
 
</tr> 
 
<tr><td> 
 
<br clear="all"><br><br> 
 

 

 
</td></tr> 
 
</table> 
 

 
</td></tr> 
 
</table> 
 

 

 

 
</body> 
 
</html>

回答

0

這是哪裏的代碼運行?我相信TLS 1.1在Java 1.7中不是默認啓用的。您必須先啓用它。

+0

我用'System.setProperty(「https.protocols」,「TLSv1.1,SSLv3」);'啓用TLS 1.1,是否正確? – Muthukumar

+0

不幸的是,我不是一個java專家,所以我真的不能說。您可能需要在其他地方提問。 –