public MainPage()
{
InitializeComponent();
// Sample code to localize the ApplicationBar
//BuildLocalizedApplicationBar();
}
private void b1_Click(object sender, RoutedEventArgs e)
{
string url = trackingtb.Text;
LoadSiteContent("http://www.mywebsite.com");
}
public void LoadSiteContent(string url)
{
//create a new WebClient object
WebClient client = new WebClient();
client.DownloadStringCompleted += new DownloadStringCompletedEventHandler(DownloadStringCallback2);
client.DownloadStringAsync(new Uri(url));
}
private void DownloadStringCallback2(Object sender, DownloadStringCompletedEventArgs e)
{
// If the request was not canceled and did not throw
// an exception, display the resource.
if (!e.Cancelled && e.Error == null)
{
output.Text= (string)e.Result;
//If you get the cross-thread exception then use the following line instead of the above
//Dispatcher.BeginInvoke(new Action (() => textBlock1.Text = (string)e.Result));
}
}
I'm trying to download the html content of a website. For some reason this code is no working. I hope windows phone 7 and windows phone 8 uses the same stuff.
Thanks.
Your code works correctly for me. You are probably gettin an error. Why don't you check if you are receiving any errors?
private void DownloadStringCallback2(Object sender, DownloadStringCompletedEventArgs e)
{
if (e.Error != null)
{
// Check the error here
}
// If the request was not canceled and did not throw
// an exception, display the resource.
else if (!e.Cancelled)
{
output.Text= (string)e.Result;
//If you get the cross-thread exception then use the following line instead of the above
//Dispatcher.BeginInvoke(new Action (() => textBlock1.Text = (string)e.Result));
}
}
Related
I have this C# code that does not do the job. This code should run a code when the download is complete. But it does not work.
Code:
private void DownloadFileCompleted(object sender, AsyncCompletedEventArgs e)
{
ZipFile.ExtractToDirectory("web/data/main/web.zip", "web/");
}
private void flatToggle2_CheckedChanged(object sender)
{
if (flatToggle2.Checked)
{
//Create File
Directory.CreateDirectory("web/data/main/");
timer2.Start();
bunifuProgressBar1.Show();
bunifuCustomLabel1.Show();
//Downloand
using (var client = new WebClient())
{
client.DownloadStringCompleted += new DownloadStringCompletedEventHandler(DownloadFileCompleted);
client.DownloadFile("https://www.apachelounge.com/download/VC15/binaries/httpd-2.4.29-Win64-VC15.zip", "web/data/main/web.zip");
}
}
else
{
}
}
This is the code that does not work
private void DownloadFileCompleted(object sender, AsyncCompletedEventArgs e)
{
ZipFile.ExtractToDirectory("web/data/main/web.zip", "web/");
}
I tried to run this code without the Unzip but it did not work so I need help with this code.
Looking at the documentation, DownloadFile() is synchronous, so there is no callback needed. Try instead just:
using (var client = new WebClient())
{
client.DownloadStringCompleted += new DownloadStringCompletedEventHandler(DownloadFileCompleted);
client.DownloadFile("https://www.apachelounge.com/download/VC15/binaries/httpd-2.4.29-Win64-VC15.zip", "web/data/main/web.zip");
ZipFile.ExtractToDirectory("web/data/main/web.zip", "web/");
}
This should suffice.
I have this code:
private void goButton_Click(object sender, EventArgs e)
{
web.Navigate(loginURL.Text + "/auth/login");
}
I have the browser showing, and it's just not navigating... It doesn't navigate etc.
The URL is valid.
MSDN is your friend. Make sure you have the 'http://' prefix and try using the Navigate(Uri url) overload.
// Navigates to the given URL if it is valid.
private void Navigate(String address)
{
if (String.IsNullOrEmpty(address))
return;
if (address.Equals("about:blank"))
return;
if (!address.StartsWith("http://") && !address.StartsWith("https://"))
{
address = "http://" + address;
}
try
{
webBrowser.Navigate(new Uri(address));
}
catch (System.UriFormatException)
{
return;
}
}
You need to handle DocumentCompleted event of web browser.
Go through following code:
private void goButton_Click(object sender, EventArgs e)
{
WebBrowser wb = new WebBrowser();
wb.AllowNavigation = true;
wb.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(wb_DocumentCompleted);
wb.Navigate(loginURL.Text + "/auth/login");
}
private void wb_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
WebBrowser wb = sender as WebBrowser;
// wb.Document is not null at this point
}
How to program wait Custom MessageBox OK,Cancel,Yes,No responses? i have been using custom messageBox because of my natural language because messagebox has only english ok,yes,no i have found this one: Customizing MessageBox on Windows Phone 7
private MessageBoxService CustomMsgBox= new MessageBoxService();
public async void Handle(RecordRequest message)
{
AutoResetEvent waitHandle = new AutoResetEvent(false);
Dispatcher.BeginInvoke(() =>
{
CustomMsgBox.Closed += CustomMsgBox_Closed;
CustomMsgBox.Closed += (s, e) => waitHandle.Set();
CustomMsgBox.Show(
"Login Please! ",
"Kayıt",
MessageBoxServiceButton.OKCancel);
});
waitHandle.WaitOne();
// Must Wait MessageBox Ok or Cancel like Normal MessageBox!!!
Login.IsOpen= true;
// do something ...
}
void CustomMsgBox_Closed(object sender, EventArgs e)
{
this.CustomMsgBox.Closed -= this.CustomMsgBox_Closed;
string rst = this.CustomMsgBox.Result.ToString();
}
Normally wp8 messagebox is locked while clicking OK-Cancel. But My Costom MessageBox is not waiting. How to lock my CustomMsgBox until clicking ok-Cancel.
SAMPLE USAGE:
private PixelLab.Common.MessageBoxService _service = new PixelLab.Common.MessageBoxService();
private void Button_Click(object sender, RoutedEventArgs e)
{
this._service.Closed += this.MessageBoxService_Closed;
this._service.Show(
"test",
"alo!",
MessageBoxServiceButton.OKCancel);
//if (_service.IsOpen == true)
//{
// MessageBoxResult rslt = _service.Result;
// if (rslt == MessageBoxResult.No)
// {
// MessageBox.Show("işlem iptal edildi");
// }
// else
// {
// MessageBox.Show("işlem onaylandı");
// }
//}
}
private void MessageBoxService_Closed(object sender, EventArgs e)
{
this._service.Closed -= this.MessageBoxService_Closed;
string rst = this._service.Result.ToString();
}
Below is my code, and what happens when I run this code, first of all it runs for about an hour freely (eating a lot memory - starts from around 400MB RAM and goes up to 1GB), but after an hour or so VSHOST crashes and Visual Studio doesn't catch any exception..
Any ideas why this is happening?
tnx :)
private void UpdateLastPosted()
{
WebClient wc = new WebClient();
string html = wc.DownloadString("http://blogs.com/lastblogs.aspx");
MatchCollection collection = Regex.Matches(html, #"blogs\.com/blogread\.asp\?blog=(\d+)");
foreach (Match pend in collection)
{
pending.Enqueue(pend.Groups[1].Value);
}
}
private void browser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
if (((WebBrowser)sender).ReadyState == WebBrowserReadyState.Complete)
{
if (pending.Count > 0)
{
((WebBrowser)sender).Stop();
NavigateTo("http://blogs.com/blogread.asp?blog=" + pending.Dequeue());
}
else
{
UpdateLastPosted();
UpdateMostActive();
if (pending.Count > 0)
{
((WebBrowser)sender).Stop();
NavigateTo("http://blogs.com/blogread.asp?blog=" + pending.Dequeue());
}
}
}
}
public void NavigateTo(string url)
{
browser.Navigate(url);
}
For every element in the html page the method browser_DocumentCompleted is called. Try specifiying the exact or similar url :
private void browser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
if(e.Url.OriginalString.ToLower() == "http://www.myisite.com/contact.aspx")
{
......
The problem is: When I remove the first message box line, my program doesn't run and throws "Exception has been thrown by the target of an invocation" on the if statement line. However, when I leave the messagebox there, it runs fine. Can someone explain to me why this is happening and what I can do to fix it? I'm fairly new to WPF by the way, any help would be appreciated.
public BrowserMode() {
InitializeComponent();
MessageBox.Show("Entering Browser Mode");
if (webBrowser1.Source.Scheme == "http")
{
//cancel navigation
//this.NavigationService.Navigating += new NavigatingCancelEventHandler(Cancel_Navigation);
qd = new QuestionData();
// code where stuff happens
var url = webBrowser1.Source;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
// from h.RequestUri = "webcam://submit?question_id=45"
var parseUrl = request.RequestUri; //the uri that responded to the request.
MessageBox.Show("The requested URI is: " + parseUrl);
This sort of work is not suited for a constructor and should be moved out until after the WebBrowser is fully loaded. You have two options:
Hook Control.Loaded and perform this behavior there.
public BrowserMode()
{
InitializeComponent();
this.Loaded += BroswerMode_Loaded;
}
void BrowserMode_Loaded(object sender, EventArgs e)
{
if (webBrowser1.Source != null
&& webBrowser1.Source.Scheme == "http")
{
qd = new QuestionData();
// ...
}
}
Hook WebBrowser.Navigating and perform this behavior there.
public BrowserMode()
{
InitializeComponent();
this.webBrowser1.Navigating += WebBrowser_Navigating;
}
void WebBrowser_Navigating(object sender, NavigatingCancelEventArgs e)
{
if (e.Uri.Scheme == "http")
{
qd = new QuestionData();
// ...
}
}