2017-08-11 117 views
0

我正在使用RestSharp nuget包向端點發出http請求。在我的情況下,端點是tableau服務器。請求是用於可信身份驗證並獲取身份驗證票據作爲響應。RestSharp代碼不起作用,但在Postman休息客戶端中工作相同

使用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); 
     } 

網址:https://servername/trusted

用戶名:用戶名誰不存在

基本上我期待的-1的響應,因爲該用戶不存在於畫面上。郵差休息客戶端上的相同郵件請求完美無瑕。但restsharp報告unable to connect to server。有什麼想法嗎 ?

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

這一個作品了。 https://community.tableau.com/thread/130481

回答

0

似乎C#代碼沒有選擇在IE瀏覽器上配置的系統代理。所以我不得不在web.config中指定代理並且它工作。

<system.net> 
    <defaultProxy> 
     <proxy proxyaddress="http://server:port"/> 
    </defaultProxy> 
</system.net> 
相關問題