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
bharat004.is091.3910779915252478E12bharat004.is091.3910779915252478E12 

I used http request and response in apex. But dont knw how to use it in visual force. My task is ie. From visual force page username , password etc should send to apex using POST method.

public class Xmlretrive {
  
   
    public String xmlOutput {
        get;
        set;
    }
 
    public String auth {
        get;
        set;
    }
    public String password {
        get;
        set;
    }
    public String userName {
        get;
        set;
    }
    public boolean displayxml {
        get;
        set;
    }
    public Xmlretrive() {
        displayxml = false;
  
    }
    public PageReference submit() {
        displayxml = true;
        if (userName == null) {
            userName = 'Write here';
        } else {
            userName = userName;
            password = password;
            auth = auth;
        }
        return null;
    }
    public String getXml() {
         HttpRequest req = new HttpRequest();
        HttpResponse res = new HttpResponse();
        Http http = new Http();      
              
        XmlStreamWriter w = new XmlStreamWriter();
        w.writeStartDocument(null, '1.0');
        w.writeProcessingInstruction('target', 'data');
        w.writeStartElement('', 'attrs','http://www.sap.com/rws/bip');
        w.writeNamespace('', 'http://www.sap.com/rws/bip' );
        w.writeStartElement('', 'attr', 'http://www.sap.com/rws/bip' );
        w.writeAttribute(null, null,'name', 'username');
        w.writeAttribute(null, null,'type', 'string');
        w.writeCharacters(username);
        w.writeEndElement();
        w.writeStartElement('', 'attr', 'http://www.sap.com/rws/bip' );
        w.writeAttribute(null, null, 'name', 'password');
        w.writeCharacters(password);
        w.writeEndElement();
        w.writeStartElement('', 'attr', 'http://www.sap.com/rws/bip' );
        w.writeAttribute(null, null,'name', 'auth');
        w.writeAttribute(null, null, 'type', 'string');
        w.writeAttribute(null, null,'possibilities','secEnterprise,secLDAP,secWinAD,secSAPR3' );
        w.writeCharacters(auth);
        w.writeEndElement();
        w.writeEndElement();
        w.writeEndDocument();
        String xmlOutput = w.getXmlString();
        w.close();
       
        //string test = ' <some xml format data>';
        req.setEndpoint('http://www.my-end-point.aspx');
        req.setMethod('POST');
        req.setBody(xmlOutput);
         req.setCompressed(true); // otherwise we hit a limit of 32000

        try {
            res = http.send(req);
        } catch(System.CalloutException e) {
            System.debug('Callout error: '+ e);
            System.debug(res.getBody());
        }
        //System.debug(xmlOutput);
        return xmlOutput;
    }
}

Visual force page
<apex:page controller="Xmlretrive">
    <apex:form >
        <apex:pageBlock >
            <apex:pageBlockButtons location="top">
                <apex:commandButton value="Click here to submit" action="{!submit}" reRender="output,resoutput" />

            </apex:pageBlockButtons>
            <apex:pageBlockSection columns="1">
                <Apex:pageBlockSectionItem >
                    <apex:outputLabel >Enter the username :</apex:outputLabel>
                    <apex:inputText id="s1" value="{!userName}" />
                </Apex:pageBlockSectionItem>
                <apex:pageBlockSectionItem >
                    <apex:outputLabel >Password :</apex:outputLabel>
                    <apex:inputSecret id="s2" value="{!password}" />
                </apex:pageBlockSectionItem>
                <apex:pageBlockSectionItem >
                    <apex:outputLabel >Oauth URL</apex:outputLabel>
                    <apex:inputText id="s3" value="{!auth}" />
                </apex:pageBlockSectionItem>
            </apex:pageBlockSection>
        </apex:pageBlock>

        <apex:outputPanel id="output">

            <apex:pageBlock id="chkoutput" title="Output" rendered="{!displayxml}">
                <apex:pageBlockButtons location="top">
                    <apex:commandButton value="View XML" reRender="DisplayXML" />

                </apex:pageBlockButtons>
                
                <apex:pageBlockSection columns="1">
                    <Apex:pageBlockSectionItem >
                        <apex:outputLabel >User Name</apex:outputLabel>
                        <apex:outputLabel >{!userName }</apex:outputLabel>
                    </Apex:pageBlockSectionItem>
                    <Apex:pageBlockSectionItem >
                        <apex:outputLabel >Password</apex:outputLabel>
                        <apex:outputLabel >{!password}</apex:outputLabel>
                    </Apex:pageBlockSectionItem>

                    <Apex:pageBlockSectionItem >
                        <apex:outputLabel >Oauth URL</apex:outputLabel>
                        <apex:outputLabel >{!auth}</apex:outputLabel>
                    </Apex:pageBlockSectionItem>
                </Apex:pageBlockSection>

            </apex:pageBlock>
        </apex:outputPanel>
       


        <apex:outputPanel id="DisplayXML">
            <apex:outputPanel rendered="{!displayxml}">
                <apex:outputText value="{!Xml}"><h1> Request XML output</h1></apex:outputText><br/>
            </apex:outputPanel>
        </apex:outputPanel>
      
    </apex:form>
</apex:page>
Ashish_SFDCAshish_SFDC

Hi Bharath,


Passing user name and password for authentication not good approach.Using oauth is recommended .

public static void sendRequest(){
        HttpRequest req = new HttpRequest();
        HttpResponse res = new HttpResponse();
        Http http = new Http();

        req.setMethod('POST' ); // Method Type
        req.setEndpoint('foobar.com/someLink'); // Server Url
        req.setHeader('Content-Type', 'application/x-www-form-urlencoded'); // Content Type
        req.setBody('userName=' + EncodingUtil.urlEncode(userName, 'UTF-8') +  
        "&password=' + EncodingUtil.urlEncode(password, 'UTF-8') + '&cardNumber=' + 
        cardNumber); // Request Parameters
        try {
            res = http.send(req);
            if(res.getBody() != null){
                // Parse Response
            }
        } catch(Exception e) {
            System.debug('error: '+ e);
        }
    }

http://salesforce.stackexchange.com/questions/5686/sending-post-data-to-external-webservice


Regards,
Ashish