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

时间:2017-08-11 18:37:23

标签: tableau postman restsharp

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

使用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的响应,因为该用户在tableau上不存在。邮递员休息客户端上相同的帖子请求完美无瑕。但是restsharp报告unable to connect to server。有什么想法吗?

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

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

1 个答案:

答案 0 :(得分:0)

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

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