分类

链接

2018 年 4 月
 1
2345678
9101112131415
16171819202122
23242526272829
30  

近期文章

热门标签

新人福利,免费薅羊毛

现在位置:    首页 > Salesforce > 正文
共享办公室出租
C#/.NET请求salesforce restful api示例
Salesforce 暂无评论 阅读(573)

先在salesforce里写一个restful api:

  1. @RestResource(urlMapping='/user/*')  
  2. global with sharing class Api_user_profile{  
  3.       
  4.     @HttpGet  
  5.     global static String getPersonBasicInformation() {  
  6.         List<Account> accList = DataBase.query('SELECT id,name FROM Account limit ' + Integer.valueOf(Math.floor(Math.random() * 10 + 1)));  
  7.         return JSON.serialize(accList);  
  8.     }
  9. }

再在c#/.net里调用salesforce restful api:

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Net;
  6. using System.IO;
  7. using Newtonsoft.Json;
  8. using Newtonsoft.Json.Linq;
  9.  
  10. namespace API_Demo
  11. {
  12.     class Program
  13.     {
  14.         static string TOKEN = "";
  15.         static string INSTANCE_URL = "";
  16.  
  17.  
  18.         static String USERNAME = "XXX@QQ.COM";
  19.         static String PASSWORD = "XXXX@XXX";
  20.         static String LOGINURL = "https://test.salesforce.com/services/oauth2/token";
  21.         static String CLIENTID = "XXXXXVG959Nd8JMmavTIGpPmGYDfRYlzefSanI4AXBO2ZsE0de2K8gRH3CRwrsNatmt_CXuQLkjC0hWOThZ7.YNi";
  22.         static String CLIENTSECRET = "111111938052357745"; 
  23.         //private static Header oauthHeader;
  24.         //private static Header prettyPrintHeader = new BasicHeader("X-PrettyPrint", "1");
  25.         static void Main(string[] args)
  26.         {
  27.             if (isAccessable())
  28.             {
  29.                 GetAccounts();
  30.             }
  31.         }
  32.  
  33.         public static string POST(byte[] data, string requestUrl, WebHeaderCollection header = null)
  34.         {
  35.             System.Net.ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Tls12 | System.Net.SecurityProtocolType.Tls11 | System.Net.SecurityProtocolType.Tls;
  36.  
  37.             string htmlStr = string.Empty;
  38.  
  39.             //创建一个客户端的Http请求实例
  40.             HttpWebRequest request = WebRequest.Create(requestUrl) as HttpWebRequest;
  41.             request.ContentType = "application/x-www-form-urlencoded";
  42.             request.Method = "POST";
  43.             request.ContentLength = data.Length;
  44.             if (header != null)
  45.             {
  46.                 request.Headers = header;
  47.             }
  48.  
  49.             Stream requestStream = request.GetRequestStream();
  50.             requestStream.Write(data, 0, data.Length);
  51.             requestStream.Close();
  52.  
  53.             //获取当前Http请求的响应实例
  54.             HttpWebResponse response = request.GetResponse() as HttpWebResponse;
  55.             Stream responseStream = response.GetResponseStream();
  56.             using (StreamReader reader = new StreamReader(responseStream, Encoding.GetEncoding("UTF-8")))
  57.             {
  58.                 htmlStr = reader.ReadToEnd();
  59.             }
  60.             responseStream.Close();
  61.  
  62.             return htmlStr;
  63.         }
  64.  
  65.  
  66.          public static string GET(string requestUrl, WebHeaderCollection header = null)
  67.         {
  68.             System.Net.ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Tls12 | System.Net.SecurityProtocolType.Tls11 | System.Net.SecurityProtocolType.Tls;
  69.  
  70.             string htmlStr = string.Empty;
  71.  
  72.             //创建一个客户端的Http请求实例
  73.             HttpWebRequest request = WebRequest.Create(requestUrl) as HttpWebRequest;
  74.             request.ContentType = "application/x-www-form-urlencoded";
  75.             request.Method = "GET";
  76.             
  77.             if (header != null)
  78.             {
  79.                 request.Headers = header;
  80.             } 
  81.  
  82.             //获取当前Http请求的响应实例
  83.             HttpWebResponse response = request.GetResponse() as HttpWebResponse;
  84.             Stream responseStream = response.GetResponseStream();
  85.             using (StreamReader reader = new StreamReader(responseStream, Encoding.GetEncoding("UTF-8")))
  86.             {
  87.                 htmlStr = reader.ReadToEnd();
  88.             }
  89.             responseStream.Close();
  90.  
  91.             return htmlStr;
  92.         }
  93.  
  94.         private static bool isAccessable()
  95.         {
  96.             try
  97.             {
  98.                 //SF基于TSL 1.2+
  99.                 System.Net.ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Tls12 | System.Net.SecurityProtocolType.Tls11 | System.Net.SecurityProtocolType.Tls;
  100.  
  101.  
  102.                 //Assemble the login request URL 
  103.                 //client_id is the Consumer Key
  104.                 //client_secret is the Consumer Secret
  105.                 //redirect_uri is the Callback URL.
  106.  
  107.                 StringBuilder content = new StringBuilder();
  108.  
  109.                 content.Append("grant_type=password");
  110.                 content.Append(">redirect_uri=https://localhost.manulife.com");
  111.                 content.Append(">client_id=" + CLIENTID);
  112.                 content.Append(">client_secret=" + CLIENTSECRET);
  113.                 content.Append(">username=" + USERNAME);
  114.                 content.Append(">password=" + PASSWORD);
  115.  
  116.  
  117.                 byte[] postBytes = Encoding.UTF8.GetBytes(content.ToString());
  118.                 var result = POST(postBytes, LOGINURL);
  119.  
  120.                 JObject json = JObject.Parse(result);
  121.  
  122.                 try
  123.                 {
  124.                     TOKEN = json.SelectToken("access_token").ToString();
  125.                     INSTANCE_URL = json.SelectToken("instance_url").ToString();
  126.  
  127.                     if (!string.IsNullOrWhiteSpace(TOKEN))
  128.                     {
  129.                         return true;
  130.                     }
  131.                 }
  132.                 catch (Exception)
  133.                 {
  134.                 }
  135.             }
  136.             catch (Exception)
  137.             {
  138.             }
  139.             return false;
  140.  
  141.         }
  142.  
  143.         private static bool GetAccounts()
  144.         {
  145.             WebHeaderCollection header = new WebHeaderCollection();
  146.             header.Add("Authorization", "OAuth " + TOKEN);
  147.             var result = GET(INSTANCE_URL + "/services/apexrest/user/getPersonBasicInformation", header);
  148.  
  149.             return true;
  150.  
  151.         }
  152.     }
  153. }

 

JAVA版示例:http://www.cnblogs.com/weizhen/p/6575522.html

POSTMAN示例:

 

 

============ 欢迎各位老板打赏~ ===========

【上篇】
【下篇】

本文版权归Bruce's Blog所有,转载引用请完整注明以下信息:
本文作者:Bruce
本文地址:C#/.NET请求salesforce restful api示例 | Bruce's Blog

发表评论

留言无头像?