function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Rani_RRani_R 

How to display a html content in the HTTPResponse object in a VF Page?

Hi,

 

I have two questions, i am trying to figure it out for a long time, please help me.

 

Below is my apex code,

 

public class Dev_Login{
    public String userName {get; set;}
    public String password {get; set;}
    public String outputText {get; set;}
    
    public Dev_Login() {
        
    }    
    
    public void loginFn() {

        Http http = new Http();
        HttpRequest req = new HttpRequest();
        HttpResponse res;
        try {        
            req.setEndpoint('https://www.mywebsite.com/jsp');
            req.setHeader('Content-Type', 'application/x-www-form-urlencoded');      
            req.setMethod('POST');
            String theMessage = 'userId='+userName+'&password='+password;            
            
            req.setBody(theMessage);
            
            res = http.send(req);
            
            outputText = res.getBody();
            
        } catch (Exception e) {
            System.debug('Error'+e);
        }
    }
}

 

 

the response (res.getBody()) will contain a HTML content, i want to display it in the VF Page that contains the login button. How should i do it? i had the below in my vf page

 

<apex:outputpanel >
        <apex:outputtext rendered="true" value="{!outputText}"/>
    </apex:outputpanel>

 

But this just displayed the html content as a string, didnt render it as html.. Please tell me how should i do it?

 

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

 

My second question,

 

say if i get an XML in the response in this page (outputText)

i want to redirect to a new VF page where i will use that xml to display something... how should i pass this xml to that new page. that newpage will use a different controller..  so i want to pass this xml to the second page's controller and second page should be displayed... how should i do it.

 

Please help me with these two questions..

 

Thanks,

Rani

sfdcfoxsfdcfox

1) When using apex:outputText, use escape="false" in order to tell Salesforce that you really want to render html as html, and not as escaped html. Careful when you use this, because that can introduce XSS (Cross-Site Scripting) attack vectors.

 

2) If the parameters are few, just use parameters. For example:

 

 

public ApexPages.PageReference nextPage() {
  ApexPages nextPage = Page.nextPage;
  nextPage.getParamters().put('xml',xmlCode);
  nextPage.setRedirect(true);
  return nextPage;
}
Note that older browsers only support up to 2k characters, so if your XML is even remotely large, you'll have to choose another path.

If possible, you should use the same controller for both pages. This automatically grants a view state transfer between the pages. You'll have to include common functions to both pages, and there are some limitations on this "freebie" state transfer, but it's far better than trying to hack your own way through.

Alternatively, combine both pages into one page. Using the "rendered" attribute makes doing this relatively painless, and you could also use compositions or templates to break the page into sections.

Finally, if nothing else is viable, use a custom object with a Long Text Area. You can use a DML to delete the record when you no longer need it.

 

Rani_RRani_R

Thankyou very much sfdfox.

 

I am trying each one of your answers.

 

1) in the outputText, i put escape="false", but now i am not getting anything there, its just empty space. if i remove the escape="false" the html text is being displayed. Why the html is not getting rendered when i put escape="False" ??

 

Thanks and Regards,

Rani

sfdcfoxsfdcfox

I did a test with Google (just to prove it works). Here's my code:

 

 

<!-- displayGoogleHome -->
<apex:page controller="RemoteGoogleController" action="{!loadGoogleHome}">
    <apex:outputText escape="false" value="{!googleHome}"/>
</apex:page>

 

public class RemoteGoogleController {
    public string googleHome { get; set; }
    public void loadGoogleHome() {
        Http endpoint = new Http();
        HttpRequest req = new HttpRequest();
        req.setEndpoint('http://www.google.com');
        req.setMethod('GET');
        HttpResponse res = endpoint.send(req);
        googleHome = res.getBody();
    }
}

This arbitrarily simple concoction allowed me to render the entire page. Of course, I could have used just a fragment from a AHAH-style response, or anything else, but the technique definitely works. The entire contents of the page were rendered inside the content section of the page, as expected, including the top navigation bar, the search box, and other links. Of course, without patching URLs, the search feature won't actually work, nor any of the other URLs, but I hope that this demonstrates that this feature works as advertised.

 

muralikrishna.sfdcmuralikrishna.sfdc

I am also working on same issue .

 

the html responce come and place on preious ulr position.

 

if you already resolve that please help me

 

 

// vf content

 

 

<apex:page Controller="paymentGateWay">
 

<apex:form >
<apex:pageBlock >
<apex:pageBlockSection >

<apex:inputText label="LoanApplication Number" value="{!LAN}"/>
<apex:inputText label="Amount" value="{!Amount}" />
<apex:commandButton value="Pay Now" immediate="true" action="{!goForPay}" />
<apex:outputpanel >
        <apex:outputtext rendered="true" value="{!outputflow2}"/>
</apex:outputpanel>
 

</apex:pageBlockSection>
</apex:pageBlock>
</apex:form>    
</apex:page

 

 

// apex content

 

 

 

 

public class paymentGateWay {

    public String outputflow2{ get; set; }

 
 public string LAN{get;set;}
 public string Amount{get;set;}
public outputflow2 {get;set;}
 
   
  public string  goForPay() {
   
 string sessionid = UserInfo.getSessionId();
   string body='?txtCustomerId='+LAN+'&txtTxnAmount='+Amount;
   string body2= EncodingUtil.urlEncode(body, 'UTF-8');  
    Http h = new Http();

  // creating https request
       HttpRequest req = new HttpRequest();
         req.setEndpoint('https://www.billdesk.com/pgidsk/pgmerc/mycompany.jsp'+body);
         req.setMethod('POST');
       
    HttpResponse res = h.send(req);
    string links = res.getBody();
    //blob link= res.getBodyAsBlob();
    
    //string body3= EncodingUtil.urlDecode(links , 'UTF-8');
        
     system.debug('FFFFFFFFFF'+link);
    
    outputflow2 =res.getBody();
    return outputflow2;
   
    }
 
 
}

sfdcfoxsfdcfox

Add escape="false" to your outputText to render the HTML correctly.