Created
December 3, 2012 08:14
-
-
Save tig/4193590 to your computer and use it in GitHub Desktop.
wp8 app crash
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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