代理在本地工作,但在上传到webhost时失败

时间:2012-10-03 22:13:20

标签: 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 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中启用了跟踪器,但是我没有登录我的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>

任何人都可以发现问题我有这样的设置开启和关闭一千次..

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

卡尔

2 个答案:

答案 0 :(得分:2)

首先尝试将该页面作为字符串下载,然后将其传递给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地址。

答案 1 :(得分:2)

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

您必须要求他们取消屏蔽您帐户中的关联,但如果他们拒绝,请不要感到惊讶。

相关问题