2014-06-27 21 views
3

我遇到以下問題: 在加載visualforce頁面時,我正在向內部salesforce頁面發出http callout,但在這裏我面臨身份驗證問題。會話ID問題:vf和開發人員控制檯的不同ID

如果我從開發者控制檯運行相同的http callout,那麼我會得到成功的響應,但是相同的代碼不適用於visualforce頁面。不工作的原因是我在開發者控制檯中的會話ID,並且visualforce域是不同的。

有關讀取我使用 「UserInfo.getSessionId()」

我也曾嘗試{$ Api.Session_ID!}會話ID,但無法正常工作

我的控制器:

public with sharing class HttpRequestForPage 
{ 
    public HttpRequestForPage() 
    { 
     requestForPage('https://ap1.salesforce.com/home/home.jsp'); 
    } 
    public void requestForPage(String pageUrl) 
    { 
     HttpResponse responseOfPage; 
     String responseString; 
     HttpRequest request = new HttpRequest(); 
     request.setMethod('GET'); 
     request.setEndpoint('https://ap1.salesforce.com/home/home.jsp'); 
     request.setHeader('Cookie', 'sid='+UserInfo.getSessionId()); 

     try 
     { 
      responseOfPage = new Http().send(request); 
     } 
     catch(Exception e) 
     { 
      system.debug(e); 
     } 
     responseString = responseOfPage.getBody(); 
     System.debug(responseString=='+responseString); 

    } 
} 

回答

0

嘗試使用授權標頭,而不是爲會話設置cookie。

request.setHeader('Authorization','Bearer '+UserInfo.getSessionId()); 

您還需要將ap1.salesforce.com設置爲遠程站點設置中的端點。


如果您要求從Salesforce內,你可以只使用PageReference getContent()方法頁。

PageReference home = new PageReference('https://ap1.salesforce.com/home/home.jsp'); 
blob homeblob = home.getContent(); 
string homeContent = homeblob.toString();