C# 代理在本地工作,但在上载到webhost时失败

C# 代理在本地工作,但在上载到webhost时失败,c#,asp.net,proxy,html-agility-pack,C#,Asp.net,Proxy,Html Agility Pack,我现在花了很长时间配置我的代理。目前,我使用一种叫做proxybonanza的服务。他们为我提供了一个代理,我用它来获取网页 我正在使用HTMLAGILITYPACK 现在,如果我在没有代理的情况下运行代码,那么在本地或上传到webhost服务器时都不会出现问题 如果我决定使用代理,它需要更长的时间,但它仍然在本地工作 If I publish my solution to, to my webhost I get a SocketException (0x274c) "A connec

我现在花了很长时间配置我的代理。目前,我使用一种叫做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"
我已经调试这个很长时间了

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

httpWebRequest useUnsafeHeaderParsing="true" 
httpRuntime executionTimeout="180"
这帮我解决了几个问题

这是我的C代码

我做错了什么?我已经在appconfig中启用了跟踪程序,但是我的webhost上没有日志

  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>

Carl

首先尝试将页面作为字符串下载,然后将其传递给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);
如果希望对请求进行更多控制,请使用:


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

听起来您的web主机出于安全考虑禁用了来自ASP.NET应用程序的传出连接,因为这将允许其他脚本/应用程序从其服务器执行恶意攻击服务器


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

您是否允许在应用程序配置上执行web请求?或者你忘了允许mimetype?您托管的是哪种服务器?@weggino Im位于共享主机上。我没有VPS,只有一个asp.NET4.0平台上的标准网络主机帐户。这绝对没问题。您可以在web.config中对IIS执行通常执行的所有设置。稍有不同的是,每次保存或发布web.config时,应用程序都会重新启动。好的。但是这对我有什么帮助呢?我仍然可以从我的web主机通过代理运行?我认为问题在于端口号。防火墙正在阻止对该特定端口的请求。
  request.KeepAlive = false;
  System.Net.ServicePointManager.Expect100Continue = false;
// 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);
// 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);