/// <summary> /// Submits a new atom entry to a particular Google service. /// </summary> /// <typeparam name="TFeed">Type of feed for the service.</typeparam> /// <typeparam name="TEntry">Type of feed entry for the service.</typeparam> /// <param name="entry">The atom entry object containing the data to submit.</param> /// <param name="applicationName">The name of the application which invokes this method.</param> /// <returns>A new atom entry containing some additional data like id, published date etc.</returns> protected virtual TEntry SubmitNewEntry <TFeed, TEntry>(TEntry entry, string applicationName) where TFeed : GFeed <TEntry>, new() where TEntry : GEntry, new() { if (String.IsNullOrEmpty(GUrl)) { throw new ArgumentNullException(GUrl, "Feed Url is not provided. Please provide Feed url first through GUrl Property."); } var request = WebRequest.Create(GUrl) as HttpWebRequest; if (request != null) { var serializer = new XObjectSerializer <TEntry>(); string xml = serializer.StringSerialize(entry); long length = xml.ToCharArray().LongLength; var gc = new GDataCredentials(GUserName, GPassword) { AccountType = "GOOGLE" }; string header = GAuthManager.RequestClientLoginHeader(gc, "code", applicationName, true, GoogleAuthentication.URI_HANDLER, ProxySettings); if (ProxySettings != null) { request.Proxy = ProxySettings; } request.Method = "POST"; request.Credentials = new NetworkCredential { UserName = GUserName, Password = GPassword }; request.ContentType = "application/atom+xml"; request.ContentLength = length; request.Headers.Add(header); var sw = new StreamWriter(request.GetRequestStream()); sw.Write(xml); sw.Close(); var webData = new GWebAsyncData <TFeed, TEntry> { Request = request, ResetEvent = new ManualResetEvent(false) }; IAsyncResult result = request.BeginGetResponse(AsyncManager <TFeed, TEntry> .AsyncSubmitNewEntry, webData); ((GWebAsyncData <TFeed, TEntry>)result.AsyncState).ResetEvent.WaitOne(); return(((GWebAsyncData <TFeed, TEntry>)result.AsyncState).EntryResult); } throw new OutOfMemoryException("Unable to create new WebRequest"); }
/// <summary> /// Downloads all feed asynchronously from a Google service. /// </summary> /// <param name="result"><see cref="System.IAsyncResult"/> object.</param> internal static void AsyncDownloadAllFeeds(IAsyncResult result) { try { var request = ((GWebAsyncData <TFeed, TEntry>)result.AsyncState).Request as HttpWebRequest; if (request != null) { WebResponse response = request.EndGetResponse(result); Stream stream = response.GetResponseStream(); var serializer = new XObjectSerializer <TFeed>(); // used to build entire input var sb = new StringBuilder(); // used on each read operation var buffer = new byte[8192]; int count; do { // fill the buffer with data count = stream.Read(buffer, 0, buffer.Length); // make sure we read some data if (count != 0) { // translate from bytes to ASCII text string tempString = Encoding.UTF8.GetString(buffer, 0, count); // continue building the string sb.Append(tempString); } } while (count > 0); // any more data to read? Trace.WriteLine(sb.ToString()); ((GWebAsyncData <TFeed, TEntry>)result.AsyncState).FeedResult = serializer.Deserialize(sb.ToString()); stream.Close(); response.Close(); ((GWebAsyncData <TFeed, TEntry>)result.AsyncState).ResetEvent.Set(); } } catch (Exception exception) { ((GWebAsyncData <TFeed, TEntry>)result.AsyncState).ResetEvent.Set(); Console.WriteLine(exception.Message); throw; } }