如何读取xml文件并使用http请求和响应进行保存

时间:2009-10-16 12:53:46

标签: c# xml vb.net

How to read XML data from a URL by using vb.NET and save

的完全重复

嗨朋友们希望所有人都做得好。关于这个问题,我得到了一些建议,但如何实施则是混乱。任何人都可以帮助实施,以便解决问题。

Try 
    Dim strUrl As String = "http://xyz" 
    Dim wr As HttpWebRequest = CType(WebRequest.Create(strUrl), HttpWebRequest) 
    Dim ws As HttpWebResponse = CType(wr.GetResponse(), HttpWebResponse) 
    ws.ContentType = "UTF-8" 
    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

我得到了以下建议

public static void CopyStream(Stream input, Stream output) 
{ 
    byte[] buffer = new byte[8192]; 
    int bytesRead; 
    while ((bytesRead = input.Read(buffer, 0, buffer.Length)) > 0) 
    { 
        output.Write(buffer, 0, bytesRead); 
    } 
} 

提前致谢。

1 个答案:

答案 0 :(得分:0)

我之前回答了你的问题(How to read XML data from a URL by using vb.NET and save) - 这个建议的方法有什么不好?

这是在C#中,但你应该没有把它转换为VB.NET:

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

你已经完成了!

请不要一遍又一遍地问同一个问题 - 等待答案,阅读答案。

马克