2017-07-26 404 views
1

我試圖在我的項目上實現第三方服務,我需要進行一些身份驗證才能使用它們的API。C#RestSharp阻止請求重定向302

要做到這一點我需要執行自己的驗證網址POST請求,問題是,當我執行請求的服務器發送一個302的響應和Location頭,如今怪異的一部分:

我需要獲取此位置鏈接並閱讀查詢字符串以獲取我需要的信息。

我已經試過了我能想到的所有東西,甚至郵差也不會使用它。

這是我的代碼:

var client = new RestClient(APIBase + "/portal/rest/oauth2/login"); 

var request = new RestRequest(Method.POST); 

request.AddHeader("content-type", "application/x-www-form-urlencoded"); 
request.AddHeader("cache-control", "no-cache"); 
request.AddParameter("application/x-www-form-urlencoded", $"user={User}&password={Password}&client_id={ClientId}", ParameterType.RequestBody); 

var content = client.Execute(request); 

這是響應頭我管理使用的命令提示符下捲曲得到:

HTTP/1.1 302 Moved Temporarily 
Date: Wed, 26 Jul 2017 17:59:32 GMT 
Server: Apache-Coyote/1.1 
Location: LOCATION-LINK 
Content-Length: 0 

HTTP/1.1 200 OK 
Date: Wed, 26 Jul 2017 17:59:32 GMT 
Server: Apache-Coyote/1.1 
Accept-Ranges: bytes 
ETag: W/"20095-1497998126000" 
Last-Modified: Tue, 20 Jun 2017 22:35:26 GMT 
Content-Type: text/html 
Content-Length: 20095 
Vary: Accept-Encoding 

這可能嗎?

回答

1

您可以禁用RestSharp與重定向:

client.FollowRedirects = false; 

然後就是檢查響應狀態代碼是302,讀Location頭。