2017-07-26 132 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头。