在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. display:inline 遇上 li 无效? why?

    若制作导航栏时,使用列表li 的定义时,若想加上一个背景图 ,这时候若定义li的一个属性为:li{display:inline ; width:83px; height:30px;},则浏览器会无视后 ...

  2. POJ 2388(排序)

    http://poj.org/problem?id=2388 题意:就N个数的中位数. 思路:用快排就行了.但我没用快排,我自己写了一个堆来做这个题.主要还是因为堆不怎么会,这个拿来练练手. #inc ...

  3. Unity3d运行时动态修改材质

    void Start () { const string MainTexVariableName = "_MainTex"; var renders = gameObject.Ge ...

  4. 用C#读取txt文件的方法

    1.使用FileStream读写文件 文件头: using System;using System.Collections.Generic;using System.Text;using System ...

  5. codeforces 518B. Tanya and Postcard 解题报告

    题目链接:http://codeforces.com/problemset/problem/518/B 题目意思:给出字符串 s 和 t,如果 t 中有跟 s 完全相同的字母,数量等于或者多过 s,就 ...

  6. 如何获得images.xcassets 中图片的路径?

    UIImage加载图片的方式以及Images.xcassets对于加载方法的影响 重点: Images.xcassets中的图片资源只能通过imageNamed:方法加载,通过NSBundle的pat ...

  7. 单独编译osgQt模块 Qt moc

    从alphapixel网站下载了OSG3.0.1VS2010x64版本的库,但是里面不包括osgQt模块,于是得自己编译 *************osgQtx64.zip工程文件可以去本博客园的“文 ...

  8. ios block中引用self

    __block __weak typeof(self) tmpSelf = self; ^(){ tmpSelf...... }

  9. 图像特征提取之LBP特征

    LBP(Local Binary Pattern,局部二值模式)是一种用来描述图像局部纹理特征的算子:它具有旋转不变性和灰度不变性等显著的优点.它是首先由T. Ojala, M.Pietik?inen ...

  10. http协议之request

    一.请求的基本格式 请求的基本格式包括请求行,请求头,请求实体三部分.例如:GET /img/bd_logo1.png HTTP/1.1Accept: */*Referer: http://www.b ...