C#REST API调用-在邮递员中工作,而不在代码中工作 [英] C# REST API Call - Working in Postman, NOT in Code

查看:72
本文介绍了C#REST API调用-在邮递员中工作,而不在代码中工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些现有的代码正在工作,并且突然退出了.

我不知道为什么...

这是我的代码:

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,然后从代码内部失败,该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,并且收到以下答复(在邮递员中):

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

这里有什么想法吗?

解决方案

因此,答案最终是Salesforce终止了对TLS 1.0的支持,这是.NET 4.5中的默认设置.

在此链接中,他们提到这应该在2017年7月发生,但是不知何故它早就出现在我们的实例中了. https://help.salesforce.com/articleView?id=000221207&type=1

我们后来确认它可以在.NET 4.6中使用,但不能在4.5中使用...

将.NET 4.6默认设置为使用TLS 1.2.

这是一个非常简单的修复程序,花了很长时间才弄清楚.

添加了这一行代码,它立即起作用:

ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;

希望可以帮助遇到相同问题的其他人!

这种简单的单行解决方案需要几天才能找到.

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.

When I try to debug using Postman, I pass the URL directly and it works fine. 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#.

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

{
  "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.

In this link, they mention this should happen in July of 2017, but somehow it hit our instance early. 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...

Turns out .NET 4.6 defaults to using 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.

这篇关于C#REST API调用-在邮递员中工作,而不在代码中工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆