通过 HttpClient(C#) Post 方法向 SalesForce 发送数据,不工作
Sending Data to SalesForce by HttpClient(C#) Post Method, Not Working
我正在尝试使用 HttpClient 向 SalesFroce 发送一个 json 对象,但这表现得很奇怪...
首先我通过以下代码登录到 Salesforce
var sendPaylod = new Dictionary<string, string>
{
{"grant_type","password"},
{"client_id",s_clientId},
{"client_secret",s_clientSecrate},
{"username",s_username},
{"password",s_password}
};
HttpClient auth = new HttpClient();
HttpContent content = new FormUrlEncodedContent(sendPaylod);
HttpResponseMessage response = await auth.PostAsync(s_tokenRequestEndpointUrl, content);
string msg = await response.Content.ReadAsStringAsync();
Console.WriteLine(msg);
string authToken = (String)jsonObj["access_token"];
现在我有 authToken
作为不记名令牌将数据发送到 salesFroce
我正在关注
var obj = new { Director = "003e000001MQYjB",
CityName = "XXAA",
CityId = "000000",
RegionName = "India",
RegionId = "00000" };
string c_url = "URL to which data will sent";
var c_Obj = JsonConvert.SerializeObject(obj);
var c_content = new System.Net.Http.StringContent(c_Obj, Encoding.UTF8, "application/json");
HttpClient c_client = new HttpClient();
c_client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Authorization","Bearer "+authToken);
HttpContent c_content = new StringContent(c_Obj, Encoding.UTF8, "application/json");
c_content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
var c_response = await c_client.PostAsync(c_url, content);
var c_msg = await c_response.Content.ReadAsStringAsync();
现在正在收到以下响应...
"status": "success",
"recordId": "",
"message": ""
如果我在 Postman 中使用相同的 Bearer Token 并发送相同的 Json 对象,我会收到以下响应。
"status": "success",
"recordId": "a16e0000002qV6aE",
"message": ""
请帮忙解决这个问题。
修复下一个错误:
变化:
var c_response = await c_client.PostAsync(c_url, content);
至:
var contentType = new MediaTypeWithQualityHeaderValue("application/json");
c_client.DefaultRequestHeaders.Accept.Add(contentType);
var c_response = await c_client.PostAsync(c_url, c_content);
var c_msg = await c_response.Content.ReadAsStringAsync();
var result =JsonConvert.DeserializeObject<your return class>(c_msg);
并将您的授权更改为:
c_client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", authToken );
我正在尝试使用 HttpClient 向 SalesFroce 发送一个 json 对象,但这表现得很奇怪...
首先我通过以下代码登录到 Salesforce
var sendPaylod = new Dictionary<string, string>
{
{"grant_type","password"},
{"client_id",s_clientId},
{"client_secret",s_clientSecrate},
{"username",s_username},
{"password",s_password}
};
HttpClient auth = new HttpClient();
HttpContent content = new FormUrlEncodedContent(sendPaylod);
HttpResponseMessage response = await auth.PostAsync(s_tokenRequestEndpointUrl, content);
string msg = await response.Content.ReadAsStringAsync();
Console.WriteLine(msg);
string authToken = (String)jsonObj["access_token"];
现在我有 authToken
作为不记名令牌将数据发送到 salesFroce
我正在关注
var obj = new { Director = "003e000001MQYjB",
CityName = "XXAA",
CityId = "000000",
RegionName = "India",
RegionId = "00000" };
string c_url = "URL to which data will sent";
var c_Obj = JsonConvert.SerializeObject(obj);
var c_content = new System.Net.Http.StringContent(c_Obj, Encoding.UTF8, "application/json");
HttpClient c_client = new HttpClient();
c_client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Authorization","Bearer "+authToken);
HttpContent c_content = new StringContent(c_Obj, Encoding.UTF8, "application/json");
c_content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
var c_response = await c_client.PostAsync(c_url, content);
var c_msg = await c_response.Content.ReadAsStringAsync();
现在正在收到以下响应...
"status": "success",
"recordId": "",
"message": ""
如果我在 Postman 中使用相同的 Bearer Token 并发送相同的 Json 对象,我会收到以下响应。
"status": "success",
"recordId": "a16e0000002qV6aE",
"message": ""
请帮忙解决这个问题。
修复下一个错误:
变化:
var c_response = await c_client.PostAsync(c_url, content);
至:
var contentType = new MediaTypeWithQualityHeaderValue("application/json");
c_client.DefaultRequestHeaders.Accept.Add(contentType);
var c_response = await c_client.PostAsync(c_url, c_content);
var c_msg = await c_response.Content.ReadAsStringAsync();
var result =JsonConvert.DeserializeObject<your return class>(c_msg);
并将您的授权更改为:
c_client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", authToken );