• 0

WebRequest and WebResponse has issues


Question

WebRequest and WebResponse has issues

I wrote a C# program that uses WebRequest and WebResponse to perform a simple web crawler. I discovered something about web sites. Web browsers such as IE and FireFox offer the capacity to view the HTML source code. But it seems that html code that is sent to the browser is one thing and what the browser interprets and displays is something else. For example, if you run a google search in IE and run the same google search in FireFox, the content that you can see when you view the source in IE will NOT have the hyperlinks and content from the search results, but you can see the html hyperlinks and content from the search results when you view the source in FireFox. So my question is this. How do you specialise the WebRequest and WebResponse to show the content after it is processed by the browser instead of before?

10 answers to this question

Recommended Posts

  • 0

It's not an 'issue', it's by design. It's not the responsibility of WebRequest/WebResponse to execute client-side scripts, no content loaded by XHR requests will be returned, just the original HTML document. I believe if you want to return the HTML after it's been modified by a client-side script you would need to use a full web browser control rather than a WebRequest.

  • 0

I think I will have to use the WebBrowser class instead.

How do I expose the LoadCompleted method in the WebBrowser class in WPF C#?

I am trying to write a C# program in wpf that retrieves the content of a web page.

The first thing I tried was to try the WebRequest and WebResponse classes. This did not provide the actual displayed content. WebResponse reveils the HTML code that is sent to the browser. But I discovered that, while the page is being loaded by the browser, javascript can change what content is finally displayed in the browser.

So I decided to use the WebBrowser class.

Immediately I found that there are two WebBrowser classes. Thee is the one that is documented for WinForms and there is another that is documented for WPF. I need to understand the one documented for WPF. What I think I neeed to know what to do is to retrieve code after the "LoadCompleted" method is caused. But I do not know how to this and I cannot find any example demonstrating how this is done.

  • 0

In whatever class you're hosting the control in (Page, Window, etc) you need to add a handler. You can either put it in the class's initialization routine as

myBrowser.LoadCompleted += WebBrowser_LoadCompleted

or put it in the XAML in the WebBrowser declaration.

<WebBrowser Name="myBrowser" LoadCompleted="WebBrowser_LoadCompleted"/>

  • 0

I am getting close to solving this and having a working bit of code. As things stand right now, the call back function for LoadCompleted is not called with the code is stepped through.

Why doesn't this call back function get called?

Basically here is the code surrounding the declaratoini callback method:

webbrowser1 = new WebBrowser();
webbrowser1.LoadCompleted +=webbrowser1_LoadCompleted;
webbrowser1.Navigate(new Uri([url="http://www.google.com"]http://www.google.com[/url]"));

Should there be something more or are they in the wrong order?

The method, webbrowser1_LoadCompleted, is never called. I have put breakpoints in the callback method and the running program never reaches this method:

		 void webbrowser1_LoadCompleted(object sender, NavigationEventArgs e)
		{
			.
			.
			.
		}

I must be missing a reference. I do not know what one I am missing. Can you offer a suggestion?

By the way, that block above is placed there by the editor on this forum, it is not how my code actually looks like

  • 0

I tested it with the following:


WebBrowser b = new WebBrowser();
b.Loaded += b_Loaded;
b.Navigated += b_Navigated;
b.LoadCompleted += b_LoadCompleted;
b.Navigate(http://microsoft.com);
[/CODE]

It seems to fire those three events in that order: Loaded, Navigated, then LoadCompleted. LoadCompleted doesn't fire until the entire page's content is completely downloaded.

Yeah, the code tags are dumb. There should be quotes around the Uri string.

  • 0

I tested it with the following:


WebBrowser b = new WebBrowser();
b.Loaded += b_Loaded;
b.Navigated += b_Navigated;
b.LoadCompleted += b_LoadCompleted;
b.Navigate(http://microsoft.com);
[/CODE]

It seems to fire those three events in that order: Loaded, Navigated, then LoadCompleted. LoadCompleted doesn't fire until the entire page's content is completely downloaded.

Yeah, the code tags are dumb. There should be quotes around the Uri string.

I am close. It works but, at the same time it does not completly come through. When I use a google search page as a test. all the methods you mention are called, but the HTMLDocument I extract when these methods are fired contain the HTML from the home page of google.

Also, if I put breakpoints on these methods, at watch the output to see if the WebBroswer class loads the google results page, it does not. The page is blank until the program is idle.

What do you think?

  • 0

If I am not mistaken it is because google uses ajax to load div content to show the results as you type and the load completed just will pull the html document that was originally loaded. If I use ajax requests and they change div content and then go to view source in any browser it will just show my original document.

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Posts

    • Simple answer is yes, you will still get the Windows updates and as long as browser is up to date, you will be good. Only thing secure boot does is protect you against boot level threats and make it harder to install other OS's. I've been looking into this pretty thoroughly lately myself as wifes computer has secure boot disabled plus my other, older computers that run Linux, don't have secure boot enabled. Have seen all kinds of questions about this on the Linux Mint and MX Linux forums. Just don't suddenly enable secure boot now.
    • How many other companies will follow Ford's lead? Or, have they already gotten lazy and become enslaved to AI--and now can't figure out how to get out of that mess.
    • Why would any self-respecting intelligent person follow any recommendation by Donald's GOP administration? With almost two years of fabrications, deceit, and blatantly illegal behavior, why believe them now? They had best be gone after the November 2026 election, so we'll wait and see.
    • AltSendme 0.4.1 by Razvan Serea AltSendme is a minimal, cross-platform application designed for fast, secure, and private peer-to-peer file transfers. It allows users to send files or entire directories directly between devices without relying on cloud servers, accounts, or any personal information. Everything is encrypted end-to-end using modern protocols like QUIC and TLS 1.3, ensuring both strong security and low-latency performance. Transfers are verified with BLAKE3 for data integrity, and interrupted downloads automatically resume, making the experience reliable even on unstable connections. You can transfer anything—images, videos, documents, and more. Integrity checks are performed on both ends, so your files are automatically verified for correctness during both sending and receiving. AltSendme works seamlessly across local networks or long-distance links, capable of saturating multi-gigabit connections for extremely fast delivery. With built-in NAT traversal and encrypted relay fallback, it connects devices almost anywhere. The app integrates with the Sendme CLI and will soon support mobile and web platforms. Fully free and open-source, AltSendme offers a lightweight, privacy-first alternative to traditional cloud-based services, removing size limits, upload costs, and unnecessary data exposure. AltSendme 0.4.1 changelog: Release Highlights Self-hosted relays: Run your own iroh relay so transfers don't rely on public infrastructure. Includes a full deployment template in deploy/relay/ with Docker Compose for a VPS and configuration examples for production use. Fly.io support: One-click deploy template for Fly.io, including a quick-start config (fly.dev.toml) for testing without a custom domain, plus production setup with Let's Encrypt and your own hostname. Relay settings UI: New Settings → Network panel to choose how AltSendme connects: automatic public relays, custom self-hosted URLs (with optional auth token), or disabled. Test connections, verify latency, and see live relay status in the footer. Disable relays: Turn off relay servers entirely when you only need same-network transfers (e.g. LAN). Direct connections only. No relay hop required when devices can reach each other. Android graduates from beta: Android is now part of the regular release cycle alongside desktop. APKs ship with each version (universal, arm64, and armv7). Other improvements Private relay access control via shared auth token Relay fallback notifications when a custom relay is unreachable Broadcast mode toggle in sharing settings Android release build fixes (split-per-ABI APKs, universal APK preservation) UI polish: mobile safe-area insets, dropzone layout, transfer progress animation Bug fixes for minification-related serialization issues and system tray icon loading What's Changed feat(relay): add relay status functionality and settings UI (a120cdf) feat(relay): implement custom relay server configuration and verification (51276c7) feat(relay): add configuration for private relay access and enhance observability features (48fbabf) feat(relay): enhance relay URL validation, display connection status (d4fffa0) feat(relay): add RelayChangeGuard component and enhance relay-related translations (16ba514) feat(broadcast): add toggle setting for broadcast mode in sharing UI (ca6d977) fix(relay): correct QUIC discovery port, pin image, templatize fly.dev (52a2ba5) fix: More broken serialization due to minification (67491a9) fix(android): preserve true universal APK across per-ABI builds (e9f256f) fix(ui): conditional safe-area insets padding on mobile (1182f0e) refactor(transfer): CircularRing component animation fix (944572b) chore(android): drop x86 and x86_64 release APKs, keep universal+arm64+armv7 (34ada0b) Download: AltSendme 0.4.1 | ARM64 | ~9.0 MB (Open Source) Download: AltSendme for MacOS | Android Links: AltSendme Home Page | GitHub | Screenshot Get alerted to all of our Software updates on Twitter at @NeowinSoftware
    • You are mostly right about the ephemeral nature of it. As I mention in the article, if you dont add a second device or take a backup of your account before uninstalling it, then yes you will lose access to your account. That said, in terms of actual user experience when you sync multiple devices your message history carries across and there's also a Saved Messages chat like there is on Telegram to send messages and attachments between your installs. But yh, what you point out are correct and its not trying to emulate Messenger or Telegram.
  • Recent Achievements

    • Week One Done
      flexorcist earned a badge
      Week One Done
    • One Month Later
      Woland13 earned a badge
      One Month Later
    • Week One Done
      Woland13 earned a badge
      Week One Done
    • One Year In
      bernmeister earned a badge
      One Year In
    • Week One Done
      Scoobystu earned a badge
      Week One Done
  • Popular Contributors

    1. 1
      +primortal
      495
    2. 2
      +Edouard
      225
    3. 3
      PsYcHoKiLLa
      149
    4. 4
      Steven P.
      75
    5. 5
      FloatingFatMan
      71
  • Tell a friend

    Love Neowin? Tell a friend!