2012-10-03 3 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" 

관련있는 두 개의 항목이 있습니다.

이제 내 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 구성 문제에서 해당 문제를 격리 할 수 ​​있습니다.

다운로드 페이지 사용하여 웹 클라이언트 : 당신이 요청을 더 제어하려면

// 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 응용 프로그램의 나가는 연결을 비활성화 한 것처럼 들립니다.

계정에서 연결 해제를 요청해야하지만, 아니요라고 대답하면 놀랄 일이 아닙니다.

관련 문제