How to GET data from an URL and save it into a file in binary in C#.NET without the encoding mess?

前端 未结 3 778
悲&欢浪女
悲&欢浪女 2021-02-01 18:51

In C#.NET, I want to fetch data from an URL and save it to a file in binary.

Using HttpWebRequest/Streamreader to read into a string and saving using StreamWriter works

3条回答
  •  面向向阳花
    2021-02-01 19:37

    This is what I use:

    sUrl = "http://your.com/xml.file.xml";
    rssReader = new XmlTextReader(sUrl.ToString());
    rssDoc = new XmlDocument();
    
    WebRequest wrGETURL;
    wrGETURL = WebRequest.Create(sUrl);
    
    Stream objStream;
    objStream = wrGETURL.GetResponse().GetResponseStream();
    StreamReader objReader = new StreamReader(objStream, Encoding.UTF8);
    WebResponse wr = wrGETURL.GetResponse();
    Stream receiveStream = wr.GetResponseStream();
    StreamReader reader = new StreamReader(receiveStream, Encoding.UTF8);
    string content = reader.ReadToEnd();
    XmlDocument content2 = new XmlDocument();
    
    content2.LoadXml(content);
    content2.Save("direct.xml");
    

提交回复
热议问题