RestSharp 代码不起作用,但在 Postman 休息客户端中同样有效

RestSharp code does not work but same works in Postman rest client

我正在使用 RestSharp nuget 包向端点发出 http 请求。在我的例子中,端点是画面服务器。请求用于可信身份验证并获取身份验证票据作为响应。

使用 Restsharp 库的 C# 代码

[HttpPost]
        public ActionResult Logon(string url,string username)
        {
            string response = string.Empty;
            try
            {
                RestClient client = new RestClient(url);
                RestRequest logonRequest = new RestRequest("trusted");
                logonRequest.AddHeader("Content-Type", "application/x-www-form-urlencoded");
                logonRequest.AddHeader("charset", "utf-8");
                logonRequest.AddParameter("username", username,ParameterType.GetOrPost);
                IRestResponse restResponse = client.ExecuteAsPost(logonRequest,"POST");
                if (restResponse.StatusCode == System.Net.HttpStatusCode.OK)
                {
                    response = restResponse.Content;
                }
                else
                {
                    response = restResponse.ErrorMessage;
                }
            }
            catch (Exception ex)
            {
                response = ex.Message;                
            }
            return Json(response);
        }

url: https://servername/trusted

用户名:不存在的用户名

基本上我期待 -1 的响应,因为画面上不存在该用户。对 chrome 的 Postman rest 客户端的相同 post 请求完美无缺。但 restsharp 报告 unable to connect to server。有什么想法吗?

参考: https://onlinehelp.tableau.com/current/server/en-us/trusted_auth_webrequ.htm

这个也行。 https://community.tableau.com/thread/130481

似乎 C# 代码没有选择 IE 浏览器上配置的系统代理。所以我必须在 web.config 中指定代理并且它起作用了。

<system.net>
    <defaultProxy>
      <proxy proxyaddress="http://server:port"/>
    </defaultProxy>
</system.net>