1

我正在尝试从 url 下载文件。我尝试了两种方法,但它不适用于外部文件。

我认为它正在发生,因为我有代理互联网。我可以下载内部网络文件(图像、mp3、mp4 等等...),但是当我尝试在外部网络中下载某些内容时,它会给我timeout404 Not Found

第一种方法: System.Net.WebResponse、System.IO.FileStream

     try
        {
            var credentials = new NetworkCredential("myNetworkUserName", "myNetworkPassword", "myNetworkDomain");
            var proxy = WebProxy.GetDefaultProxy(); //new WebProxy("myNetworkProxy") <-- I TRY BOOTH WAYS
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://farm1.static.flickr.com/83/263570357_e1b9792c7a.jpg"); //External image link

            proxy.Credentials = credentials;

            request.Proxy = proxy;

            responseExternalImage = request.GetResponse();//explode here ex=""Unable to connect to the remote server""

            string fileName = GetFileName(response.ResponseUri.OriginalString);
                Stream stream = response.GetResponseStream();

                using (BinaryReader br = new BinaryReader(stream))
                {
                    content = br.ReadBytes(50000000);//perto de 50Mb
                    br.Close();
                }
                response.Close();

                FileStream fs = new FileStream(pathToSaveFile + "\\" + fileName, FileMode.Create);
                BinaryWriter bw = new BinaryWriter(fs);
                try
                {
                    bw.Write(content);
                }
                finally
                {
                    fs.Close();
                    bw.Close();
                }
        }
        catch (Exception ex)
        {

        }

GetResponse()捕获的异常说:“无法连接到远程服务器”,“”连接尝试失败,因为连接方在一段时间后没有正确响应,或者建立连接失败,因为连接的主机没有响应77.238.160.184:80""


第二种方法: System.Net.WebClient,DownloadFile

      try
        {
            var credentials = new NetworkCredential("xpta264", ConfigurationManager.AppSettings["Xpta264Password"], "ptsi");
            var proxy = WebProxy.GetDefaultProxy();
            proxy.Credentials = credentials;

            // Create a new WebClient instance.
            using (WebClient myWebClient = new WebClient())
            {
                myWebClient.Proxy = proxy;
                // Download the Web resource and save it into the current filesystem folder.
                myWebClient.DownloadFile("http://farm1.static.flickr.com/83/263570357_e1b9792c7a.jpg", pathToSaveFile + "\\testImage.jpg");
            }
        }
        catch (Exception e)
        {

        }

异常在DownloadFile方法中被捕获并给了我同样的错误。

希望可以有人帮帮我。

对不起我的英语不好

4

1 回答 1

0

WebProxy.GetDefaultProxy()已过时,不能处理所有情况。从文档

注意:此 API 现在已过时。

GetDefaultProxy 方法不会获取任何由 Internet Explorer 运行的脚本、自动配置条目或 DHCP 或 DNS 查找生成的动态设置。

应用程序应使用 WebRequest.DefaultWebProxy 属性和 WebRequest.GetSystemWebProxy 方法,而不是 GetDefaultProxy 方法。

更好的方法是尝试请求一个 url 并查看是否使用了代理。我在自己的生产系统中使用此代码,它似乎工作得很好。找到代理后,您可以在某处缓存地址:

WebProxy proxy = null;

Uri testUri = new Uri("http://www.example.com/"); // replace with REAL url
Uri proxyEndpoint = WebRequest.GetSystemWebProxy().GetProxy(testUri);
if (!testUri.Equals(proxyEndpoint))
    proxy = new WebProxy(proxyEndPoint.ToString());

你也可以试着打电话WebRequest.GetSystemWebProxy()看看是否给你代理,但我记得有问题,这就是我去上面的请求路由的原因。YMMV。

于 2011-10-07T16:04:44.493 回答