Salesforce Bulk API 基于.Net平台下的实施

在最近的salesforce实施项目中应用到Bulk API来做数据接口。顺便把实际应用的例子写下来。希望对做salesforce接口的朋友有借鉴作用。

一 参考网络牛人写好的Demo.

    下载地址:https://github.com/lfreeland/Salesforce-Bulk-API-Starter

    注意事项:

    1)  目前的salesforce 禁用了TLS1.0,所以这个代码要加上

          System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;

         

    2)  引入enterprise.wsdl 时候,目前是发现有个bug.

         不清楚怎么引用webservice的朋友,可以先借鉴一下这个好博客

         http://www.cnblogs.com/mingmingruyuedlut/p/3493791.html

         需要修改reference.cs里面的两个地方,把[][]二维数组改为[] , 方能避免编译错误。

   3)关键步骤

        a). Connected and get sessionId

            

 1         private void Login()
 2         {
 3             System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;
 4             _sfService = new SFDC.SforceService();
 5             _sfService.Url = _LoginURL;
 6             _loginResult = _sfService.login(_UserName, _Password);
 7             _sfService.Url = _loginResult.serverUrl;
 8             SessionID = _loginResult.sessionId;
 9             _urlheader = "https://" + _sfService.Pod + ".salesforce.com";
10             //SforceService sfService = new SforceService();
11             _sfService.SessionHeaderValue = new SessionHeader();
12             _sfService.SessionHeaderValue.sessionId = _loginResult.sessionId;
13 
14         }
View Code

 

        b). Create Job

            

 1         public Job CreateJob(CreateJobRequest createJobRequest)
 2         {
 3             String jobRequestXML =
 4             @"<?xml version=""1.0"" encoding=""UTF-8""?>
 5              <jobInfo xmlns=""http://www.force.com/2009/06/asyncapi/dataload"">
 6                <operation>{0}</operation>
 7                <object>{1}</object>
 8                {3}
 9                <contentType>{2}</contentType>
10              </jobInfo>";
11 
12             String externalField = String.Empty;
13 
14             if (String.IsNullOrWhiteSpace(createJobRequest.ExternalIdFieldName) == false)
15             {
16                 externalField = "<externalIdFieldName>" + createJobRequest.ExternalIdFieldName + "</externalIdFieldName>";
17             }
18 
19             jobRequestXML = String.Format(jobRequestXML,
20                                           createJobRequest.OperationString,
21                                           createJobRequest.Object,
22                                           createJobRequest.ContentTypeString,
23                                           externalField);
24 
25             String createJobUrl = "https://" + _sfService.Pod + ".salesforce.com/services/async/31.0/job";
26 
27             String resultXML = invokeRestAPI(createJobUrl, jobRequestXML);
28 
29             return Job.Create(resultXML);
30         }
View Code

 

        c). Create BatchRequest

       

 1         public Batch CreateBatchRequest(string JobID, string filepath)
 2         {
 3             HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(_urlheader + "/services/async/31.0/job/" + JobID + "/batch");
 4             request.Method = WebRequestMethods.Http.Post;
 5             request.ContentType = "text/csv; charset=GB2312";
 6             request.Headers.Add("X-SFDC-Session", SessionID);
 7             request.KeepAlive = false;
 8             request.UserAgent = ".NET Framework Test Client";
 9             //"C:\SalesforceStudy\Project_Code\MyAccountImportData.csv"
10             byte[] byteArray = File.ReadAllBytes(filepath);
11             request.ContentLength = byteArray.Length;
12             using (var writeStream = request.GetRequestStream())
13             {
14                 writeStream.Write(byteArray, 0, byteArray.Length);
15             }
16             using (var response = (HttpWebResponse)request.GetResponse())
17             {
18                 using (var responseStream = response.GetResponseStream())
19                 {
20                     if (responseStream != null)
21                         using (var reader = new StreamReader(responseStream))
22                         {
23                             string responseValue = reader.ReadToEnd();
24                             return Batch.CreateBatch(responseValue);
25                         }
26                 }
27             }
28             return null;
29         }
View Code

 

转载于:https://www.cnblogs.com/shaotianyi/p/7474031.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值