DownloadAsync an Url with a length of over 2100 chars - c#

I want to call a website, that is generated depending on request and has an Url length of about 2200 chars.
When I call the donwload-Method with that Url using WebClient, the whole app crashes without any error message. Here is a sample of the link: http://tinyurl.com/bpp25za
How is it possible to download the content than?

You could use WebBrowser instead:
WebBrowser browser;
private void button1_Click(object sender, RoutedEventArgs e)
{
string url = #"your_really_long_url_here";
browser = new WebBrowser();
browser.Navigated += new EventHandler<NavigationEventArgs>(browser_Navigated);
browser.Navigate(new Uri(url, UriKind.Absolute));
}
void browser_Navigated(object sender, System.Windows.Navigation.NavigationEventArgs e)
{
string htmlContent = browser.SaveToString();
System.Diagnostics.Debug.WriteLine(htmlContent);
}

Related

c# WebBrowser, how I get page content of result page?

I have a c# application with a WebBrowser component.
I set the documentText properties with a string that contains a form and an autosubmit to another page:
In the application the code is:
private void carica_Click(object sender, EventArgs e)
{
browserRoar.DocumentText = formHTML;
browserRoar.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(ShowDocument);
}
private void ShowDocument(object sender,WebBrowserDocumentCompletedEventArgs e)
{
string newContent = (browserRoar.DocumentText);
}
In the webbrowser i see that the new page is the one with results but when i check newContent i find the starting content. How can i get the new content?
Thanks
Fulvio

DownloadStringAsync return error , download file using url

when i am using a DownloadStringAsync with webclient its give me error how to solved this problem
private void BtnDownload_Click(object sender, RoutedEventArgs e)
{
//string PDFPath = ((((sender as Button).Content) as StackPanel).Children[1] as TextBlock).Text;
string PDFPath = "http://www.ncu.edu.tw/~ncu25352/Uploads/20131231103232738561744.pdf";
pdffile = PDFPath;
WebClient wb = new WebClient();
wb.DownloadStringCompleted += wb_DownloadStringCompleted;
wb.DownloadStringAsync(PDFPath,"pdfnamefile");
}
void wb_DownloadStringCompleted(object sender, System.Net.DownloadStringCompletedEventArgs e)
{
throw new NotImplementedException();
}
First, you should give us the exact error.
This answer will assume you get the NotImplementedException.
You are getting this error because you auto-generated the wb_DownloadStringCompleted handler and didn't removed the throw clause.
Therefore, as soon as the download complete, you are yourself firing a NotImplementedException. Chances are that everything is in fact fine.
Try this code :
void wb_DownloadStringCompleted(object sender, System.Net.DownloadStringCompletedEventArgs e)
{
//What you want to do
}
And add your instructions, of course.

.NET C# - webBrowser.Navigate to next URL doesnt work always

I have a List of URLs in a textfile which i want to visit using the C# webBrowser class and save the content of every website to somewhere. The problem is, that the program doesn't always visit the new URL.
Link 1 and 2 is visited correctly, then the browser window doesn't refresh on link 3. Link 4 works again, while 5, 6 and 7 fails. Link 8 works, 9 to 15 fails. 16 Works and so on...
Here is an example list of URLs:
http://www.example.com/somefile_7.html*SomeOtherText1*SomeAdditionalText1
http://www.example.com/somefile_12.html*SomeOtherText1*SomeAdditionalText2
static int counter_getURL = 0;
private void Form1_Load(object sender, EventArgs e)
{
nextTurn();
}
void startBrowser(string url)
{
webBrowser1.Navigate(new Uri(url), "_self");
webBrowser1.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(get_browser_string);
}
void get_browser_string(object sender, WebBrowserDocumentCompletedEventArgs e)
{
// Display the content of the website in textBox1
textBox1.Text = webBrowser1.Document.Body.InnerText;
MessageBox.Show("Next");
nextTurn();
}
public void nextTurn()
{
startBrowser(getURL());
}
public string getURL()
{
string url = "";
string[] input = System.IO.File.ReadAllLines(#"C:\Users\WORKSTATION01\Desktop\url_list.txt", Encoding.Default);
// Get the URL only
string[] splitted = input[counter_getURL].Split(new char[] { '*' });
url = splitted[0];
counter_getURL++;
return url;
}
DocumentCompleted also fires for FRAMEs inside a webpage. My guess is that some webpages of your URLs have FRAMEs and that interferes with your code.

How can I invoke javascript functions with c# with Gekofx Browser

I am using the Gekofx browser because my html files don't work with the default webbrowser control.
So far I was using ObjectForScripting to call javascript code from my C# project. But I was not able to call anything with the Gekofx browser.
I just want to send some data to my html file and display it with the Gekofx browser. Is it possible at all?
For contemplation here is my code:
GeckoWebBrowser myBrowser;
public Form1()
{
InitializeComponent();
String path = #"C:\tools\xulrunner\xulrunner-sdk\bin";
Console.WriteLine("Path: " + path);
Skybound.Gecko.Xpcom.Initialize(path);
myBrowser = new GeckoWebBrowser();
myBrowser.Parent = this;
myBrowser.Dock = DockStyle.Fill;
}
private void btn_go_Click(object sender, EventArgs e)
{
// like the normal browsers
myBrowser.Navigate(tbx_link.Text);
}
private void btn_test_Click(object sender, EventArgs e)
{
// getting the link to my own html file
String path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Webpage");
path += "\\Webpage.html";myBrowser.Navigate(path);
}
I hope you understand what I mean. Thank you!
You can always invoke javascript like this:
mybrowser.Navigate("javascript:YourJavascriptFunction('yourArgument1', 'youArgument2')");
Building on #jordy's answer above, call:
mybrowser.Navigate("javascript:YourJavascriptFunction('yourArgument1', 'youArgument2')");
prefferably in the Document complete event handler to allow for the page to first load.
void myBrowser_DocumentCompleted(object sender, Gecko.Events.GeckoDocumentCompletedEventArgs e)
{
myBrowser.Navigate("javascript:YourJavascriptFunction('yourArgument1', 'youArgument2')");
}

Browser control NavigateToString display HTML code instead of rendering page

I am developing a browser app using Windows Phone 8 browser control.
The app download an external webpage using WebClient into a string in the background. Then the browser navigate to the content using
webBrowser.NavigateToString(str);
However, instead of rendering the page, the browser shows the HTML code. I thought since no changes were made to the string, NavigateToString should handle it seamlessly. Or perhaps I am missing something.
So how do I display the HTML page instead of its code?
EDIT
Here's some of my code
webClient = new WebClient();
webClient.DownloadStringCompleted += new DownloadStringCompletedEventHandler(webClient_DownloadStringCompleted);
webClient.DownloadStringAsync(new Uri(uri));
private void webClient_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
{
PageString = e.Result;
}
...
webBrowser.NavigateToString(PageString);
This is an issue with Windows Phone 8.
Here you have a workaround.
When you use DownloadStringAsync, it also downloads the DOCTYPE declaration. You can remove this and start your code with the <html> block as NavigateToString doesn't seem to like the <!DOCTYPE HTML> declaration.
webClient = new WebClient();
webClient.DownloadStringCompleted += webClient_DownloadStringCompleted;
webClient.DownloadStringAsync(new Uri(uri));
void webClient_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
{
//remove "<!DOCTYPE HTML>"
PageString = e.Result.Replace("<!DOCTYPE HTML>","").Trim();
}
webBrowser.NavigateToString(PageString);
Documentation for WebBrowser.NavigateToString says:
If the text parameter is not in valid HTML format, it will be displayed as plain text.
Can you check if str is in valid HTML format?
private void webClient_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
{
PageString = e.Result;
webBrowser.NavigateToString(PageString);
}
Another Way:
wb.Navigate("");
do
{
Application.DoEvents();
} while ((wb.ReadyState != WebBrowserReadyState.Complete));
wb.Document.Body.InnerHtml = "Html";

Categories

Resources