Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Windows Forms
  4. C# console application for web file download gets 401

C# console application for web file download gets 401

Scheduled Pinned Locked Moved Windows Forms
helpcsharpjavaswiftapache
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Mike Devenney
    wrote on last edited by
    #1

    I've been working on automating a file download from the swift.com website. I don't know if this makes a difference but the site uses the Apache webserver. The documentation from SWIFT stresses that they're using the Apache HTTP Client Library, but their sample code is in Java, so that might be why. I assumed that a web request is a web request, regardless of the library you use to create it, but here I am asking for help, so maybe my assumption is incorrect. I've included a portion of the code I wrote to download the file.

    ...
    WebRequest myReq = WebRequest.Create(URL);
    string username = "me@mydomain.com";
    string password = "myPassword";
    string usernamePassword = username + ":" + password;
    CredentialCache mycache = new CredentialCache();
    mycache.Add(new Uri(URL), "Basic", new NetworkCredential(username, password, "swift.com"));
    myReq.Credentials = mycache;
    myReq.PreAuthenticate = true;
    myReq.Headers.Add("Authorization", "Basic " + Convert.ToBase64String(new ASCIIEncoding().GetBytes(usernamePassword)));
    myReq.Proxy = WebProxy.GetDefaultProxy();
    myReq.Proxy.Credentials = CredentialCache.DefaultCredentials;
    <-- Error occurs on the next line: The remote server returned an error: (401) Unauthorized -->
    WebResponse wr = myReq.GetResponse();
    Stream receiveStream = wr.GetResponseStream();
    StreamReader sr = new StreamReader(receiveStream, Encoding.UTF8);
    StringBuilder bicFile = new StringBuilder();
    ...

    Anyone care to shed some light on this for me? I've logged into the site using the credentials so I know I have them right. Cross posted in C# and Windows-Forms forums.

    Mike Devenney

    B 1 Reply Last reply
    0
    • M Mike Devenney

      I've been working on automating a file download from the swift.com website. I don't know if this makes a difference but the site uses the Apache webserver. The documentation from SWIFT stresses that they're using the Apache HTTP Client Library, but their sample code is in Java, so that might be why. I assumed that a web request is a web request, regardless of the library you use to create it, but here I am asking for help, so maybe my assumption is incorrect. I've included a portion of the code I wrote to download the file.

      ...
      WebRequest myReq = WebRequest.Create(URL);
      string username = "me@mydomain.com";
      string password = "myPassword";
      string usernamePassword = username + ":" + password;
      CredentialCache mycache = new CredentialCache();
      mycache.Add(new Uri(URL), "Basic", new NetworkCredential(username, password, "swift.com"));
      myReq.Credentials = mycache;
      myReq.PreAuthenticate = true;
      myReq.Headers.Add("Authorization", "Basic " + Convert.ToBase64String(new ASCIIEncoding().GetBytes(usernamePassword)));
      myReq.Proxy = WebProxy.GetDefaultProxy();
      myReq.Proxy.Credentials = CredentialCache.DefaultCredentials;
      <-- Error occurs on the next line: The remote server returned an error: (401) Unauthorized -->
      WebResponse wr = myReq.GetResponse();
      Stream receiveStream = wr.GetResponseStream();
      StreamReader sr = new StreamReader(receiveStream, Encoding.UTF8);
      StringBuilder bicFile = new StringBuilder();
      ...

      Anyone care to shed some light on this for me? I've logged into the site using the credentials so I know I have them right. Cross posted in C# and Windows-Forms forums.

      Mike Devenney

      B Offline
      B Offline
      bc_sridhar
      wrote on last edited by
      #2

      Mike where you able to solve this, I am facing the same problem, if you have a solution for this please share Thanks, Sridhar bc_sridhar@yahoo.com

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups