在Salesforce中可以创建Web Service供外部系统调用,并且可以以SOAP或者REST方式向外提供调用接口,接下来的内容将详细讲述一下用SOAP的方式创建Web Service并且用Asp.net的程序进行简单的调用。

1):在Salesforce中创建如下Class

【注:要想使其成为web service,那么class一定要定义成global的,具体的方法要用 webService static 修饰】

【代码中省略了GenerateAccountFromXmlInfo方法的具体实现,细节请看:http://www.cnblogs.com/mingmingruyuedlut/p/3497646.html 】

global class SFAccountWebService {

    webService static string UpsertAccount(String accountXmlInfo) {
Account currentAcc = GenerateAccountFromXmlInfo(accountXmlInfo);
try
{
Account acc = [Select Id From Account a Where AccountNumber =: currentAcc.AccountNumber];
if(acc != null){
currentAcc.Id = acc.Id;
}
upsert currentAcc;
return 'true';
}
catch(exception ex){
return 'false';
}
} private static Account GenerateAccountFromXmlInfo(String accountXmlInfo){
Account currentAcc = new Account();
// Parse the xml info to generate the Account Object
return currentAcc;
} }

2):在保存好上述的class之后,我们到setup --> build --> develop --> apex classes 中找到刚刚保存的class,我们会发现在对应的Action中有WSDL这个选项,此选项就是Salesforce默认所提供的将Web Service的class转化成WSDL文件。如下图所示

3):点击上图的WSDL按钮,会看到如下界面,这里显示的是生成的WSDL文件的详细信息,我们点击鼠标右键,将此文件保存到本地,这里姑且取名为SFAccountWebService.wsdl

4):我们可以简单的创建一个WebApplication的project,如下图所示,点击Reference后进行Add Web Reference

5):接上图,在URL的输入框中选择我们刚刚生成的WSDL文件,填写好右下角的Web Service Name,然后点击Add Reference按钮,这样我们就已经应用到了我们所刚刚生成的Web Service,是不是很简单呢~~

6):由于我们是通过外部系统去访问Salesforce内部的资源,那么不可逃避的首先便是认证,也就是说,我们必须首先通过Salesforce的认证,获取登陆用户的SessionId,然后此SessionId将作为此后每次访问Salesforce内部资源的认证标识,只有这样我们才能顺利调用到我们对外开放的Web Service。

如何在外部系统进行登陆认证获取对应的SessionId呢?这就涉及到了Salesforce默认提供的另外一个Web Service,如下图所示:

【setup --> build --> develop --> api --> partner wsdl --> generate partner wsdl】

7):将此WSDL文件以相同的方式保存到本地,这里姑且取名为 SFCommonService.wsdl

8):在对应Web Application的project中以相同的方式引用此文件

9):可以简单的看一下最终的引用状态,如下图所示

10):如何进行调用对应的Web Service呢? 请看如下代码

【login方法的两个参数是:用户名和密码。注:这里的密码是 用户密码 + 所对应的token】

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls; using TestSFWebServiceApplication.SFCommonWebService;
using TestSFWebServiceApplication.SFAccountWebService; namespace TestSFWebServiceApplication
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//Call login function to get the session id
SforceService sfService = new SforceService();
LoginResult result = sfService.login(@"******************", @"***********************");
string currentSessionId = result.sessionId; //Call our web service function
string accountXmlInfo = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><Accounts><Account><AccountName>WebServiceTest001</AccountName><AccountNumber>AAA000-009</AccountNumber></Account></Accounts>";
SFAccountWebServiceService sfAccountService = new SFAccountWebServiceService();
SFAccountWebService.SessionHeader sfAccountHeader = new SFAccountWebService.SessionHeader();
sfAccountHeader.sessionId = currentSessionId;
sfAccountService.SessionHeaderValue = sfAccountHeader;
string upsertResult = sfAccountService.UpsertAccount(accountXmlInfo);
}
}
}

11):那么在Salesforce中如何引用外部系统所提供的Web Service呢?请看下图,将外部的WSDL文件生成Salesforce中所对应的Class

之后的具体调用这里就不详细列举了,请看如下链接:http://www.cnblogs.com/mingmingruyuedlut/p/3512262.html

更多内容请看如下链接:

http://shivasoft.in/blog/salesforce/consume-salesforce-web-service-in-c-net-application/

http://shivasoft.in/blog/salesforce/create-a-custom-web-service-in-salesforce-and-consume-it-in-c-net-application/

12):当然了我们可以创建对应的Rest Service供外部系统调用

12.1):在Salesforce中创建对应的Class,代码如下所示

@RestResource(urlMapping='/SFEricSunTestRestService')
global class SFEricSunTestRestService { @HttpGet
global static string GetTestRestInfo(){
string testInfo = 'Hello Rest Service.';
return testInfo;
}
}

这里的标识为 @RestResource(urlMapping='/.....') 这样所形成的Rest Service Uri 格式为 https://cs5.salesforce.com/services/apexrest/SFEricSunTestRestService

Http 的 GET 和 DELETE 方法不支持传递参数

12.2):在外部系统调用上面的Rest Service(Get 方法),代码如下所示

        private void TestSFRestService()
{
SforceService sfService = new SforceService();
LoginResult result = sfService.login(@"......", @"......");
string currentSessionId = result.sessionId; string restServiceURI = @"https://cs5.salesforce.com/services/apexrest/SFEricSunTestRestService"; WebRequest request = WebRequest.Create(restServiceURI);
request.Method = "GET";
//request.ContentType = "application/xml";
request.Headers.Add("Authorization:Bearer " + currentSessionId); WebResponse response = request.GetResponse(); if (response != null)
{
StreamReader reader = new StreamReader(response.GetResponseStream());
string msg = reader.ReadToEnd();
} }

12.3):也可以进一步扩展,代码如下

@RestResource(urlMapping='/SFEricSunTestRestService/*')
global class SFEricSunTestRestService { @HttpDelete
global static string doDelete() {
RestRequest req = RestContext.request;
String name = req.requestURI.substring(req.requestURI.lastIndexOf('/')+);
return 'Delete ' + name;
} @HttpGet
global static string doGet() {
RestRequest req = RestContext.request;
String name = req.requestURI.substring(req.requestURI.lastIndexOf('/')+);
return 'Get ' + name;
} @HttpPost
global static String doPost(String name, String age) {
return 'Post' + name + ' ' + age;
} }

12.4):在外部系统调用上面的Rest Service,代码如下所示

        private void TestSFRestService()
{
SforceService sfService = new SforceService();
LoginResult result = sfService.login(@"....", @"....");
string currentSessionId = result.sessionId; string restServiceURI = @"https://cs5.salesforce.com/services/apexrest/SFEricSunTestRestService/MyName"; WebRequest request = WebRequest.Create(restServiceURI);
request.Method = "GET";
//request.ContentType = "application/xml";
request.Headers.Add("Authorization:Bearer " + currentSessionId); WebResponse response = request.GetResponse(); if (response != null)
{
StreamReader reader = new StreamReader(response.GetResponseStream());
string msg = reader.ReadToEnd();
} }

这是对GET方法的调用,我们将Name放在了Url的最后面传递给Service中,如果想调用Delete的方法,那么将request.Method = "GET";修改成为request.Method = "DELETE";

这里的Post方法有些特殊,需要传递name和age参数(实质是就是将实参加入到request的body中一起发到目的端去请求),调用代码如下所示

这里提供了SOUP UI的截图如下所示:

12.5):如果我们想在Salesforce内部去调用对应的Rest Service,那么可以用如下方式

https://developer.salesforce.com/forums/#!/feedtype=SINGLE_QUESTION_SEARCH_RESULT&id=906F000000099zbIAA

更加详细的信息请看如下链接:

http://www.salesforce.com/us/developer/docs/apexcode/index_Left.htm#CSHID=apex_web_services_methods.htm|StartTopic=Content%2Fapex_web_services_methods.htm|SkinName=webhelp

https://developer.salesforce.com/page/Creating_REST_APIs_using_Apex_REST

在Salesforce中创建Web Service供外部系统调用的更多相关文章

  1. VS2010下创建WEBSERVICE,第二天 ----你会在C#的类库中添加web service引用吗?

    本文并不是什么高深的文章,只是VS2008应用中的一小部分,但小部分你不一定会,要不你试试: 本人对于分布式开发应用的并不多,这次正好有一个项目要应用web service,我的开发环境是vs2008 ...

  2. 你会在C#的类库中添加web service引用吗?

    本文并不是什么高深的文章,只是VS2008应用中的一小部分,但小部分你不一定会,要不你试试: 本人对于分布式开发应用的并不多,这次正好有一个项目要应用web service,我的开发环境是vs2008 ...

  3. IntelliJ IDEA中创建Web聚合项目(Maven多模块项目)

    Eclipse用多了,IntelliJ中创建Maven聚合项目可能有小伙伴还不太熟悉,我们今天就来看看. IntelliJ中创建普通的Java聚合项目相对来说比较容易,不会涉及到web操作,涉及到we ...

  4. 使用Axis2创建Web Service

    Axis2是新一代Web Service开发工具,目前最新版本是1.5.本文主要介绍如何用Axis2创建Web Service. 首先下载二进制包和war包,将war包复制到Tomcat的webapp ...

  5. 38.IntelliJ IDEA中创建Web聚合项目(Maven多模块项目)

    转自:https://blog.csdn.net/u012702547/article/details/77431765 Eclipse用多了,IntelliJ中创建Maven聚合项目可能有小伙伴还不 ...

  6. IntelliJ IDEA中创建Web聚合项目(Maven多模块项目)(转载)

    创建parent项目 1.打开IDEA,注意这里不要勾选模板,用模板创建过maven项目的小伙伴都知道模板创建项目非常慢,所以这里不要选模板,需要的文件夹我们后面自己来创建就可以了.所以这个页面直接点 ...

  7. win7 gsoap与vs2010 c++创建Web Service

    ---恢复内容开始--- 之前曾经编写过简单的样例,很久没有碰过,发现已经全部忘记,如今又需要重新巩固一下. 首先是下载gsoap,无法访问官方下载页面,只能在网上搜索,找到一个2.8版本存入云盘以防 ...

  8. 微软BI 之SSIS 系列 - 在 SSIS 中使用 Web Service 以及 XML 解析

    开篇介绍 Web Service 的用途非常广几乎无处不在,像各大门户网站上的天气预报使用到的第三方 Web Service API,像手机客户端和服务器端的交互等都可以通过事先设计好的 Web Se ...

  9. ASP.NET 5系列教程 (六): 在 MVC6 中创建 Web API

    ASP.NET 5.0 的主要目标之一是统一MVC 和 Web API 框架应用. 接下来几篇文章中您会了解以下内容: ASP.NET MVC 6 中创建简单的web API. 如何从空的项目模板中启 ...

随机推荐

  1. poj 2251

    http://poj.org/problem?id=2251 一道简单的BFS,只不过是二维数组,变三维数组,也就在原来基础上加了两个方向. 题意就是从S走到E,#不能走. #include < ...

  2. Javascript之setTimeout

    参考:http://codethoughts.info/javascript/2015/07/06/javascript-callbacks/

  3. 1. EasyUI 学习总结(一)——对话框dialog

    文章参考来源:http://www.cnblogs.com/xdp-gacl/p/4075079.html 感谢博主的分享,写得非常精细,我在这边给看过的做一个记录. 一.EasyUI下载 使用eas ...

  4. Django~Settings.py

    配置 数据库 默认sqlite, 支持Mysql,postgresql,oracle 更改时区 查看表结构 .schema (SQLite), display the tables Django cr ...

  5. BestCoder20 1002.lines (hdu 5124) 解题报告

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5124 题目意思:给出 n 条线段,每条线段用两个整数描述,对于第 i 条线段:xi,yi 表示该条线段 ...

  6. linux 配置tomcat运行远程监控(JMX)

    在实际使用中,我们经常要监控tomcat的运行性能.需要配置相应的参数提供远程连接来监控tomcat服务器的性能.本文详细介绍如何一步一步的配置tomcat相应参数.允许远程连接监控. 工具/原料 v ...

  7. 【leetcode】Decode Ways(medium)

    A message containing letters from A-Z is being encoded to numbers using the following mapping: 'A' - ...

  8. HDU 4387 Stone Game (博弈)

    题目:传送门. 题意:长度为N的格子,Alice和Bob各占了最左边以及最右边K个格子,每回合每人可以选择一个棋子往对面最近的一个空格移动.最先不能移动的人获得胜利. 题解: k=1时 很容易看出,n ...

  9. java内存分配原理

    一般Java在内存分配时会涉及到以下区域: ◆寄存器:我们在程序中无法控制 ◆栈:存放基本类型的数据和对象的引用,但对象本身不存放在栈中,而是存放在堆中 ◆堆:存放用new产生的数据 ◆静态域:存放在 ...

  10. 【Python升级录】--基础知识

    创建角色成功! 正在载入python........ [python介绍] python是一门动态解释性的强类型定义语言. python的创始人为吉多·范罗苏姆(Guido van Rossum).1 ...