【问题标题】:Using public proxy server in HTTP client在 HTTP 客户端中使用公共代理服务器
【发布时间】:2012-12-19 17:56:01
【问题描述】:

我正在尝试使用公共代理服务器 (http://www.unblockwebnow.info/) 将 HTTP 请求发送到目标站点,例如 http://stackoverflow.com :)

我的 HTTP 客户端具有以下架构:

string url = "http://stackoverflow.com";
HttpWebRequest HttpWRequest = (HttpWebRequest)WebRequest.Create(url);
HttpWRequest.Method = "GET";

WebProxy myProxy = new WebProxy();
myProxy.Address = new Uri("http://www.unblockwebnow.info/");
HttpWRequest.Proxy = myProxy;

HttpWebResponse HttpWResponse = (HttpWebResponse)HttpWRequest.GetResponse();
StreamReader sr = new StreamReader(HttpWResponse.GetResponseStream(), encoding);
var rawHTML = sr.ReadToEnd();
sr.Close();

执行 rawHTML 的代码后,我得到 "pageok -managed by puppet - hostingcms02 pageok"

如果我注释掉 HttpWRequest.Proxy = myProxy; 行,我会得到网站内容。

【问题讨论】:

  • 代理地址各种乱七八糟。看起来像一个垃圾网站。

标签: c# http proxy


【解决方案1】:

这似乎有效,但不适用于您的代理(不知道 unblockwebnow.info 的端口号)。 在 URI 中的 ":" 后添加端口号

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            string url = "http://stackoverflow.com";
            HttpWebRequest HttpWRequest = (HttpWebRequest)WebRequest.Create(url);
            HttpWRequest.Method = "GET";

            WebProxy myProxy = new WebProxy();

            //United States proxy, from http://www.hidemyass.com/proxy-list/
            myProxy.Address = new Uri("http://72.64.146.136:8080");
            HttpWRequest.Proxy = myProxy;

            HttpWebResponse HttpWResponse = (HttpWebResponse)HttpWRequest.GetResponse();
            StreamReader sr = new StreamReader(HttpWResponse.GetResponseStream(), true);
            var rawHTML = sr.ReadToEnd();
            sr.Close();

            Console.Out.WriteLine(rawHTML);
            Console.ReadKey();
        }
    }
}

【讨论】:

  • 如果它不适用于他的代理,那么这不能回答他的问题。
猜你喜欢
  • 2018-04-01
  • 1970-01-01
  • 2016-12-23
  • 1970-01-01
  • 1970-01-01
  • 2016-03-10
  • 1970-01-01
  • 1970-01-01
  • 2017-05-14
相关资源
最近更新 更多