[SalesForce] Apex callout exception when making hitting an external Service

I am looking at a strange exception when I make an apex callout to a external system. I am forming a very simple apex code to hit the endpoint using wsdl2Apex class. I get the below excception:

Exception:

System.CalloutException: Web service callout failed: WebService returned a SOAP Fault: [ISS.0088.9164] Access to WSDescriptor wsdl2apexClassName.webservices:wsdlName denied. faultcode=soapenv:Client faultactor=http://..endpoint

Apex Callout Code:

    wsdl2apexclassName sample = new wsdl2apexclassName();
wsdl2apexclassName.input req = new wsdl2apexclassName.input();
sample.endpoint_x = 'the end point goes here';

sample.inputHttpHeaders_x = new Map<String, String>();
sample.inputHttpHeaders_x.put('Username','username');
sample.inputHttpHeaders_x.put('Password','pass');
wsdl2apexclassName.response res = sample.functionwhichhasInvokeMethod(req);
system.debug('******' + res);

WSDL has these information in the beginning:

> <?xml version="1.0" encoding="UTF-8"?><wsdl:definitions
> xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" name="wsdlName"
> targetNamespace="http://URL"
> xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/"
> xmlns:http="http://schemas.xmlsoap.org/wsdl/http/"
> xmlns:soapjms="http://www.w3.org/2010/soapjms/"
> xmlns:tns="http://URL****"
> xmlns:xsd="http://www.w3.org/2001/XMLSchema"
> xmlns:mime="http://schemas.xmlsoap.org/wsdl/mime/"
> xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
> xmlns:soap12="http://schemas.xmlsoap.org/wsdl/soap12/">   
<wsdl:types>
......

DEbug log: CALLOUT_REQUEST

invokeMethod:[apex_schema_type_info=(http://endpoint, false, false), field_order_type_info=

(Input), Input=Input:[apex_schema_type_info=(http://targetURL, false, false), 

field_order_type_info=(listOfPfOrderOutboundIo), listOfPfOrderOutboundIo=null, 

listOfPfOrderOutboundIo_type_info=(listOfPfOrderOutboundIo, 

http://targetURL, null, 1, 1, 

false)], orderInput_type_info=(Input, 

http://targetURL, null, 1, 1, 

true)]::SOAPAction="SOAPAction" 

Accept=text/xml Username=usernamee**** User-Agent=SFDC-Callout/38.0 SFDC_STACK_DEPTH=1 

Password=**** Content-Type=text/xml; charset=UTF-8

Best Answer

The reason for this erro was , I was passing the username and password in the wrong way in Http header like below:

sample.inputHttpHeaders_x = new Map<String, String>();
sample.inputHttpHeaders_x.put('Username','username');
sample.inputHttpHeaders_x.put('Password','pass');

It should be added to the header using a property name 'authorization' and the username and password should be base64encoded as below

Blob headerValue = Blob.valueOf(username + ':' + password);
String authorizationHeader = 'BASIC ' + EncodingUtil.base64Encode(headerValue);    
sample.inputHttpHeaders_x.put('Authorization', authorizationHeader);

Reference - https://developer.salesforce.com/blogs/developer-relations/2009/01/using-basic-authentication-with-web-services.html

This has solved my problem. Hope it helps anybody else also.

Related Topic