我正在尝试将来自特定端口的所有请求(所有请求都来自 Web 浏览器)重定向到请求的主机名作为代码
1 - 从请求中读取所有接收数据
while (recvRequest)
{
this.clientSocket.Receive(requestBuffer);
string fromByte = ASCIIEncoding.ASCII.GetString(requestBuffer);
requestPayload += fromByte;
if (requestPayload.EndsWith(EOL + EOL))
{
recvRequest = false;
}
}
2-解析查询(获取主机和端口以及请求类型)
HeaderFields = ParseQuery(requestPayload);
if (HeaderFields == null || !HeaderFields.ContainsKey("Host"))
{
return;
}
int Port;
string Host;
int Ret;
3 - 如果类型等于 CONNECT
if (HttpRequestType.ToUpper().Equals("CONNECT"))
{ //HTTPS
Ret = RequestedPath.IndexOf(":");
if (Ret >= 0)
{
Host = RequestedPath.Substring(0, Ret);
if (RequestedPath.Length > Ret + 1)
Port = int.Parse(RequestedPath.Substring(Ret + 1));
else
Port = 443;
}
else
{
Host = RequestedPath;
Port = 443;
}
}
4 - 如果输入等于 post 或 get
else
{ //Normal HTTP
Ret = ((string)HeaderFields["Host"]).IndexOf(":");
if (Ret > 0)
{
Host = ((string)HeaderFields["Host"]).Substring(0, Ret);
Port = int.Parse(((string)HeaderFields["Host"]).Substring(Ret + 1));
}
else
{
Host = (string)HeaderFields["Host"];
Port = 80;
}
if (HttpRequestType.ToUpper().Equals("POST"))
{
int index = requestPayload.IndexOf("\r\n\r\n");
m_HttpPost = requestPayload.Substring(index + 4);
}
}
requestPayload = "";
foreach (string line in requestLines)
{
requestPayload += line;
requestPayload += EOL;
}
5 - 连接到主机
Socket destServerSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
destServerSocket.Connect(Host, Port);
//State 2: Sending New Request Information to Destination Server and Relay Response to Client
destServerSocket.Send(ASCIIEncoding.ASCII.GetBytes(requestPayload));
//Console.WriteLine("Begin Receiving Response...");
6 - 向客户端发送接收响应
而(destServerSocket.Receive(responseBuffer)!= 0){
Console.Write(ASCIIEncoding.ASCII.GetString(responseBuffer));
this.clientSocket.Send(responseBuffer);
}
destServerSocket.Disconnect(false);
destServerSocket.Dispose();
this.clientSocket.Disconnect(false);
this.clientSocket.Dispose();
但是当我打开电子邮件时无法正常工作