【问题标题】:Sending Data to SalesForce by HttpClient(C#) Post Method, Not Working通过 HttpClient(C#) Post 方法向 SalesForce 发送数据,不工作
【发布时间】:2021-01-07 14:00:11
【问题描述】:

我正在尝试使用 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 中使用相同的承载令牌并发送相同的 Json 对象,我会收到以下响应。

"status": "success",
"recordId": "a16e0000002qV6aE",
"message": ""

请帮忙解决这个问题。

【问题讨论】:

  • 试试这个:c_client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token);
  • 我试过了,不工作..程序没​​有在这一行之后进一步执行......并且执行将在那里冻结......我也无法得到这里的问题。
  • 对不起,我的意思是 - c_client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", authToken ); ——
  • 刚刚试了一下,结果一样。
  • var c_response = await c_client.PostAsync(c_url, content);更改为 c_client.PostAsync(c_url, c_content);

标签: c# rest salesforce dotnet-httpclient


【解决方案1】:

修复下一个错误:

变化:

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 ); 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-01-19
    • 2023-02-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多