none
(截取登入網站後的頁面)HttpWebRequest似乎沒法用 RRS feed

  • 問題

  • 不知道有人做過類似的機制嗎?

    有一個網站需要登入,登陸後的網頁才是有用的

    這兩天測過 HttpWebRequest 的方式 可呼叫該網頁

    但是沒法登入咧..

    以前VB有個元件可以模擬IE去開啟網頁,因為該網頁是會紀錄session

    因此要不斷的用該IE去跑第2個頁面,該功能想改由.NET的機制處理不知道

    有否解決方案

    2006年6月15日 上午 08:11

解答

  • 以下為取自 .NET SDK 的範例程式碼(SDK 文件要多看)

    Imports System
    Imports System.Net
    Imports System.Text
    Imports System.IO
    
    
        Public Class Test
    
            ' Specify the URL to receive the request.
            Public Shared Sub Main(ByVal args() As String)
            Dim request As HttpWebRequest = CType(WebRequest.Create(args(0)), HttpWebRequest)
    
    
            ' Set some reasonable limits on resources used by this request
            request.MaximumAutomaticRedirections = 4
            request.MaximumResponseHeadersLength = 4
    
            ' Set credentials to use for this request.
            request.Credentials = CredentialCache.DefaultCredentials
    
            Dim response As HttpWebResponse = CType(request.GetResponse(), HttpWebResponse)
    
            Console.WriteLine("Content length is {0}", response.ContentLength)
            Console.WriteLine("Content type is {0}", response.ContentType)
    
            ' Get the stream associated with the response.
            Dim receiveStream As Stream = response.GetResponseStream()
    
            ' Pipes the stream to a higher level stream reader with the required encoding format. 
            Dim readStream As New StreamReader(receiveStream, Encoding.UTF8)
    
            Console.WriteLine("Response stream received.")
            Console.WriteLine(readStream.ReadToEnd())
            response.Close()
            readStream.Close()
        End Sub 'Main
    End Class 'Test
    '
    'The output from this example will vary depending on the value passed into Main 
    'but will be similar to the following:
    '
    'Content length is 1542
    'Content type is text/html; charset=utf-8
    'Response stream received.
    '<html>
    '...
    '</html>
    '
    '
    
    2006年6月15日 上午 08:22
    版主

所有回覆

  • 以下為取自 .NET SDK 的範例程式碼(SDK 文件要多看)

    Imports System
    Imports System.Net
    Imports System.Text
    Imports System.IO
    
    
        Public Class Test
    
            ' Specify the URL to receive the request.
            Public Shared Sub Main(ByVal args() As String)
            Dim request As HttpWebRequest = CType(WebRequest.Create(args(0)), HttpWebRequest)
    
    
            ' Set some reasonable limits on resources used by this request
            request.MaximumAutomaticRedirections = 4
            request.MaximumResponseHeadersLength = 4
    
            ' Set credentials to use for this request.
            request.Credentials = CredentialCache.DefaultCredentials
    
            Dim response As HttpWebResponse = CType(request.GetResponse(), HttpWebResponse)
    
            Console.WriteLine("Content length is {0}", response.ContentLength)
            Console.WriteLine("Content type is {0}", response.ContentType)
    
            ' Get the stream associated with the response.
            Dim receiveStream As Stream = response.GetResponseStream()
    
            ' Pipes the stream to a higher level stream reader with the required encoding format. 
            Dim readStream As New StreamReader(receiveStream, Encoding.UTF8)
    
            Console.WriteLine("Response stream received.")
            Console.WriteLine(readStream.ReadToEnd())
            response.Close()
            readStream.Close()
        End Sub 'Main
    End Class 'Test
    '
    'The output from this example will vary depending on the value passed into Main 
    'but will be similar to the following:
    '
    'Content length is 1542
    'Content type is text/html; charset=utf-8
    'Response stream received.
    '<html>
    '...
    '</html>
    '
    '
    
    2006年6月15日 上午 08:22
    版主
  • 已經試過了,但是還是沒法出現ㄟ

    (Login 後就會導到錯誤的頁面)

    2006年6月16日 上午 06:01