2012-07-02 3 views
1
Imports System.Net 
Imports System.IO 

Public Class Form1 
    Public Function GetHTML(ByVal url As Uri) As String 
     Dim HTML As String 

     Dim Request As HttpWebRequest 
     Dim Response As HttpWebResponse 
     Dim Reader As StreamReader 

     Try 
      Request = HttpWebRequest.Create(url) 
      Response = Request.GetResponse 
      Reader = New StreamReader(Response.GetResponseStream()) 

      HTML = Reader.ReadToEnd 
     Catch ex As Exception 
      HTML = Nothing 
     End Try 

     Return HTML 
    End Function 

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click 
     Dim url As Uri = New Uri(TextBox1.Text) 

     TextBox2.Text = GetHTML(url) 
    End Sub 
End Class 

위의 코드는 웹 페이지에서 html을 가져 오는 코드입니다. 나는 이런 식으로 뭔가가 http://www.google.com.sg/url?sa=t&rct=j&q=vb.net%20convert%20string%20to%20uri&source=web&cd=1&ved=0CFcQFjAA&url=http%3A%2F%2Fwww.vbforums.com%2Fshowthread.php%3Fp%3D3434187&ei=R0fxT872Cs2HrAesq4m-DQ&usg=AFQjCNGGedjegaM8osT689qWhbqpf6NI7Qvb.net httpwebrequest Google 링크로 HTML 얻기

이 나에게

<script>window.googleJavaScriptRedirect=1</script> 
    <script> 
    var f={}; 
    f.navigateTo=function(b,a,g){ 
     if(b!=a&&b.google) 
     { 
     if(b.google.r) 
     { 
      b.google.r=0; 
      b.location.href=g; 
      a.location.replace("about:blank"); 
     } 
     } 
     else 
     { 
     a.location.replace(g); 
     } 
    }; 

    f.navigateTo(window.parent,window,"http://www.vbforums.com/showthread.php?p\x3d3434187"); 

    </script> 
    <noscript> 
    <META http-equiv="refresh" content="0;URL='http://www.vbforums.com/showthread.php?p=3434187'"> 
    </noscript> 

하지 http://www.vbforums.com/showthread.php?p=3434187

의 HTML을 제공 입력하면 문제가 발생하여 어떻게 내 코드가 리디렉션을하고 HTML을 얻을 수 있습니까?

답변

1

메타 태그에서 URL을 긁어서 새 요청을하십시오. 스크래핑을 위해 HtmlAgilityPack을 권장합니다. http://html-agility-pack.net/에서 다운로드하거나 NuGet을 사용하여 설치할 수 있습니다.

관련 문제