Skip to content

Instantly share code, notes, and snippets.

@sandrinodimattia
Created July 23, 2012 06:17
Show Gist options
  • Save sandrinodimattia/3162226 to your computer and use it in GitHub Desktop.
Save sandrinodimattia/3162226 to your computer and use it in GitHub Desktop.
GoogleSession
public class GoogleSession : IDisposable
{
/// <summary>
/// Auth token.
/// </summary>
private string auth;
/// <summary>
/// Initialize request.
/// </summary>
/// <param name="auth"></param>
public GoogleSession(string auth)
{
this.auth = auth;
}
/// <summary>
/// Create a google request and get the response.
/// </summary>
/// <param name="url"></param>
/// <param name="parameters"></param>
/// <returns></returns>
public WebResponse GetResponse(string url, params GoogleParameter[] parameters)
{
// Format the parameters.
string formattedParameters = string.Empty;
foreach (var par in parameters)
formattedParameters += string.Format("{0}={1}&", par.Name, par.Value);
formattedParameters = formattedParameters.TrimEnd('&');
// Create a request with or without parameters.
HttpWebRequest request = null;
if (formattedParameters.Length > 0)
request = (HttpWebRequest)WebRequest.Create(string.Format("{0}?{1}",
url, formattedParameters));
else
request = (HttpWebRequest)WebRequest.Create(url);
// Add the authentication header.
request.Headers.Add("Authorization", "GoogleLogin auth=" + auth);
// Get the response, validate and return.
HttpWebResponse response = request.GetResponse() as HttpWebResponse;
if (response == null)
throw new GoogleResponseNullException();
else if (response.StatusCode != HttpStatusCode.OK)
throw new GoogleResponseException(response.StatusCode,
response.StatusDescription);
return response;
}
/// <summary>
/// Create a google request and get the response stream.
/// </summary>
/// <param name="url"></param>
/// <param name="parameters"></param>
/// <returns></returns>
public Stream GetResponseStream(string url, params GoogleParameter[] parameters)
{
return GetResponse(url, parameters).GetResponseStream();
}
/// <summary>
/// Create a google request and get the page source.
/// </summary>
/// <param name="url"></param>
/// <param name="parameters"></param>
/// <returns></returns>
public string GetSource(string url, params GoogleParameter[] parameters)
{
using (StreamReader reader = new StreamReader(
GetResponseStream(url, parameters)))
{
return reader.ReadToEnd();
}
}
/// <summary>
/// Create a google request and get the feed.
/// </summary>
/// <param name="url"></param>
/// <param name="parameters"></param>
/// <returns></returns>
public SyndicationFeed GetFeed(string url, params GoogleParameter[] parameters)
{
// Load the stream into the reader.
using (StreamReader reader = new StreamReader(
GetResponseStream(url, parameters)))
{
// Create an xml reader out of the stream reader.
using (XmlReader xmlReader = XmlReader.Create(reader,
new XmlReaderSettings()))
{
// Get a syndication feed out of the xml.
return SyndicationFeed.Load(xmlReader);
}
}
}
/// <summary>
/// Clean up the authentication token.
/// </summary>
public void Dispose()
{
auth = null;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment