// // TODO ITEMS: // * Need to change this to use HttpWebRequest, since I need to erad // the result back and create a tweet out of it, and insert in DB. // // * Report error to the user? Perhaps have a priority flag // (posts show dialog, btu starring does not? // // Runs on a thread from the threadpool. void PostTask(QueuedTask2 [] tasks) { var client = GetClient(); try { Util.PushNetworkActive(); foreach (var task in tasks) { Uri taskUri = new Uri(task.Url); client.Headers [HttpRequestHeader.Authorization] = OAuthAuthorizer.AuthorizeRequest(OAuthConfig, OAuthToken, OAuthTokenSecret, "POST", taskUri, task.PostData); try { var res = client.UploadData(taskUri, task.Verb, Encoding.UTF8.GetBytes(task.PostData)); //var r = new StreamReader (new MemoryStream (res)); //Console.WriteLine (r.ReadToEnd ()); } catch (Exception e) { // Can happen if we had already favorited this status Console.WriteLine("Can happen if we already favorited: {0}", e); } lock (Database.Main) Database.Main.Execute("DELETE FROM QueuedTask2 WHERE TaskId = ?", task.TaskId); } } catch (Exception e) { Console.WriteLine(e); } finally { Util.PopNetworkActive(); } }
void Launch(string url, bool callbackOnMainThread, Action <Stream> callback) { Util.PushNetworkActive(); Uri uri = new Uri(url); // Wake up 3G if it has been more than 3 minutes lock (minuteLock){ var nowTicks = DateTime.UtcNow.Ticks; if (nowTicks - lastLaunchTick > TimeSpan.TicksPerMinute * 3) { MonoTouch.ObjCRuntime.Runtime.StartWWAN(uri); } lastLaunchTick = nowTicks; } var request = (HttpWebRequest)WebRequest.Create(uri); request.AutomaticDecompression = DecompressionMethods.GZip; request.Headers [HttpRequestHeader.Authorization] = OAuthAuthorizer.AuthorizeRequest(OAuthConfig, OAuthToken, OAuthTokenSecret, "GET", uri, null); request.BeginGetResponse(ar => { try { lock (queue) pending--; Util.PopNetworkActive(); Stream stream = null; try { var response = (HttpWebResponse)request.EndGetResponse(ar); stream = response.GetResponseStream(); // Since the stream will deliver in chunks, make a copy before passing to the main UI if (callbackOnMainThread) { var ms = new MemoryStream(); CopyStream(stream, ms); ms.Position = 0; stream.Close(); stream = ms; } } catch (WebException we) { var response = we.Response as HttpWebResponse; if (response != null) { switch (response.StatusCode) { case HttpStatusCode.Unauthorized: // This is the case of sharing two keys break; } stream = null; } Console.WriteLine(we); } catch (Exception e) { Console.WriteLine(e); stream = null; } if (callbackOnMainThread) { invoker.BeginInvokeOnMainThread(delegate { InvokeCallback(callback, stream); }); } else { InvokeCallback(callback, stream); } } catch (Exception e) { Console.WriteLine(e); } lock (queue){ if (queue.Count > 0) { var nextRequest = queue.Dequeue(); Launch(nextRequest.Url, nextRequest.CallbackOnMainThread, nextRequest.Callback); } } }, null); }