C#Web代理请求问题 [英] C# Web Proxy request problem

查看:103
本文介绍了C#Web代理请求问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试制作一个Web代理.这是我到目前为止的内容:

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:

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:

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



如您所见,我发送的正是此邮件.问题在于程序在sr.ReadToEnd()方法处停止.它只是在等待数据到达,但是什么也没有发生.如果我发送了错误的请求,那么它将起作用,因此浏览器将显示错误的请求页面(400).



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).

推荐答案

问题出在浏览器上.我在Firefox上尝试过,但无法正常工作.我不知道为什么...
The problem was with the browser. I tried it with Firefox, and not it works. I don''t know why though...


很确定ReadToEnd()读到Stream的末尾,而不是消息的末尾.

您没有结束流,因此没有尽头.所以它只是阻止.
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.


这篇关于C#Web代理请求问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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