6

プロキシの設定にかなりの時間を費やしました。現在、proxybonanzaというサービスを利用しています。彼らは私がウェブページをフェッチするために使用するプロキシを私に提供します。

HTMLAGILITYPACKを使用しています

これで、プロキシなしでコードを実行した場合、ローカルまたはWebホストサーバーにアップロードしたときに問題は発生しません。

プロキシを使用することにした場合、少し時間がかかりますが、それでもローカルで機能します。

 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には、これに関連する2つのエントリがあります

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>

誰かが私がこれらの設定を1000回のようにオンとオフにしている問題を見つけることができますか?

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

カール

4

2 に答える 2

2

他のスクリプト/アプリがサーバーから悪意のある攻撃を実行できるようになるため、WebホストがセキュリティのためにASP.NETアプリケーションからの発信接続を無効にしているようです。

アカウントの接続のブロックを解除するように依頼する必要がありますが、「いいえ」と言っても驚かないでください。

于 2012-10-09T00:03:56.727 に答える
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アドレスへのアクセスは許可されていません。

于 2012-10-08T23:41:37.717 に答える