简体   繁体   English

C#REST API调用 - 在Postman中工作,而不是在Code中

[英]C# REST API Call - Working in Postman, NOT in Code

I have some existing code that was working and all-of-a-sudden quit. 我有一些现有的代码正在工作,并且突然之间退出。

I can't figure out why... 我无法弄清楚为什么......

Here is my code: 这是我的代码:

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

I am now getting a 500 Internal Server Error where before this was working cleanly. 我现在得到500 Internal Server Error ,在此之前干净利落。

When I try to debug using Postman, I pass the URL directly and it works fine. 当我尝试使用Postman进行调试时,我直接传递了URL,它工作正常。 Even when I put a stop in the code and use the exact same URL that then fails from inside the code, it works in Postman, but not in C#. 即使我在代码中停止并使用完全相同的URL然后在代码内部失败,它也适用于Postman,但不适用于C#。

Out of Postman, I get an orderly... 走出邮递员,我得到了一个有序......

{
  "access_token": "XXXXXX",
  "instance_url": "https://xxx.cloudforce.com",
  "id": "https://login.salesforce.com/id/XXXXXX",
  "token_type": "Bearer",
  "issued_at": "XXXXXX",
  "signature": "XXXXXX"
}

To clarify, I have tried a GET request instead of POST and I receive the following response (in Postman): 为了澄清,我尝试了一个GET请求而不是POST ,我收到了以下响应(在Postman中):

{
  "error": "invalid_request",
  "error_description": "must use HTTP POST"
}

Any ideas here? 这里有什么想法?

So, the answer ended up being that Salesforce ended their support for TLS 1.0, which is the default in .NET 4.5. 因此,答案最终是Salesforce结束了对TLS 1.0的支持,这是.NET 4.5的默认设置。

In this link, they mention this should happen in July of 2017, but somehow it hit our instance early. 在这个链接中,他们提到这应该发生在2017年7月,但不知何故,它提前击中了我们的实例。 https://help.salesforce.com/articleView?id=000221207&type=1 https://help.salesforce.com/articleView?id=000221207&type=1

We later confirmed that it would work in .NET 4.6, but NOT in 4.5... 我们后来证实它可以在.NET 4.6中运行,但不适用于4.5 ......

Turns out .NET 4.6 defaults to using TLS 1.2. 原来.NET 4.6默认使用TLS 1.2。

It was a very simple fix, which took a LONG time to figure out. 这是一个非常简单的修复,花了很长时间才弄明白。

Added this one line of code and it immediately worked: 添加了这一行代码,它立即起作用:

ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;

Hope that helps someone else with the same problem! 希望能帮助别人解决同样的问题!

A little frustrating when such a simple one-line solution takes days to find. 当这样一个简单的单行解决方案需要数天才能找到时,有点令人沮丧。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM