Date: Fri, 29 Mar 2024 05:29:32 -0400 (EDT) Message-ID: <2117561259.92.1711704572256@gs-ed-prod-wiki1.earthdata.nasa.gov> Subject: Exported From Confluence MIME-Version: 1.0 Content-Type: multipart/related; boundary="----=_Part_91_965148720.1711704572255" ------=_Part_91_965148720.1711704572255 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Content-Location: file:///C:/exported.html
The following C# code example demonstrates how to configure a co= nnection to download data from an Earthdata Login enabled server. Note that= you will need a secure way to configure the Earthdata Login username and p= assword.
using Syst= em; using System.Net; using System.IO; // Connector class for downloading a resource from an Earthdata Login enabl= ed service // class EarthdataLoginConnector { =20 public static void Main() { string resource =3D "<url of resource>"; string urs =3D "https://urs.earthdata.nasa.gov"; string username =3D "<URS user ID>"; string password =3D "<URS user password>"; =20 try { // Ideally the cookie container will be persisted to/from file =20 CookieContainer myContainer =3D new CookieContainer(); // Create a credential cache for authenticating when redirected= to Earthdata Login CredentialCache cache =3D new CredentialCache(); cache.Add(new Uri(urs), "Basic", new NetworkCredential(username= , password)); // Execute the request =20 HttpWebRequest request =3D (HttpWebRequest) WebRequest.Create(r= esource); request.Method =3D "GET"; request.Credentials =3D cache; request.CookieContainer =3D myContainer; request.PreAuthenticate =3D false; request.AllowAutoRedirect =3D true; HttpWebResponse response =3D (HttpWebResponse)request.GetRespon= se(); // Now access the data =20 long length =3D response.ContentLength; string type =3D response.ContentType; Stream stream =3D response.GetResponseStream(); StreamReader reader =3D new StreamReader(stream); // Process the stream data (e.g. save to file) ... // Tidy up =20 stream.Close(); reader.Close(); } catch(Exception ex) { System.Console.WriteLine("Error: " + ex.Message); } } }