C# 使用自定义 header 调用 Salesforce Rest Api
C# Calling Salesforce Rest Api with custom header
我正在尝试使用 Rest Api 在 Salesforce 中插入一个新联系人,但不幸的是,使用 Postman 收到 400 错误请求 exception.While 一切都很完美,但通过代码它抛出异常。有人指导我我错了。
string new_Contact_url = ConfigurationManager.AppSettings["SalesForceBaseUrl"] + "sobjects/contact";
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(new_Contact_url);
request.Method = "POST";
request.Headers.Add("Authorization", "Access_Token");
request.ContentType = "application/json; charset=utf-8";
var keyValues = new Dictionary<string, string>
{
{ "LastName", "Nafees" },
{ "Email", "email "},
{ "Tools_User_ID__c", "login_id" }
};
JavaScriptSerializer js = new JavaScriptSerializer();
string postData = js.Serialize(keyValues);
byte[] byteArray = Encoding.UTF8.GetBytes(postData.Trim());
request.ContentLength = byteArray.Length;
Stream dataStream = request.GetRequestStream();
dataStream.Write(byteArray, 0, byteArray.Length);
dataStream.Close();
WebResponse response = request.GetResponse();
dataStream = response.GetResponseStream();
StreamReader reader = new StreamReader(dataStream);
var resp = reader.ReadToEnd();
var serializer = new JavaScriptSerializer();
var responseBody = (IDictionary<string, object>)serializer.DeserializeObject(resp);
if (!string.IsNullOrEmpty(responseBody["access_token"].ToString()))
{
string id = responseBody["Id"].ToString();
}
Salesforce 最近从支持 TLS1.0 切换到 TLS1.2。
在你的 C# 代码中你可以添加这个:
using System.Net;
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
ServicePointManager 部分应该在您发送请求之前。
我正在尝试使用 Rest Api 在 Salesforce 中插入一个新联系人,但不幸的是,使用 Postman 收到 400 错误请求 exception.While 一切都很完美,但通过代码它抛出异常。有人指导我我错了。
string new_Contact_url = ConfigurationManager.AppSettings["SalesForceBaseUrl"] + "sobjects/contact";
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(new_Contact_url);
request.Method = "POST";
request.Headers.Add("Authorization", "Access_Token");
request.ContentType = "application/json; charset=utf-8";
var keyValues = new Dictionary<string, string>
{
{ "LastName", "Nafees" },
{ "Email", "email "},
{ "Tools_User_ID__c", "login_id" }
};
JavaScriptSerializer js = new JavaScriptSerializer();
string postData = js.Serialize(keyValues);
byte[] byteArray = Encoding.UTF8.GetBytes(postData.Trim());
request.ContentLength = byteArray.Length;
Stream dataStream = request.GetRequestStream();
dataStream.Write(byteArray, 0, byteArray.Length);
dataStream.Close();
WebResponse response = request.GetResponse();
dataStream = response.GetResponseStream();
StreamReader reader = new StreamReader(dataStream);
var resp = reader.ReadToEnd();
var serializer = new JavaScriptSerializer();
var responseBody = (IDictionary<string, object>)serializer.DeserializeObject(resp);
if (!string.IsNullOrEmpty(responseBody["access_token"].ToString()))
{
string id = responseBody["Id"].ToString();
}
Salesforce 最近从支持 TLS1.0 切换到 TLS1.2。
在你的 C# 代码中你可以添加这个:
using System.Net;
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
ServicePointManager 部分应该在您发送请求之前。