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