在 c# 中请求网页欺骗主机 [英] Request Web Page in c# spoofing the Host

查看:18
本文介绍了在 c# 中请求网页欺骗主机的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要为发送到我们网站的网页创建一个请求,但我还需要能够设置主机标头信息.我已经尝试过使用 HttpWebRequest,但是标头信息是只读的(或者至少它的主机部分是只读的).我需要这样做,因为我们想在用户之前执行页面的初始请求.我们有 10 个负载均衡的 Web 服务器,因此我们需要从每个 Web 服务器请求文件.

I need to create a request for a web page delivered to our web sites, but I need to be able to set the host header information too. I have tried this using HttpWebRequest, but the Header information is read only (Or at least the Host part of it is). I need to do this because we want to perform the initial request for a page before the user can. We have 10 web server which are load balanced, so we need to request the file from each of the web servers.

我尝试了以下方法:

HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://192.168.1.5/filename.htm");
request.Headers.Set("Host", "www.mywebsite.com");
WebResponse response = request.GetResponse();

显然这不起作用,因为我无法更新标题,而且我不知道这是否确实是正确的方法.

Obviously this does not work, as I can't update the header, and I don't know if this is indeed the right way to do it.

推荐答案

我已经设法通过使用套接字找到了一条更漫长的路线.我在 IPEndPoint 的 MSDN 页面中找到了答案:

I have managed to find out a more long winded route by using sockets. I found the answer in the MSDN page for IPEndPoint:

string getString = "GET /path/mypage.htm HTTP/1.1
Host: www.mysite.mobi
Connection: Close

";
Encoding ASCII = Encoding.ASCII;
Byte[] byteGetString = ASCII.GetBytes(getString);
Byte[] receiveByte = new Byte[256];
Socket socket = null;
String strPage = null;
try
{
    IPEndPoint ip = new IPEndPoint(IPAddress.Parse("10.23.1.93"), 80);
    socket = new Socket(ip.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
    socket.Connect(ip);
}
catch (SocketException ex)
{
    Console.WriteLine("Source:" + ex.Source);
    Console.WriteLine("Message:" + ex.Message);
}
socket.Send(byteGetString, byteGetString.Length, 0);
Int32 bytes = socket.Receive(receiveByte, receiveByte.Length, 0);
strPage = strPage + ASCII.GetString(receiveByte, 0, bytes);

while (bytes > 0)
{
    bytes = socket.Receive(receiveByte, receiveByte.Length, 0);
    strPage = strPage + ASCII.GetString(receiveByte, 0, bytes);
}
socket.Close();

这篇关于在 c# 中请求网页欺骗主机的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆