Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static string GetRealm(string url)
- {
- var request = (HttpWebRequest)WebRequest.Create(url);
- try
- {
- using (request.GetResponse())
- {
- return null;
- }
- }
- catch (WebException e)
- {
- if (e.Response == null) return null;
- var auth = e.Response.Headers[HttpResponseHeader.WwwAuthenticate];
- if (auth == null) return null;
- // Example auth value:
- // Basic realm="Some realm"
- return ...Extract the value of "realm" here (with a regex perhaps)...
- }
- }
- // Create a request to a URL
- WebRequest myReq = WebRequest.Create(url);
- string usernamePassword = "username:password";
- //Use the CredentialCache so we can attach the authentication to the request
- CredentialCache mycache = new CredentialCache();
- mycache.Add(new Uri(url), "Basic", new NetworkCredential("username", "password"));
- myReq.Credentials = mycache;
- myReq.Headers.Add("Authorization", "Basic " + Convert.ToBase64String(new ASCIIEncoding().GetBytes(usernamePassword)));
- //Send and receive the response
- WebResponse wr = myReq.GetResponse();
- Stream receiveStream = wr.GetResponseStream();
- StreamReader reader = new StreamReader(receiveStream, Encoding.UTF8);
- string content = reader.ReadToEnd();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement