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. C#
  4. Web file download from C# console application gets 401

Web file download from C# console application gets 401

Scheduled Pinned Locked Moved C#
helpcsharpjavaswiftapache
6 Posts 4 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.

    Mike Devenney

    L N M 3 Replies 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.

      Mike Devenney

      L Offline
      L Offline
      led mike
      wrote on last edited by
      #2

      Mike Devenney wrote:

      myReq.Headers.Add("Authorization", "Basic " + Convert.ToBase64String(new ASCIIEncoding().GetBytes(usernamePassword)));

      I'm not sure you should be doing that since you are using WebRequest.Credentials. At least in the case I used it I did NOT add the Header. Of course my target was an IIS Server. Also suspicious of

      Mike Devenney wrote:

      myReq.PreAuthenticate = true;

      Try it without those.

      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.

        Mike Devenney

        N Offline
        N Offline
        Noctris
        wrote on last edited by
        #3

        Well, we have some software of ours downloading files from apache and i think you are making it way to difficult:

        System.Net.NetworkCredential cred = new System.Net.NetworkCredential();
        cred.UserName = "Your Username";
        cred.Password = "Your Password";
        System.Net.WebRequest req = new System.Net.WebRequest();
        req.Credentials = cred;

        Do Or Don't, there is no "try catch ex as exception end try"

        B M 2 Replies Last reply
        0
        • N Noctris

          Well, we have some software of ours downloading files from apache and i think you are making it way to difficult:

          System.Net.NetworkCredential cred = new System.Net.NetworkCredential();
          cred.UserName = "Your Username";
          cred.Password = "Your Password";
          System.Net.WebRequest req = new System.Net.WebRequest();
          req.Credentials = cred;

          Do Or Don't, there is no "try catch ex as exception end try"

          B Offline
          B Offline
          Baeltazor
          wrote on last edited by
          #4

          thanks for that Noctris... worked for me :-)

          1 Reply Last reply
          0
          • N Noctris

            Well, we have some software of ours downloading files from apache and i think you are making it way to difficult:

            System.Net.NetworkCredential cred = new System.Net.NetworkCredential();
            cred.UserName = "Your Username";
            cred.Password = "Your Password";
            System.Net.WebRequest req = new System.Net.WebRequest();
            req.Credentials = cred;

            Do Or Don't, there is no "try catch ex as exception end try"

            M Offline
            M Offline
            Mike Devenney
            wrote on last edited by
            #5

            Thanks for the input to all who answered. We are in business. I think I had some extra credential passing going on and it must have been confusing the poor webserver. :doh:

            Mike Devenney

            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.

              Mike Devenney

              M Offline
              M Offline
              Mike Devenney
              wrote on last edited by
              #6

              Ok... turns out I was wrong. :doh: We are, in fact, not in business. I ran the code and didn't get any error so I assumed that all was well, but I wasn't getting to the line inhe code where the response was requested. When I removed the breakpoint and re-ran the code the 401 error was still happening on the line where I do:

              WebResponse wr = myReq.GetResponse();

              Anyone have any experience with the Automated SWIFT download that can see something in the code below that could be causing it to fail? I've spoken with their support team but they don't offer support on automation, all they could do is verify that my username and password were correct and that I was authorized (ironic, no?) to download the file.

              // build credentials for SWIFT authentication
              System.Net.NetworkCredential cred = new System.Net.NetworkCredential();
              cred.UserName = "mySWIFTUsername";
              cred.Password = "mwSWIFTPassword";
              WebRequest myReq = WebRequest.Create(URL);
              myReq.Credentials = cred;

              // build credentials for WTC Proxy authentication
              myReq.Proxy = WebProxy.GetDefaultProxy();
              myReq.Proxy.Credentials = CredentialCache.DefaultCredentials;

              // send request and get response
              WebResponse wr = myReq.GetResponse();
              Stream receiveStream = wr.GetResponseStream();
              StreamReader sr = new StreamReader(receiveStream, Encoding.UTF8);
              StringBuilder bicFile = new StringBuilder();

              // parse response into file, write file out to \\server\BICDownloads
              do
              {
              line = sr.ReadLine();
              bicFile.Append(line);
              } while (line != null);

              File.WriteAllText(bicFileTemp, bicFile.ToString());

              Mike Devenney

              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