【发布时间】:2018-08-22 09:35:30
【问题描述】:
我有一些现有的代码正在运行并且突然退出。
我不知道为什么......
这是我的代码:
public static string RequestToken(string u, string pw)
{
string result = string.Empty;
string strUrl = "https://xxx.cloudforce.com/services/oauth2/token?grant_type=password&client_id=XXXX&client_secret=XXXX&username=" + u + "&password=" + pw;
HttpWebRequest tokenRequest = WebRequest.Create(strUrl) as HttpWebRequest;
Debug.Print(strUrl);
tokenRequest.Method = "POST";
try
{
using (HttpWebResponse tokenResponse = tokenRequest.GetResponse() as HttpWebResponse)
{
if (tokenResponse.StatusCode != HttpStatusCode.OK)
throw new Exception(String.Format(
"Server error (HTTP {0}: {1}).",
tokenResponse.StatusCode,
tokenResponse.StatusDescription));
DataContractJsonSerializer jsonSerializer2 = new DataContractJsonSerializer(typeof(ResponseAuthentication));
object objTokenResponse = jsonSerializer2.ReadObject(tokenResponse.GetResponseStream());
ResponseAuthentication jsonResponseAuthentication = objTokenResponse as ResponseAuthentication;
result = jsonResponseAuthentication.strAccessToken;
}
}
catch (Exception ex)
{
Debug.Print(ex.InnerException.ToString());
}
return result;
}
我现在得到一个500 Internal Server Error,在此之前它可以正常工作。
当我尝试使用 Postman 进行调试时,我直接传递了 URL,它工作正常。即使我在代码中停止并使用完全相同的 URL,然后从代码内部失败,它也可以在 Postman 中工作,但不能在 C# 中。
从邮递员那里,我得到一个有序的……
{
"access_token": "XXXXXX",
"instance_url": "https://xxx.cloudforce.com",
"id": "https://login.salesforce.com/id/XXXXXX",
"token_type": "Bearer",
"issued_at": "XXXXXX",
"signature": "XXXXXX"
}
为了澄清,我尝试了 GET 请求而不是 POST 并且我收到了以下响应(在 Postman 中):
{
"error": "invalid_request",
"error_description": "must use HTTP POST"
}
这里有什么想法吗?
【问题讨论】:
-
你确定方法类型应该是 'POST' 吗?
-
@levent - 是的,这也是我在 Postman 中使用的...... GET 不起作用。
-
邮递员成功案例中的 request.contentType 是什么?
-
@levent - 适用于 Postman 中的任何内容类型。
form-data、x-www-form-urlencoded、raw或binary
标签: c# rest salesforce httpwebrequest