Invoke WebService using Apex ( with/without Authorization) using Apex

  • Add your **Endpoint URL** in **remote site settings** as shown

    Navigation: Setup -> Security Controls -> Remote site Settings

  • Save Remote Site settings
  • lets consider **Apex__gen__class** as **wsdl** generated class
  • Don't know how to generate **Apex Class** from **WSDL** click here
  • Initiate Class as follows
  • ```java Apex_gen_class ws = new Apex_gen_class(); ```
  • if your webservice needs basic authentication, it should go like this
  • ```java // "ws" is an Object of Apex_gen_class, which initiated above

    ws.inputHttpHeaders_x = new Map <String, String>();
    String b64_string = EncodingUtil.base64Encode(Blob.valueOf('<user_name>:'));
    ws.inputHttpHeaders_x.put('Authorization', 'Basic ' + b64_string);

    // donot add this if authentication not need

    <li>Successfully authenticated! Now invoke your **webservice methods** as shown</li>
    
    ```java
    ws.<your_methods_name>(); // pass parameters if method takes arguments
    
  • Thats it! now assign method to corresponding data type, like string or List etc.
  • NOTE: if Authorization not needed simply dont add bullet #6.

    Summary

    So we have learned,

    1. How to create Remote Site Settings
    2. Generation of Apex Class from WSDL
    3. Iniatiation
    4. Basic Authorization
    5. webservice methods invoking