C#无法使用代理保存来自url的文件

时间:2011-10-07 16:00:16

标签: c# proxy download webclient webresponse

我正在尝试从网址下载文件。我尝试了两种方法,但它不能使用外部文件。

我认为这种情况正在发生,因为我有互联网代理服务器。我可以下载内部网络文件(图像,mp3,mp4,等等......)但是当我尝试在外部网络中下载内容时,它会给我超时 404 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 方法中捕获并给出了同样的错误。

希望有人可以帮助我。

抱歉我的英文

1 个答案:

答案 0 :(得分:0)

WebProxy.GetDefaultProxy()已过时,无法处理所有情况。来自docs

  

注意:此API现已过时。

     

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

     

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

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

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。