gpt4 book ai didi

vb.net - 如何使用 vb.NET 从 URL 读取 XML 数据并保存

转载 作者:行者123 更新时间:2023-12-02 06:50:20 26 4
gpt4 key购买 nike

friend 们,我能够通过单个字节获取 XML 文件,也许这遇到了一些问题。您可以建议我使用替代方法来执行相同的操作来保存 XML 文件吗?

  Try
Dim strUrl As String = "http://example.com"
Dim wr As HttpWebRequest = CType(WebRequest.Create(strUrl), HttpWebRequest)
Dim ws As HttpWebResponse = CType(wr.GetResponse(), HttpWebResponse)
ws.ContentType = "UTF-16"
Dim str As Stream = ws.GetResponseStream()
Dim inBuf(100000) As Byte
Dim bytesToRead As Integer = CInt(inBuf.Length)
Dim bytesRead As Integer = 0
While bytesToRead > 0
Dim n As Integer = str.Read(inBuf, bytesRead, bytesToRead)
If n = 0 Then
Exit While
End If
bytesRead += n
bytesToRead -= n
End While
Dim fstr As New FileStream("c:/GetXml.xml", FileMode.OpenOrCreate, FileAccess.Write)
fstr.Write(inBuf, 0, bytesRead)
str.Close()
fstr.Close()
Catch ex As WebException
Response.Write(ex.Message)
End Try

最佳答案

为什么不直接使用 WebClient 类及其 DownloadFile 方法?看起来容易多了......

这是用 C# 编写的,但将其转换为 VB.NET 应该没有问题:

WebClient wc = new WebClient();
wc.DownloadFile("http://xyz", @"C:\getxml.xml");

你就完成了!

马克

关于vb.net - 如何使用 vb.NET 从 URL 读取 XML 数据并保存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1576534/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com