Created
August 1, 2011 14:24
-
-
Save stack72/1118221 to your computer and use it in GitHub Desktop.
This file contains 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
public List<string> GetAllUserEmailAddresses() | |
{ | |
var listEmails = new List<string>(); | |
var users = GetAllUsers(); | |
foreach(var user in users) | |
{ | |
listEmails.Add(GetUserEmailAddress(user.Id); | |
} | |
} | |
private string GetUserEmailAddress(string userId) | |
{ | |
var user = new User(); | |
var userResponse = DoWebRequest(string.Format("http://myserver:8000/app/rest/users/id:{0}", userId)); | |
//serialise the userResponse to a User | |
return user.Email; | |
} | |
private List<User> GetAllUsers() | |
{ | |
var users= new List<User>(); | |
var userResponse = DoWebRequest("http://myserver:8000/app/rest/users"); | |
//serialise the userResponse to List<User> | |
return users; | |
} | |
private string DoWebRequest(string address) | |
{ | |
var request = (HttpWebRequest)WebRequest.Create(address); | |
request.Method = "GET"; | |
request.Credentials = new NetworkCredential("username, "password"); | |
request.PreAuthenticate = true; | |
string output = string.Empty; | |
try | |
{ | |
using (var response = request.GetResponse()) | |
{ | |
using (var stream = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding(1252))) | |
{ | |
output = stream.ReadToEnd(); | |
} | |
} | |
} | |
catch (WebException ex) | |
{ | |
if (ex.Status == WebExceptionStatus.ProtocolError) | |
{ | |
using (var stream = new StreamReader(ex.Response.GetResponseStream())) | |
{ | |
output = stream.ReadToEnd(); | |
} | |
} | |
else if (ex.Status == WebExceptionStatus.Timeout) | |
{ | |
output = "Request timeout is expired."; | |
} | |
} | |
Console.WriteLine(output); | |
return output; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment