2012-10-03 5 views
6

私は今プロキシを設定するのに楽しい時間を過ごしました。現時点で私はproxybonanzaというサービスを使用しています。彼らは私にウェブページを取得するために使うプロキシを私に供給します。プロキシはローカルで動作しますが、ウェブホストにアップロードすると失敗します

私はプロキシなしで私のコードを実行する場合、私は今HTMLAGILITYPACK

を使用しているウェブホストサーバにアップロードローカルまたは全く問題はありません。

私がプロキシを使用することに決めた場合、それはやや時間がかかりますが、依然としてローカルで動作します。

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" 

に関連する2つのエントリがあります。

これは私の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; 

カール

+0

あなたのアプリ設定でウェブリクエストを許可しましたか?あるいは、あなたはmimetypeを許可するのを忘れていたでしょうか?どのようなサーバーをホスティングしていますか? – wegginho

+0

@wegginho Imを共有ホストに追加しました。私はVPSをasp.net 4.0プラットフォームの標準的なウェブホスティングアカウントしか持っていません。 – 8bitcat

+0

これは大丈夫です。 web.config内のIISに通常行うすべての設定を行うことができます。わずかな違いは、web.configを保存または公開するたびに、アプリケーションが再起動することです。 – wegginho

答えて

2

はその後にそれを渡して、最初の文字列としてページをダウンロードしてみてくださいHtmlAgilityPack。これにより、ダウンロードプロセス中に発生したエラーを、HTML解析プロセス中に発生したエラーから切り離すことができます。 proxybonanzaに問題がある場合(投稿の終わりを参照)、HtmlAgilityPack設定の問題からその問題を切り分けることができます。 Webクライアントを使用して

ダウンロードページ:

// 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

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

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