Skip to content

Instantly share code, notes, and snippets.

@tig
Created December 3, 2012 08:14
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tig/4193590 to your computer and use it in GitHub Desktop.
Save tig/4193590 to your computer and use it in GitHub Desktop.
wp8 app crash
// App exits on call to client.UploadStringAsync
// The ClientUploadStringCompleted handler is never called.
public event EventHandler<LoginCompletedEventArgs> LoginCompleted;
public void OnLoginCompleted(LoginCompletedEventArgs e)
{
var handler = LoginCompleted;
if (handler != null) handler(this, e);
}
// GeoVision lamely authenticates with username/pw in the clear in the POST body
// At least we can use SSL.
// At https://host:port/phoneinfo is an HTML doc that lists the cameras, which is
public void Login(String host, int port, bool ssl, String userName, String password,
EventHandler<LoginCompletedEventArgs> loginCompletedEventHandler) {
LoginCompleted += loginCompletedEventHandler;
// stash away the base URL
_url = String.Format("{0}://{1}:{2}", ssl ? "https" : "http", host, port);
var formData = String.Format("id={0}&pwd={1}&ImageType=1", userName, password);
var client = new WebClient();
client.UploadStringCompleted += ClientUploadStringCompleted;
client.UploadStringAsync(new Uri(_url + "/phoneinfo"), formData);
}
private void ClientUploadStringCompleted(object sender, UploadStringCompletedEventArgs e) {
ObservableCollection<GeoVisionCamera> cameras = null;
Exception ex = null;
if (e.Error != null) {
ex = e.Error;
}
else {
var htmlDoc = new HtmlDocument();
htmlDoc.LoadHtml(e.Result);
cameras = new ObservableCollection<GeoVisionCamera>();
var nodes = htmlDoc.DocumentNode.SelectNodes("//a");
if (nodes == null) {
ex = new Exception("Error parsing html");
var h2 = htmlDoc.DocumentNode.SelectSingleNode("//h2");
if (h2 != null)
ex = new Exception(h2.InnerText.Trim());
}
else {
// get all but last (it's not a camera)
for (int i = 0; i < nodes.Count - 1; i++) {
var name = nodes[i].InnerText.Trim();
var url = nodes[i].GetAttributeValue("href", "");
var jpgUrl = String.Format("{0}/{1}/cam{2}.jpg", _url,
url.Substring(url.IndexOf("IDKey=", StringComparison.Ordinal) + 6, 36),
i);
cameras.Add(new GeoVisionCamera(name, i, jpgUrl));
}
}
}
var args = new LoginCompletedEventArgs(ex, cameras);
OnLoginCompleted(args);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment