I am looking for a way to work with an API that requires a login and then redirects to a different URL.
The thing is, so far I have only come up with a way to make 2 Http requests for every action I want to do: first, get a cookie with AllowRedirect = false, and then get the actual URI and make a second request with a cookie
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(sUrl); request.AllowAutoRedirect = false; HttpWebResponse response = (HttpWebResponse)request.GetResponse(); string redirectedUrl = response.Headers["Location"]; if (!String.IsNullOrEmpty(redirectedUrl)) { redirectedUrl = "http://www.ApiUrlComesHere.com/" + redirectedUrl; HttpWebRequest authenticatedRequest = (HttpWebRequest)WebRequest.Create(redirectedUrl); authenticatedRequest.Headers["Cookie"] = response.Headers["Set-Cookie"]; response = (HttpWebResponse)request.GetResponse(); }
Seems terribly ineffective. Is there another way?
Thanks!
source share