In C #, httpwebrequest uses proxy to implement the domain name request of the specified IP Address

Source: Internet
Author: User
Document directory
  • What should we do?
  • The reference code is as follows (the code is from reference 1 ):

I have the following requirement:

A domain name, xxx.com, is actually followed by many IP addresses: for example:

  • 1.2.3.4,
  • 5.6.7.8,
  • 9.10.11.12

These IP addresses all have the same website. During domain name resolution, a random IP address is assigned to you (this is DNS load balancing ).

However, if I want to access a website on a specific IP address, such as a website on 5.6.7.8, but the website is restricted to be accessible only through a domain name, directly change the domain name to an IP Address URL such as http: // 5.6.7.8.

What should we do?

There are two methods:

1. Modify the hosts file and specify xxx.com to be resolved to 5.6.7.8.

2. Use http: // 5.6.7.8:

HOST: xxx.com

 

Because I use C # code to implement this function, I want to solve it in 2nd ways.

 

C # uses the httpwebrequest class to obtain an HTTP request. It has a header attribute and can modify the value in the header. However, according to msdn, the host identity cannot be modified in this way. If you use this method:

Httpwebrequest. headers ["host"] = "xxx.com ";

It throws an exception:

Argumentexception: The 'host' header cannot be modified directly.

Can we still meet the above requirements? The answer is yes, but the method should be changed:

The URL still uses the Domain Name:

Http://xxx.com/

Set the proxy attribute of httpwebrequest to the IP address you want to access, as follows:

Httpwebrequest. Proxy = new WebProxy (IP. tostring ());

 

The reference code is as follows (the code is from reference 1 ):
using System;using System.IO;using System.Net;namespace ConsoleApplication1{    class Program    {        public static void Main(string[] args)        {            HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://www.example.com/Default.aspx");            System.Net.WebProxy proxy = new WebProxy("208.77.186.166", 80);            request.Proxy = proxy;            using (WebResponse response = request.GetResponse())            {                using (TextReader reader = new StreamReader(response.GetResponseStream()))                {                    string line;                    while ((line = reader.ReadLine()) != null)                        Console.WriteLine(line);                }            }        }    }}

 

In this way, the domain name request of the specified IP address is implemented.

Note: Someone has already reported to Microsoft that the Host header cannot be modified. Microsoft has reported that a new host attribute will be added to the next. Net framewok file, so that the Host header can be modified.

Original article:

ByMicrosoftSend the message

The next release of the. NET framework will include a new "host" property. The value of this property will be sent as "host" header in the HTTP request.

References:

  1. Httpwebrequest. headers ["host"] throws an unexpected exception

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.