Skip to content

Instantly share code, notes, and snippets.

@stack72
Created August 1, 2011 14:24
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save stack72/1118221 to your computer and use it in GitHub Desktop.
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