Last active
February 1, 2017 12:08
-
-
Save VictorVelarde/67c6b2a020ca17d56c63167cebae46c2 to your computer and use it in GitHub Desktop.
Get protected resource from THREDDS (C# Code)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var url = "http://urlToProtectedResource/xxxxxx_2016102600.nc?service=WMS&version=1.3.0&request=GetCapabilities"; | |
var username = "user"; | |
var password = "password"; | |
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); | |
request.AllowAutoRedirect = false; | |
HttpWebResponse response = (HttpWebResponse)request.GetResponse(); | |
while (response.StatusCode == HttpStatusCode.RedirectKeepVerb) | |
{ | |
var cookie = response.Headers["Set-Cookie"]; | |
var location = response.GetResponseHeader("Location"); // ccmdata... | |
// new request (keeps cookie & auth) | |
HttpWebRequest newRequest = (HttpWebRequest)WebRequest.Create(new Uri(location)); | |
newRequest.AllowAutoRedirect = false; | |
newRequest.Headers.Add("Cookie", cookie); | |
var encoded = System.Convert.ToBase64String(System.Text.Encoding.GetEncoding("ISO-8859-1").GetBytes(username + ":" + password)); | |
newRequest.Headers.Add(HttpRequestHeader.Authorization, "Basic " + encoded); | |
response = (HttpWebResponse)newRequest.GetResponse(); | |
} | |
var stream = response.GetResponseStream(); | |
StreamReader reader = new StreamReader(stream, Encoding.UTF8); | |
string content = reader.ReadToEnd(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment