我正在获取一个cookie并使用它,继续模拟相应的浏览器步骤给出的步骤。由于自动重定向已关闭,因此我必须单独执行每个步骤,然后在下一步中使用它来获取响应头中的位置。经过三项此类操作后,响应的标头包含location字段,但它为null。使用浏览器执行相应的操作,其中包含每个重定向步骤中的实际位置。
private static Uri ObtainExecutor(Uri referer, Uri uri, Cookie cookie)
{
CookieContainer cookieJar = new CookieContainer();
cookieJar.Add(cookie);
HttpClientHandler handler = new HttpClientHandler
{
CookieContainer = cookieJar,
AllowAutoRedirect = false
};
HttpClient client = new HttpClient(handler);
client.DefaultRequestHeaders.Referrer = referer;
client.DefaultRequestHeaders.Host = uri.Host;
HttpResponseMessage response = client.GetAsync(uri).Result;
HttpResponseHeaders headers = response.Headers;
return new Uri(uri, response.Headers.Location);
}
我不知道如何进一步解决它。我比较了标头,没有发现任何相关性。我以相同的结果震惊了它,但无法掌握即将到来的软件包的超载。当然,我也用Google搜索了一个多星期。
我确定调用的执行方式会有细微的差别,但我看不到它。下面的示例连接到实际的服务器,并显示缺少重定向位置的位置。 (我对大样本表示歉意-我试图尽量减少样本,但确保不要遗漏我不清楚的相关细节。)
class Program
{
static void Main(string[] args)
{
const string url = "https://www.pensionsmyndigheten.se/service/login/login"
+ "?targetPage=https://www.pensionsmyndigheten.se/service/overview/"
+ "&failurePage=https://www.pensionsmyndigheten.se/service/login/error/login-failed"
+ "&cancelPage=https://www.pensionsmyndigheten.se/";
Uri uri = new Uri(url);
Cookie cookie = GenerateCookie(uri);
Uri discovery = DiscoverService(cookie);
Uri execution = ObtainExecutor(uri, discovery, cookie);
Uri conversation = ObtainConversor(uri, execution, cookie);
}
private static Cookie GenerateCookie(Uri uri)
{
CookieContainer cookieJar = new CookieContainer();
HttpClientHandler handler = new HttpClientHandler { CookieContainer = cookieJar };
HttpClient client = new HttpClient(handler);
HttpResponseMessage response = client.GetAsync(uri).Result;
HttpContent content = response.Content;
Cookie cookie = cookieJar.GetCookies(uri)
.Cast<Cookie>()
.First(e => e.Name == "pm_retention_urls");
return cookie;
}
private static Uri DiscoverService(Cookie cookie)
{
string url = "https://www.pensionsmyndigheten.se/service/login/discoresponse"
+ "?spId=default"
+ "&entityID=https%3A%2F%2Feid.legitimeringstjanst.se%2Fmobilt-bankid%2F";
Uri uri = new Uri(url);
CookieContainer cookieJar = new CookieContainer();
cookieJar.Add(cookie);
HttpClientHandler handler = new HttpClientHandler
{
CookieContainer = cookieJar,
AllowAutoRedirect = false
};
HttpClient client = new HttpClient(handler);
client.DefaultRequestHeaders.Referrer = uri;
client.DefaultRequestHeaders.Host = uri.Host;
HttpResponseMessage response = client.GetAsync(uri).Result;
HttpResponseHeaders headers = response.Headers;
return headers.Location;
}
private static Uri ObtainExecutor(Uri referer, Uri uri, Cookie cookie)
{
CookieContainer cookieJar = new CookieContainer();
cookieJar.Add(cookie);
HttpClientHandler handler = new HttpClientHandler
{
CookieContainer = cookieJar,
AllowAutoRedirect = false
};
HttpClient client = new HttpClient(handler);
client.DefaultRequestHeaders.Referrer = referer;
client.DefaultRequestHeaders.Host = uri.Host;
HttpResponseMessage response = client.GetAsync(uri).Result;
HttpResponseHeaders headers = response.Headers;
return new Uri(uri, response.Headers.Location);
}
private static Uri ObtainConversor(Uri referer, Uri uri, Cookie cookie)
{
CookieContainer cookieJar = new CookieContainer();
cookieJar.Add(cookie);
HttpClientHandler handler = new HttpClientHandler
{
CookieContainer = cookieJar,
AllowAutoRedirect = false
};
HttpClient client = new HttpClient(handler);
client.DefaultRequestHeaders.Referrer = referer;
client.DefaultRequestHeaders.Host = uri.Host;
HttpResponseMessage response = client.GetAsync(uri).Result;
HttpResponseHeaders headers = response.Headers;
// Figure out why the returned redirection URL is empty.
Uri redirection = headers.Location;
// It should be containing the following value.
redirection = new Uri(
"https://idpproxy.pensionsmyndigheten.se/idp/Authn/SamlSP?conversation=e1s1");
return redirection;
}
}