DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
View Events Video Library
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Integrating PostgreSQL Databases with ANF: Join this workshop to learn how to create a PostgreSQL server using Instaclustr’s managed service

Mobile Database Essentials: Assess data needs, storage requirements, and more when leveraging databases for cloud and edge applications.

Monitoring and Observability for LLMs: Datadog and Google Cloud discuss how to achieve optimal AI model performance.

Automated Testing: The latest on architecture, TDD, and the benefits of AI and low-code tools.

Related

  • How to Secure Your Website in 10 Easy Steps
  • Apache Ranger and AWS EMR Automated Installation and Integration Series (2): OpenLDAP + EMR-Native Ranger
  • Hardware-Accelerated OpenGL Rendering in a Linux Container
  • SSH Tutorial: Nice and Easy [Video]

Trending

  • Top 8 Conferences Developers Can Still Attend
  • TypeScript: Useful Features
  • Extracting Maximum Value From Logs
  • The API-Centric Revolution: Decoding Data Integration in the Age of Microservices and Cloud Computing

Spoofing the Host Entry in a HTTPWebRequest

Paul Stack user avatar by
Paul Stack
·
May. 06, 12 · Interview
Like (0)
Save
Tweet
Share
8.17K Views

Join the DZone community and get the full member experience.

Join For Free

I had this idea to create a dashboard that would ping different version of a site in order to get the build number. The details are as follows:

www.mysite.com

runs on a local site at 192.x.x.x, a QA environment at 10.x.x.x, a staging environment at 66.x.x.x, and a live environment at 88.x.x.x (these IPs are made up). In order to hit each site, I would usually manually change the host file and then make the web request. This was not a great solution as I hate dealing with host file entries. On experimenting with HTTPWebRequest, I noticed that you can use reflection in order to change the Headers of the Request. The normal code to create a HTTPWebRequest would look as follows:

try
{
    var request = (HttpWebRequest)HttpWebRequest.Create(new Uri("string url"));

    var webresponse = (HttpWebResponse)request.GetResponse();
    var streamReader = new StreamReader(webresponse.GetResponseStream());

    string response = streamReader.ReadToEnd();
}

I was able to spoof the host by changing the host using reflection:

try
{
var request = (HttpWebRequest)HttpWebRequest.Create(new Uri("string IP address"));
request.Headers.GetType().InvokeMember("ChangeInternal",
BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.InvokeMethod, null,
request.Headers, new object[] { "Host", "Fake host entry" });

var webresponse = (HttpWebResponse)request.GetResponse();

var streamReader = new StreamReader(webresponse.GetResponseStream());

string response = streamReader.ReadToEnd();
}

This allowed me to make multiple requests to different versions of the same site. Using a simple collection of my site objects to build a dashboard that had the following outline:

  Local QA Stage Live
Build Number web_12_4_qa web_12_4_qa web_12_5_stg web_12_4_pro

 

Host (Unix)

Opinions expressed by DZone contributors are their own.

Related

  • How to Secure Your Website in 10 Easy Steps
  • Apache Ranger and AWS EMR Automated Installation and Integration Series (2): OpenLDAP + EMR-Native Ranger
  • Hardware-Accelerated OpenGL Rendering in a Linux Container
  • SSH Tutorial: Nice and Easy [Video]

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends: