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

 

 1  HttpRequest req = new HttpRequest();  
 2         //Set HTTPRequest Method  
 3         req.setMethod('POST');  
 4         req.setEndpoint('https://api.authorize.net/soap/v1/Service.asmx');  
 5         req.setMethod('POST');  
 6         req.setHeader('Content-Type', 'text/xml; charset=utf-8');  
 7         req.setHeader('SOAPAction', 'https://api.authorize.net/soap/v1/CreateCustomerProfile');//  
 8         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">'+  
 9                       '<soap:Body><CreateCustomerProfile xmlns="https://api.authorize.net/soap/v1/">'+  
10                       '<merchantAuthentication><name>Merchant name here</name>'+  
11                       '<transactionKey>Transaction Key here</transactionKey></merchantAuthentication>'+  
12 '<profile><description>description</description>'+  
13                       '<email>sforce2009@gmail.com</email>'+  
14                       '<paymentProfiles>'+  
15                       '<CustomerPaymentProfileType><customerType>individual</customerType>'+  
16 '<payment><creditCard><cardNumber>6011000000000012</cardNumber>'+  
17                       '<expirationDate>2009-12</expirationDate></creditCard>'+  
18                       '</payment></CustomerPaymentProfileType></paymentProfiles></profile>'+  
19                       '</CreateCustomerProfile></soap:Body></soap:Envelope>';  
20         req.setBody(b);  
21         Http http = new Http();  
22         try {  
23           //Execute web service call here         
24           HTTPResponse res = http.send(req);     
25           //Helpful debug messages  
26           System.debug(res.toString());  
27           System.debug('STATUS:'+res.getStatus());  
28           System.debug('STATUS_CODE:'+res.getStatusCode());  
29         //YOU CAN ALWAYS PARSE THE RESPONSE XML USING XmlStreamReader  CLASS  
30        } catch(System.CalloutException e) {  
31             //Exception handling goes here....  
32      }  
View Code

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".

 

posted @ 2017-09-04 17:20  shaotianyi  阅读(348)  评论(1编辑  收藏  举报