【问题标题】:Proxy works locally but fails when uploaded to webhost代理在本地工作,但在上传到虚拟主机时失败
【发布时间】:2012-10-03 22:13:19
【问题描述】:

我现在花了很多时间配置我的代理。目前,我使用一项名为 proxybonanza 的服务。他们为我提供了一个代理,我用它来获取网页。

我正在使用 HTMLAGILITYPACK

现在,如果我在没有代理的情况下运行我的代码,则在本地或上传到 webhost 服务器时没有问题。

如果我决定使用代理,它需要更长的时间,但它仍然可以在本地工作。

 If I publish my solution to, to my webhost I get a SocketException (0x274c) 

 "A connection attempt failed because the connected party did not properly respond
 after a period of time, or established connection failed because connected host has
 failed to respond 38.69.197.71:45623"

我已经调试了很长时间了。

我的 app.config 有两个与此相关的条目

httpWebRequest useUnsafeHeaderParsing="true" 
httpRuntime executionTimeout="180"

这帮助我解决了几个问题。

现在这是我的 C# 代码。

 HtmlWeb htmlweb = new HtmlWeb();
 htmlweb.PreRequest = new HtmlAgilityPack.HtmlWeb.PreRequestHandler(OnPreRequest);
 HtmlDocument htmldoc = htmlweb.Load(@"http://www.websitetofetch.com,
                                         "IP", port, "username", "password");

 //This is the preRequest config
 static bool OnPreRequest(HttpWebRequest request)
    {
      request.KeepAlive = false;
        request.Timeout = 100000;
        request.ReadWriteTimeout = 1000000; 
        request.ProtocolVersion = HttpVersion.Version10;
        return true; // ok, go on
    }

我做错了什么?我在 appconfig 中启用了跟踪器,但我的虚拟主机上没有日志...?

  Log stuff from app.config

 <system.diagnostics>
 <sources>
  <source name="System.ServiceModel.MessageLogging" switchValue="Warning, ActivityTracing" >
     <listeners>
        <add name="ServiceModelTraceListener"/>
     </listeners>
  </source>


  <source name="System.ServiceModel" switchValue="Verbose,ActivityTracing">
     <listeners>
        <add name="ServiceModelTraceListener"/>
     </listeners>
     </source>
     <source name="System.Runtime.Serialization" switchValue="Verbose,ActivityTracing">
        <listeners>
           <add name="ServiceModelTraceListener"/>
        </listeners>
     </source>
   </sources>
   <sharedListeners>
   <add initializeData="App_tracelog.svclog"
     type="System.Diagnostics.XmlWriterTraceListener, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
     name="ServiceModelTraceListener" traceOutputOptions="Timestamp"/>
 </sharedListeners>
 </system.diagnostics>

谁能发现我将这些设置打开和关闭一千次的问题..

  request.KeepAlive = false;
  System.Net.ServicePointManager.Expect100Continue = false;

卡尔

【问题讨论】:

  • 您是否在您的应用配置中允许了网络请求?或者,也许你忘了允许 mimetype?你托管什么样的服务器?
  • @wegginho 我在共享主机上。我没有 VPS,只有一个标准的 asp.net 4.0 平台的虚拟主机帐户。
  • 没关系。您可以在 web.config 中执行通常对 IIS 执行的所有设置。细微的差别在于,每次您保存或发布 web.config 时,应用程序都会重新启动。
  • 好的..但这对我有什么帮助?我仍然可以从我的虚拟主机通过代理运行吗?
  • 我认为问题出在端口号上。防火墙正在阻止对该特定端口的请求。

标签: c# asp.net proxy html-agility-pack


【解决方案1】:

尝试先将页面下载为字符串,然后将其传递给 HtmlAgilityPack。这将使您能够将下载过程中发生的错误与 html 解析过程中发生的错误隔离开来。如果您对 proxybonanza 有疑问(请参阅帖子末尾),您将能够将该问题与 HtmlAgilityPack 配置问题隔离开来。

使用 WebClient 下载页面:

// Download page
System.Net.WebClient client = new System.Net.WebClient();
client.Proxy = new System.Net.WebProxy("{proxy address and port}");
string html = client.DownloadString("http://example.com");

// Process result
HtmlAgilityPack.HtmlDocument htmlDoc = new HtmlAgilityPack.HtmlDocument();
htmlDoc.LoadHtml(html);

如果您想对请求进行更多控制,请使用System.Net.HttpWebRequest

// Create request
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://example.com/");

// Apply settings (including proxy)
request.Proxy = new WebProxy("{proxy address and port}");
request.KeepAlive = false;
request.Timeout = 100000;
request.ReadWriteTimeout = 1000000;
request.ProtocolVersion = HttpVersion.Version10;

// Get response
try
{
    HttpWebResponse response = (HttpWebResponse)request.GetResponse();
    Stream stream = response.GetResponseStream();
    StreamReader reader = new StreamReader(stream);
    string html = reader.ReadToEnd();
}
catch (WebException)
{
    // Handle web exceptions
}
catch (Exception)
{
    // Handle other exceptions
}

// Process result
HtmlAgilityPack.HtmlDocument htmlDoc = new HtmlAgilityPack.HtmlDocument();
htmlDoc.LoadHtml(html);

此外,请确保您的代理提供商 (proxybonanza) 允许从您的生产环境访问您的代理。大多数提供商将对代理的访问限制为某些 IP 地址。他们可能允许访问您在本地运行的网络的外部 IP,但不允许访问您的生产环境的外部 IP 地址。

【讨论】:

    【解决方案2】:

    听起来您的网络主机已禁用来自 ASP.NET 应用程序的传出连接以确保安全,因为这将允许其他脚本/应用程序从其服务器执行恶意攻击。

    您必须要求他们取消阻止您帐户上的连接,但如果他们拒绝,请不要感到惊讶。

    【讨论】:

      猜你喜欢
      • 2012-09-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多