0

XmlReadを使用してリクエストのユーザーエージェントを設定しようとしています。私はこれについてたくさんグーグルで検索しましたが、答えが見つかりませんでした。これが私のコードのチャンクです:

 Dim RssData As DataSet
        Dim Title As String
        Dim Url As String
        Dim Stream As String
        Dim buffer As Integer
        RssData = New DataSet()
        RssData.ReadXml("http://localhost/user_agent.php")
        buffer = 0
        For Each RssRow As DataRow In RssData.Tables("entry").Rows
            Title = Microsoft.VisualBasic.Left(RssRow.Item("title").ToString, 30)
            Stream += Title & vbCrLf

        Next
        LinkLabel3.Text = Stream

        For Each RssRow As DataRow In RssData.Tables("entry").Rows
            Title = Microsoft.VisualBasic.Left(RssRow.Item("title").ToString, 30)
            Url = RssRow.Item("url").ToString
            LinkLabel3.Links.Add(buffer, Title.Length, Url)
            buffer = buffer + Title.Length + 2
        Next
4

1 に答える 1

1

Webリクエストを実際に実行するコードの部分はかなり深く埋め込まれているため、要求したことを実行するには、一連のコードを継承する必要があります。代わりに、別のパスを提案し、そのヘッダーを簡単に設定できるコードを使用してXMLを独自にダウンロードし、それをデータセットにロードします。このWebClientクラスでは、任意のヘッダーを設定でき、簡単なDownloadStringメソッドがあります。それを取得したら、それをでラップして、MemoryStreamに渡すことができますReadXml()。(XMLを文字列として読み取る方法が見つからなかったため、XMLを文字列として読み取る必要がありましたStream。)

    ''//Will hold our downloaded XML
    Dim MyXml As String

    ''//Create a webclient to download our XML
    Using WC As New System.Net.WebClient()
        ''//Manually set the user agent header
        WC.Headers.Add("user-agent", "your user agent here")
        ''//Download the XML
        MyXml = WC.DownloadString("http://localhost/user_agent.php")
    End Using
    ''//Create our dataset object
    Dim RssData As New DataSet()
    ''//There is no direct method to load XML as a string (at least that I could find) so we will
    ''//    convert it to a byte array and load it into a memory stream
    Dim Bytes As Byte() = System.Text.Encoding.UTF8.GetBytes(MyXml)
    Using MS As New System.IO.MemoryStream(Bytes)
        ''//Load the stream into the reader
        RssData.ReadXml(MS)
    End Using

    ''//Your code continues normally here
于 2011-08-29T18:27:59.887 に答える