المساعد الشخصي الرقمي

مشاهدة النسخة كاملة : Bumping an old post - https web request [modified]



C# Programming
05-29-2009, 07:40 PM
I'm still trying to get a block of code that does an https file download working. The vendor (swift.com) says that they took my code and got it working on their end no problem which leads me to believe that the something that is preventing me lives on my side of the request. We have a download scanner that pops up when I load the URL in my browser, so it's possible that I'm getting blocked there but I'm still working with the security team on that one. I'd appreciate it if someone could take a look at my code below and let me know if you see anything that's wrong/missing/poorly formed for an https request. This is being run from a .NET 2.0 console application if that makes any difference. I highlighted the line in the code where the 401 Unauthorized error occurs.

I added a few hard returns to the code to keep the page from going WAY WIDE. If you pull the code down from here and past it into VS you might have to remove them.


private bool DownloadFile()
{
private const string URL = "https://www2.swift.com/bicdownload/bicdownloader?
action=getfile&productline=bicdir&product=bicdb&content=full&format=txt&platform=win";
try
{
string bicFileTemp = string.Format("{0}\\BIC-{1}.txt",
ConfigurationManager.AppSettings["BICDLPath"], DateTime.Now.ToString("dd-MMM-yyyy"));
string line;


// build credentials for SWIFT authentication
System.Net.NetworkCredential cred = new System.Net.NetworkCredential();
cred.UserName = "myUserName";
cred.Password = "myPassword";
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
* ERROR HERE * 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());

return true;
}
catch (Exception ex)
{
Utilities.ReportError("Error in BIC-DownloadFile: " + ex.Message, this, ex.StackTrace);
return false;
}
}


Mike Devenney

modified on Friday, May 29, 2009 11:35 AM