2009-10-16 49 views
0

How to read XML data from a URL by using vb.NET and save如何讀取XML文件並保存使用的HttpRequest和響應


喜的朋友完全相同的副本希望所有的R做得很好。關於這個問題,我得到了一些建議,但如何實施是困惑。任何人都可以幫助實施,以便解決問題。

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); 
    } 
} 

感謝。

回答

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"); 

就大功告成了!

請不要一遍又一遍地問同樣的問題 - 等待答案,閱讀答案。

Marc

+0

謝謝先生,但我去了流輸入和輸出中給出的另一個解決方案。 – pravakar 2009-10-16 13:03:10

+0

@marc_s,所以你可以要求關閉這個問題,所以我們繼續回答以前的問題? – 2009-10-16 13:16:54

+0

是的先生與webclient文件下載非常感謝,是的,我們可以去以前的一個。 – pravakar 2009-10-16 13:33:15