怎样编写HTTP代理软件

zhuzhi 2002-12-19 10:39:57
怎样编写HTTP代理软件
...全文
108 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
ArLi2003 2003-04-21
  • 打赏
  • 举报
回复
建议用 Encoding.Default
mamu 2003-04-20
  • 打赏
  • 举报
回复
mark
ltyx01 2003-04-20
  • 打赏
  • 举报
回复
这个怎么不支持中文啊,怎么改才支持中文呢
timmy3310 2003-04-20
  • 打赏
  • 举报
回复
如果要支持中文,你把他代码里面所有的ASCII换成:System.Text.Encoding.GetEncoding("gb2312")返回的对象
yarshray 2002-12-19
  • 打赏
  • 举报
回复
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.IO;
using System.Threading;


namespace WebProxy2
{

class WebProxy2
{
Socket clientSocket;
Byte[] read = new byte[1024];
Byte [] Buffer = null;
Encoding ASCII = Encoding.ASCII;
const string HTTP_VERSION = "HTTP/1.0";
const string CRLF = "\r\n";
Byte[] RecvBytes = new Byte[4096];

public WebProxy2(Socket socket)
{
this.clientSocket = socket;
}
public void run()
{
String clientmessage = " ", sURL = " ";
int bytes = readmessage(read, ref clientSocket, ref clientmessage);
if(bytes == 0)
{
return;
}
int index1 = clientmessage.IndexOf(' ');
int index2 = clientmessage.IndexOf(' ', index1 + 1);
if((index1 == -1) || (index2 == -1))
{
throw new IOException();
}
Console.WriteLine("Connecting to Site: {0}", clientmessage.Substring(index1 + 1, index2 - index1));
Console.WriteLine("Connection from {0}", clientSocket.RemoteEndPoint);

string part1 = clientmessage.Substring(index1 + 1, index2 - index1);
int index3 = part1.IndexOf('/', index1 + 8);
int index4 = part1.IndexOf(' ', index1 + 8);
int index5 = index4 - index3;
sURL = part1.Substring(index1 + 4, (part1.Length - index5) - 8);

try
{
IPHostEntry IPHost = Dns.Resolve(sURL);
Console.WriteLine("Request resolved: ", IPHost.HostName);
string [] aliases = IPHost.Aliases;
IPAddress[] address = IPHost.AddressList;
Console.WriteLine(address[0]);
IPEndPoint sEndpoint = new IPEndPoint(address[0], 80);
Socket IPsocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream,ProtocolType.Tcp);
IPsocket.Connect(sEndpoint);
if(IPsocket.Connected)
Console.WriteLine("Socket connect OK");
string GET = clientmessage;
Byte[] ByteGet = ASCII.GetBytes(GET);
IPsocket.Send(ByteGet, ByteGet.Length, 0);
Int32 rBytes = IPsocket.Receive(RecvBytes, RecvBytes.Length, 0);
Console.WriteLine("Recieved {0}", + rBytes);
//Buffer = RecvBytes;
String strRetPage = null;
strRetPage = strRetPage + ASCII.GetString(RecvBytes, 0, rBytes);
while (rBytes > 0)
{
rBytes = IPsocket.Receive(RecvBytes, RecvBytes.Length, 0);
strRetPage = strRetPage + ASCII.GetString(RecvBytes, 0, rBytes);
}
IPsocket.Shutdown(SocketShutdown.Both);
IPsocket.Close();
sendmessage(clientSocket, strRetPage);
}
catch (Exception exc2)
{
Console.WriteLine(exc2.ToString());
}
}
private int readmessage(byte [] ByteArray, ref Socket s, ref String clientmessage)
{
int bytes = s.Receive(ByteArray, 1024, 0);
string messagefromclient = Encoding.ASCII.GetString(ByteArray);
clientmessage = (String)messagefromclient;
return bytes;
}

private void sendmessage(Socket s, string message)
{
Buffer = new Byte[message.Length + 1];
int length = ASCII.GetBytes(message, 0, message.Length, Buffer, 0);
s.Send(Buffer, length, 0);
}

static void Main(string[] args)
{
const int port = 8889;
TcpListener tcplistener = new TcpListener(port);
Console.WriteLine("Listening on port {0}", + port);
tcplistener.Start();
while(true)
{
Socket socket = tcplistener.AcceptSocket();
WebProxy2 webproxy = new WebProxy2(socket);
Thread thread = new Thread(new ThreadStart(webproxy.run));
thread.Start();
}
}
}
}

110,590

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧