c#下载文件到本地代码

╰+哭是因爲堅強的太久メ 2021-12-15 23:29 417阅读 0赞
  1. string url = "";
  2. string html = "";
  3. try
  4. \{
  5. url = "http://www.taiwantrade.com.tw/EP/Products.do?Method=showProductDetail&catalogId=400628&company=hsin2&company\_id=136459&setLangCode=en&come\_soon=0&locale=2";
  6. HttpWebRequest req = WebRequest.Create(url) as HttpWebRequest;
  7. req.ContentType = "multipart/form-data";
  8. req.Accept = "\*/\*";
  9. req.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727)";
  10. req.Timeout = 30000;//30秒连接不成功就中断
  11. req.Method = "GET";
  12. HttpWebResponse response = req.GetResponse() as HttpWebResponse;
  13. using (StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.UTF8))
  14. \{
  15. html = sr.ReadToEnd();
  16. \}
  17. \}
  18. catch (WebException ex)
  19. \{
  20. Console.WriteLine(ex.ToString());
  21. \}
  22. Console.WriteLine(html);

发表评论

表情:
评论列表 (有 0 条评论,417人围观)

还没有评论,来说两句吧...

相关阅读