C# Web 代理请求

发布于 2024-12-24 03:19:04 字数 1749 浏览 0 评论 0原文

我正在尝试制作一个网络代理。这是我到目前为止所得到的:

    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();</pre>

以及我如何阅读请求:

StreamReader sr = new StreamReader(s);
string plusz = "";
plusz = sr.ReadLine();
while (plusz != "")
{
    lista.Add(plusz);
        plusz = sr.ReadLine();
}
return lista;

请求看起来像这样:

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

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();</pre>

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

独孤求败 2024-12-31 03:19:04
namespace ProxyTester
{

class Program
{
    static void Main(string[] args)
    {
        var htmlResponse = new StringBuilder();
        var RequestPage = BuildHttpRequest("https://google.com/");
        GetHttpResponse(RequestPage, htmlResponse);
    }

    public static HttpWebRequest BuildHttpRequest(string url)
    {
        try
        {
            var getPage = (HttpWebRequest)WebRequest.Create(url);
            WebProxy proxyHTTP = new WebProxy("201.38.194.50", 53128);


            getPage.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
            getPage.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2.19) Gecko/20110707 Firefox/3.6.19";
            getPage.ProtocolVersion = HttpVersion.Version11;
            getPage.Method = "GET";
            getPage.Proxy = proxyHTTP;
            getPage.Timeout = 10000;
            getPage.ReadWriteTimeout = 10000;
            return getPage;
        }
        catch (WebException ex)
        {
            Console.WriteLine(ex.ToString());
        }

        return null;
    }

    public static bool GetHttpResponse(HttpWebRequest page,  StringBuilder htmlResponse)
    {
        htmlResponse.Length = 0; 
        try
        {
            Console.WriteLine("A");
           // var pageResponse = page.GetResponse();
            page.Timeout = 10000;
            var pageResponse = (HttpWebResponse)page.GetResponse();
            Console.WriteLine("5 minutes!");
            if (pageResponse.StatusCode == HttpStatusCode.OK)
            {

                var reader = new StreamReader(pageResponse.GetResponseStream());
                htmlResponse.Append(reader.ReadToEnd());
                pageResponse.Close();
                reader.Close();
                return true;
            }
            Console.WriteLine(pageResponse.StatusCode.ToString());
            pageResponse.Close();
            return false;
        }
        catch (WebException ex)
        {
            Console.WriteLine(ex.ToString());
        }

        return false;
    }
}

}

namespace ProxyTester
{

class Program
{
    static void Main(string[] args)
    {
        var htmlResponse = new StringBuilder();
        var RequestPage = BuildHttpRequest("https://google.com/");
        GetHttpResponse(RequestPage, htmlResponse);
    }

    public static HttpWebRequest BuildHttpRequest(string url)
    {
        try
        {
            var getPage = (HttpWebRequest)WebRequest.Create(url);
            WebProxy proxyHTTP = new WebProxy("201.38.194.50", 53128);


            getPage.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
            getPage.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2.19) Gecko/20110707 Firefox/3.6.19";
            getPage.ProtocolVersion = HttpVersion.Version11;
            getPage.Method = "GET";
            getPage.Proxy = proxyHTTP;
            getPage.Timeout = 10000;
            getPage.ReadWriteTimeout = 10000;
            return getPage;
        }
        catch (WebException ex)
        {
            Console.WriteLine(ex.ToString());
        }

        return null;
    }

    public static bool GetHttpResponse(HttpWebRequest page,  StringBuilder htmlResponse)
    {
        htmlResponse.Length = 0; 
        try
        {
            Console.WriteLine("A");
           // var pageResponse = page.GetResponse();
            page.Timeout = 10000;
            var pageResponse = (HttpWebResponse)page.GetResponse();
            Console.WriteLine("5 minutes!");
            if (pageResponse.StatusCode == HttpStatusCode.OK)
            {

                var reader = new StreamReader(pageResponse.GetResponseStream());
                htmlResponse.Append(reader.ReadToEnd());
                pageResponse.Close();
                reader.Close();
                return true;
            }
            Console.WriteLine(pageResponse.StatusCode.ToString());
            pageResponse.Close();
            return false;
        }
        catch (WebException ex)
        {
            Console.WriteLine(ex.ToString());
        }

        return false;
    }
}

}

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文