Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8431269
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T05:48:34+00:00 2026-06-10T05:48:34+00:00

How can I simulate the functions/actions of a proxy server but without calling elements

  • 0

How can I simulate the functions/actions of a proxy server but without calling elements like HttpListener or TcpListener? How can I generate them from within my C# application?

I’ve been able to get as far as getting actual data streamed back to my WebBrowser element in my C# application but upon viewing the results, it gives me errors. The reason being is because I’m viewing the LITERAL string and there are JS/CSS components within the resulting HTML stream that makes references to objects via relative URIs. Obviously, my solution thinks they’re local and, as such, can’t resolve them.

I’m missing proxy-like functions where it should just hand off the stream back to my mock browser and display properly. However, looking at sample proxy server codes built on C#, they’re all built as servers using listeners. I’d like it to be something that I can instantiate locally without the need to create a listening interface.

Now, you may be wondering why I’m trying to do this? Well, there are a couple of reasons:

  • To be able to inject headers ad-hoc so I can test internal web servers
  • To run as a headless (no GUI) component that can take either HTTP or HTTPS streams from other .NET components and inject headers from, yet, other .NET components.
  • Some other back-end stuff that I think might but won’t know until I have this in place.

Here’s what I have so far:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using HtmlAgilityPack;
using System.Net;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {

            WebClient client = new WebClient();
            var baseUrl = new Uri(textBox1.Text);
            client.Headers.Add("Token1", textBox2.Text);
            client.Headers.Add("Token2",textBox3.Text);

            byte[] requestHTML = client.DownloadData(textBox1.Text);
            string sourceHTML = new UTF8Encoding().GetString(requestHTML);

            HtmlAgilityPack.HtmlDocument htmlDoc = new HtmlAgilityPack.HtmlDocument();
            htmlDoc.LoadHtml(sourceHTML);

            //"//*[@background or @lowsrc or @src or @href]"    
            foreach (HtmlNode link in htmlDoc.DocumentNode.SelectNodes("//*[@href]"))
            {
                //Console.Out.WriteLine(link.ToString());
                if (!string.IsNullOrEmpty(link.Attributes["href"].Value))
                {
                    HtmlAttribute att = link.Attributes["href"];
                    Console.WriteLine("Before: " + att.Value);
                    //Console.Out.WriteLine(att.Value.ToString());
                    Console.WriteLine(new Uri(baseUrl, att.Value));
                    link.Attributes["href"].Value = new Uri(baseUrl, att.Value).ToString();
                    Console.WriteLine("After: " + link.Attributes["href"].Value);

                    //att.Value = this.AbsoluteUrlByRelative(att.Value);
                }
            }

            foreach (HtmlNode link2 in htmlDoc.DocumentNode.SelectNodes("//*[@src]"))
            {
                //Console.Out.WriteLine(link.ToString());
                if (!string.IsNullOrEmpty(link2.Attributes["src"].Value))
                {
                    HtmlAttribute att = link2.Attributes["src"];
                    Console.WriteLine("Before: " + att.Value);
                //    //Console.Out.WriteLine(att.Value.ToString());
                    Console.WriteLine(new Uri(baseUrl, att.Value));
                    if (!att.Value.Contains("/WS"))
                    {
                        Console.WriteLine("HIT ME!");
                        var output = "/WS/" + att.Value;
                        link2.Attributes["src"].Value = new Uri(baseUrl, output).ToString();
                        Console.WriteLine("After HIT: " + link2.Attributes["src"].Value);
                    }
                    else
                    {
                        link2.Attributes["src"].Value = new Uri(baseUrl, att.Value).ToString();
                        Console.WriteLine("After: " + link2.Attributes["src"].Value);
                    }


                //    //att.Value = this.AbsoluteUrlByRelative(att.Value);
                }
            }

            Console.WriteLine(htmlDoc.DocumentNode.OuterHtml);
            Console.WriteLine("+========================+");
            webBrowser1.DocumentText = htmlDoc.DocumentNode.OuterHtml;

        }
    }
}

Again, this is just prototyped code so forgive the wacky spacing and commenting. In the end, it will be more formal. Right now, this monkey is killing my back.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T05:48:35+00:00Added an answer on June 10, 2026 at 5:48 am

    How about using something like NMock or similar? It would mean having to introduce interfaces so that the mocks can be injected, but still beats doing it almost any other way, IMHO…

    From the NMock site:

    NMock is a dynamic mock object library for .NET. Mock objects make it
    easier to test single components—often single classes—without relying
    on real implementations of all of the other components. This means we
    can test just one class, rather than a whole tree of objects, and can
    pinpoint bugs much more clearly. Mock objects are often used during
    Test Driven Development.

    You would mock the proxy server more or less like this:

    var mocks = new Mockery();
    var mockProxyServer = mocks.NewMock<IMyProxyServer>();
    

    That’s all you need to do. As you can see, it’s interface-dependent. But usually all that I’ve needed to do is Refactor->Extract Interfaces from the relevant class in VS.

    Setting up the simulation is usually done within the context of the unit test, like:

    public class TransferFundsPresenterTest
    {
        private Mockery mocks;
        private IMyProxyServer mockProxyServer
    
        [SetUp]
        public void SetUp()
        {
            mocks = new Mockery();
            mockProxyServer = mocks.NewMock<IMyProxyServer>();
        }
    
        [Test]
        public void TestProxyFunction()
        {
            Expect.Once.On(mockProxyServer).
                Method("ProxyFunctionA").
                With("1234").   //  <-- simulate the input params here
                Will(Return.Value("Test"));  // <-- simulate the output from server here
        }
    

    This is just a basic example. You can do a lot more, it’s a very flexible library.

    You really should take a look at the NMock site, it’s pretty easy to get fully up to speed with the library.

    http://www.nmock.org/index.html

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In regards to Can a PHP Proxy call Javascript functions like a click I'm
I noticed that i can write functions like the one below to simulate classes.
I would like to build a simulation engine which can simulate a soccer (association
I've searched and discovered that I can simulate a keypress event using jQuery, but
I recently came across Linsched where one can simulate in user-space some of the
Is there a way by which we can simulate thread level constants in C++?
Does anyone know of a class/library/etc. that can simulate 32 bit unsigned integers on
Can I simulate in C#/C++ code Control + Alt + Delete sequence in Vista?
How can I simulate the behavior of a stack or a queue in VHDL?
How can I simulate an incoming call from a private number -- in an

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.