Click here to Skip to main content
15,892,199 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am trying to make a web proxy. Here is what I have so far:
IPHostEntry IPHost = Dns.GetHostEntry(sURL);
Console.WriteLine("Resolved:{0}", 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 OK");

NetworkStream ns = new NetworkStream(IPsocket);
StreamWriter sw = new StreamWriter(ns);
StreamReader sr = new StreamReader(ns);


for (int i = 0; i < lista.Count; i++)
{
    sw.WriteLine(lista[i]);
    Console.WriteLine(lista[i]);
}
sw.Flush();
string response = sr.ReadToEnd();


And how I read the request:
C#
StreamReader sr = new StreamReader(s);
string plusz = "";
plusz = sr.ReadLine();
while (plusz != "")
{
    lista.Add(plusz);
        plusz = sr.ReadLine();
}
return lista;


The request looks like this:
VB
GET http://google.com/ HTTP/1.1
Host: google.com
Proxy-Connection: keep-alive
Cache-Control: max-age=0
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.12 Safari/535.11
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Encoding: gzip,deflate,sdch
Accept-Language: hu-HU,hu;q=0.8,en-US;q=0.6,en;q=0.4
Accept-Charset: ISO-8859-2,utf-8;q=0.7,*;q=0.3
Cookie: rememberme=true; NID=54=l  
(...)
 pY


And as you can see I sent this exactly. The problem is that the program stops at the sr.ReadToEnd() method. It is just waiting for the data to arrive, but nothing happens. If I send a wrong request, then it works, so the browser displays the wrong request page (400).
Posted
Updated 2-Jan-12 10:57am
v2

The problem was with the browser. I tried it with Firefox, and not it works. I don't know why though...
 
Share this answer
 
Pretty sure that ReadToEnd() reads to the end of the Stream, not to the end of the message.

You didn't end the Stream, so there is no end. So it just blocks.
 
Share this answer
 
Comments
velvet7 2-Jan-12 18:52pm    
I don't think so, because I tried the Peek() and Read() methods, but they just block it too. And also, as I said when I send a wrong request it works, so it reads the incoming data and it forwards it to the browser.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900