salesforce apex class call exteral webservice

时间:2023-03-09 22:31:50
salesforce apex class call exteral webservice

在项目中需要调用外面的Webservice,  从Salesforce往外写入其他系统。目前一般有两种方法。

1. 根据对方提供的wsdl文件生成apex class,直接实例化后调用其方法(测试成功),包括Java 和 .Net不同的平台提供的wsdl文件。

2.直接用HttpRequest去调用webservice.

下面就是参考这位朋友的博客,测试成功的案例。

http://www.cnblogs.com/yqskj/archive/2013/04/25/3041781.html

  HttpRequest req = new HttpRequest();
//Set HTTPRequest Method
req.setMethod('POST');
req.setEndpoint('https://api.authorize.net/soap/v1/Service.asmx');
req.setMethod('POST');
req.setHeader('Content-Type', 'text/xml; charset=utf-8');
req.setHeader('SOAPAction', 'https://api.authorize.net/soap/v1/CreateCustomerProfile');//
string b = '<?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">'+
'<soap:Body><CreateCustomerProfile xmlns="https://api.authorize.net/soap/v1/">'+
'<merchantAuthentication><name>Merchant name here</name>'+
'<transactionKey>Transaction Key here</transactionKey></merchantAuthentication>'+
'<profile><description>description</description>'+
'<email>sforce2009@gmail.com</email>'+
'<paymentProfiles>'+
'<CustomerPaymentProfileType><customerType>individual</customerType>'+
'<payment><creditCard><cardNumber>6011000000000012</cardNumber>'+
'<expirationDate>2009-12</expirationDate></creditCard>'+
'</payment></CustomerPaymentProfileType></paymentProfiles></profile>'+
'</CreateCustomerProfile></soap:Body></soap:Envelope>';
req.setBody(b);
Http http = new Http();
try {
//Execute web service call here
HTTPResponse res = http.send(req);
//Helpful debug messages
System.debug(res.toString());
System.debug('STATUS:'+res.getStatus());
System.debug('STATUS_CODE:'+res.getStatusCode());
//YOU CAN ALWAYS PARSE THE RESPONSE XML USING XmlStreamReader CLASS
} catch(System.CalloutException e) {
//Exception handling goes here....
}

http://blog.giovannimodica.com/post/call-a-net-wcf-service-from-salesforce

第一种方案,在最近的项目中,遇到一些问题,记录下来,给大家一个参考。

1.对方的wsdl文件打开后,要用SingleWsdl;

2.点击从Wsdl导入生产Apex类的时候一般会报错,请不要慌,一个一个解决;

a)Attribute error. 直接把报错的attribute的报错节点去掉;

b)salesforce目前不支持Soap12. 直接把soap12 header去掉,保留一个soap,替代所有已用的"soap12" 节点为 "soap".