Visual Basic 使用 ReadXml 设置用户代理

发布于 2024-12-01 17:32:57 字数 893 浏览 4 评论 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

I'm trying to set the user agent for a request with XmlRead. I googled a lot about this and couldn't find the answer. Here is my chunk of code:

 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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

×眷恋的温暖 2024-12-08 17:32:57

实际执行 Web 请求的代码部分埋藏得很深,因此您必须继承一堆代码才能执行您要求的操作。相反,我建议采用不同的路径,使用易于设置标头的代码自行下载 XML,然后将其加载到数据集中。 WebClient 类允许您设置任意标头,并具有简单的 DownloadString 方法。获得后,您可以将其包装在 MemoryStream 中,并将其传递给 ReadXml()。 (我找不到将 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

The part of the code that actually performs the web request is buried pretty deep so you'd have to inherit a bunch of code to do what you asked for. Instead, let me suggest a different path, download the XML on your own with code that's easy to set that header, and then load that into the dataset. The WebClient class lets you set arbitrary headers and has a simple DownloadString method. Once you've got that you can wrap it in a MemoryStream and pass that into ReadXml(). (I couldn't find a way to read the XML as a string, that's why I was forced to read it as 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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文