现在位置:
首页 > Salesforce > 正文
C#/.NET请求salesforce restful api示例
先在salesforce里写一个restful api:
- @RestResource(urlMapping='/user/*')
- global with sharing class Api_user_profile{
- @HttpGet
- global static String getPersonBasicInformation() {
- List<Account> accList = DataBase.query('SELECT id,name FROM Account limit ' + Integer.valueOf(Math.floor(Math.random() * 10 + 1)));
- return JSON.serialize(accList);
- }
- }
再在c#/.net里调用salesforce restful api:
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Net;
- using System.IO;
- using Newtonsoft.Json;
- using Newtonsoft.Json.Linq;
- namespace API_Demo
- {
- class Program
- {
- static string TOKEN = "";
- static string INSTANCE_URL = "";
- static String USERNAME = "XXX@QQ.COM";
- static String PASSWORD = "XXXX@XXX";
- static String LOGINURL = "https://test.salesforce.com/services/oauth2/token";
- static String CLIENTID = "XXXXXVG959Nd8JMmavTIGpPmGYDfRYlzefSanI4AXBO2ZsE0de2K8gRH3CRwrsNatmt_CXuQLkjC0hWOThZ7.YNi";
- static String CLIENTSECRET = "111111938052357745";
- //private static Header oauthHeader;
- //private static Header prettyPrintHeader = new BasicHeader("X-PrettyPrint", "1");
- static void Main(string[] args)
- {
- if (isAccessable())
- {
- GetAccounts();
- }
- }
- public static string POST(byte[] data, string requestUrl, WebHeaderCollection header = null)
- {
- System.Net.ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Tls12 | System.Net.SecurityProtocolType.Tls11 | System.Net.SecurityProtocolType.Tls;
- string htmlStr = string.Empty;
- //创建一个客户端的Http请求实例
- HttpWebRequest request = WebRequest.Create(requestUrl) as HttpWebRequest;
- request.ContentType = "application/x-www-form-urlencoded";
- request.Method = "POST";
- request.ContentLength = data.Length;
- if (header != null)
- {
- request.Headers = header;
- }
- Stream requestStream = request.GetRequestStream();
- requestStream.Write(data, 0, data.Length);
- requestStream.Close();
- //获取当前Http请求的响应实例
- HttpWebResponse response = request.GetResponse() as HttpWebResponse;
- Stream responseStream = response.GetResponseStream();
- using (StreamReader reader = new StreamReader(responseStream, Encoding.GetEncoding("UTF-8")))
- {
- htmlStr = reader.ReadToEnd();
- }
- responseStream.Close();
- return htmlStr;
- }
- public static string GET(string requestUrl, WebHeaderCollection header = null)
- {
- System.Net.ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Tls12 | System.Net.SecurityProtocolType.Tls11 | System.Net.SecurityProtocolType.Tls;
- string htmlStr = string.Empty;
- //创建一个客户端的Http请求实例
- HttpWebRequest request = WebRequest.Create(requestUrl) as HttpWebRequest;
- request.ContentType = "application/x-www-form-urlencoded";
- request.Method = "GET";
- if (header != null)
- {
- request.Headers = header;
- }
- //获取当前Http请求的响应实例
- HttpWebResponse response = request.GetResponse() as HttpWebResponse;
- Stream responseStream = response.GetResponseStream();
- using (StreamReader reader = new StreamReader(responseStream, Encoding.GetEncoding("UTF-8")))
- {
- htmlStr = reader.ReadToEnd();
- }
- responseStream.Close();
- return htmlStr;
- }
- private static bool isAccessable()
- {
- try
- {
- //SF基于TSL 1.2+
- System.Net.ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Tls12 | System.Net.SecurityProtocolType.Tls11 | System.Net.SecurityProtocolType.Tls;
- //Assemble the login request URL
- //client_id is the Consumer Key
- //client_secret is the Consumer Secret
- //redirect_uri is the Callback URL.
- StringBuilder content = new StringBuilder();
- content.Append("grant_type=password");
- content.Append(">redirect_uri=https://localhost.manulife.com");
- content.Append(">client_id=" + CLIENTID);
- content.Append(">client_secret=" + CLIENTSECRET);
- content.Append(">username=" + USERNAME);
- content.Append(">password=" + PASSWORD);
- byte[] postBytes = Encoding.UTF8.GetBytes(content.ToString());
- var result = POST(postBytes, LOGINURL);
- JObject json = JObject.Parse(result);
- try
- {
- TOKEN = json.SelectToken("access_token").ToString();
- INSTANCE_URL = json.SelectToken("instance_url").ToString();
- if (!string.IsNullOrWhiteSpace(TOKEN))
- {
- return true;
- }
- }
- catch (Exception)
- {
- }
- }
- catch (Exception)
- {
- }
- return false;
- }
- private static bool GetAccounts()
- {
- WebHeaderCollection header = new WebHeaderCollection();
- header.Add("Authorization", "OAuth " + TOKEN);
- var result = GET(INSTANCE_URL + "/services/apexrest/user/getPersonBasicInformation", header);
- return true;
- }
- }
- }
JAVA版示例:http://www.cnblogs.com/weizhen/p/6575522.html
POSTMAN示例:
============ 欢迎各位老板打赏~ ===========
与本文相关的文章
- · Salesforce VisualForce 使用apex:actionFunction调用后台方法
- · How to Call Salesforce API from Lightning Component
- · apex highlight userDefineLang for notepad++
- · 详解 Salesforce 15 和 18 位的ID
- · Salesforce Test class注意事项
- · salesforce Milestone clock is not running
- · salesforce apex traced the number of query
- · salesforce antscript无法拉取完整的profile
- · salesforce lightning report取URL参数
- · salesforce test class用现有数据测试
- · salesforce report get url parameters
- · salesforce display trigger error