WebClient class is throwing exception

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Deepson Thomas

    #1

    WebClient class is throwing exception

    Hi,

    Iam trying to use WebClient class under system.net namespace to download
    files from internet via http. For that iam using DownloadFile method. But
    when ever iam trying to download any file iam getting the error “"The
    underlying connection was closed: Unable to connect to the remote server”. My
    computer is behind proxy. If iam trying to open this image via browser then
    it is working.

    I am pasting my code below.

    string remoteUri = "http://www.deepson.com/images/";
    string fileName = "uc2_large08.jp g", myStringWebReso urce = null;


    WebClient myWebClient = new WebClient();
    myStringWebReso urce = remoteUri + fileName;
    Response.Write( "<hr>Downloadin g File "+ fileName + " from "+
    myStringWebReso urce );


    myWebClient.Dow nloadFile(myStr ingWebResource, "C:\\inetpub\\w wwroot\\TestWeb App\\Downloaded \\"+fileName) ;

    Response.Write( "<hr>Successful ly Downloaded File "+ fileName +" from "+
    myStringWebReso urce);

    When I am executing this code the file is created with 0 KB size but when
    the error comes it is deleted automatically.

    --
    Deepson Thomas

  • Jesse Houwing

    #2
    Re: WebClient class is throwing exception

    Deepson Thomas wrote:
    Hi,
    >
    Iam trying to use WebClient class under system.net namespace to download
    files from internet via http. For that iam using DownloadFile method. But
    when ever iam trying to download any file iam getting the error “"The
    underlying connection was closed: Unable to connect to the remote server”. My
    computer is behind proxy. If iam trying to open this image via browser then
    it is working.
    I think you've already answered your own question. You need to set up
    your browser credentials for the connection:

    WebProxy ProxyObject = new WebProxy("http://194.3.3.60:80" , true);
    NetworkCredenti al cred = new NetworkCredenti al("User","pass word");
    ProxyObject.Cre dentials=cred;
    GlobalProxySele ction.Select=pr oxyObject ;

    >
    I am pasting my code below.
    >
    string remoteUri = "http://www.deepson.com/images/";
    string fileName = "uc2_large08.jp g", myStringWebReso urce = null;
    >
    >
    webclient myWebClient = new WebClient();
    myStringWebReso urce = remoteUri + fileName;
    Response.Write( "<hr>Downloadin g File "+ fileName + " from "+
    myStringWebReso urce );
    >
    >
    myWebClient.Dow nloadFile(myStr ingWebResource, "C:\\inetpub\\w wwroot\\TestWeb App\\Downloaded \\"+fileName) ;
    >
    Response.Write( "<hr>Successful ly Downloaded File "+ fileName +" from "+
    myStringWebReso urce);
    >
    When I am executing this code the file is created with 0 KB size but when
    the error comes it is deleted automatically.
    >

    Comment

    Working...