Web authentication issue using Script Task in SSIS - c#

Hi I am using Script Task to download a file from a website but I seem to be having problems. I can't get past the web page which asks for Username and password. In the Connection Manager Editor I have listed the URL address and User Name and Passwords and the Test Connection Succeeded. So I am not sure what the problem is. Any help is appreciated.
namespace ST_054ab1f1837a4b9d8f167cfd91109f9b
{
/// <summary>
/// ScriptMain is the entry point class of the script. Do not change the name, attributes,
/// or parent of this class.
/// </summary>
[Microsoft.SqlServer.Dts.Tasks.ScriptTask.SSISScriptTaskEntryPointAttribute]
public partial class ScriptMain : Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTARTScriptObjectModelBase
{
public void Main()
{
try
{
// Logging start of download
bool fireAgain = true;
Dts.Events.FireInformation(0, "Download File", "Start downloading " + Dts.Connections["HTTP"].ConnectionString, string.Empty, 0, ref fireAgain);
// Get your newly added HTTP Connection Manager
Object mySSISConnection = Dts.Connections["HTTP"].AcquireConnection(null);
// Create a new connection
HttpClientConnection myConnection = new HttpClientConnection(mySSISConnection);
// Download file and use the Flat File Connectionstring (D:\SourceFiles\Products.csv)
// to save the file (and replace the existing file)
myConnection.DownloadFile(Dts.Connections["FileName"].ConnectionString, true);
// Logging end of download
Dts.Events.FireInformation(0, "Download File", "Finished downloading " + Dts.Connections["FileName"].ConnectionString, string.Empty, 0, ref fireAgain);
// Quit Script Task succesful
Dts.TaskResult = (int)ScriptResults.Success;
}
catch (Exception ex)
{
// Logging why download failed
Dts.Events.FireError(0, "Download File", "Download failed: " + ex.Message, string.Empty, 0);
// Quit Script Task unsuccesful
Dts.TaskResult = (int)ScriptResults.Failure;
}
}
}
}

Related

Access to the path 'c:\windows\system32\inetsrv\wooOrders.log' is denied

I have a WebApp running under the default AppPool in IIS.
In my webApi project, I have an [HttpGet] method and an [HttpPost] method.
I have a custom bit of code that basically tries to create a text file in the app location write to it and close.
string path = AppDomain.CurrentDomain.BaseDirectory;
Reporting.PrepareChannel("log", path + ".log");
Reporting.Report("WooOrders", "order created from webhook - Woo order no: ");
Reporting.Shutdown();
placing the above in my [HttpGet] method all works fine, but in my [HttpPost] method It drops onto my catch block and returns the above error?
I'm at a loss as to why this is?
In my [HttpGet] method it creates the file in the physical location that I expect it to be in which is on our 'D' drive.
Here's the full code for both methods. Although I've deployed to our live environment they are not being used yet. I've cut the methods down to literally just create the file write to it and close. I've commented out all other processing for the moment.
[Route("api/woo/createOrder")]
[HttpPost]
public string CreateOrder([FromBody] Models.Order order)
{
try
{
string path = AppDomain.CurrentDomain.BaseDirectory;
Reporting.PrepareChannel("log", path + ".log");
Reporting.Report("WooOrders", "order created from webhook - Woo order no: ");
Reporting.Shutdown();
return "all good";
}
catch (Exception ex)
{
Reporting.Shutdown();
return ex.Message;
}
}
[Route("api/woo/getOrder/{id}")]
[HttpGet]
public string OrderGet([FromUri] string Id)
{
try
{
string path = AppDomain.CurrentDomain.BaseDirectory;
Reporting.PrepareChannel("log", path + ".log");
Reporting.Report("WooOrders", "Get Order ");
Reporting.Shutdown();
return "all good";
}
catch (Exception ex)
{
Reporting.Shutdown();
return ex.Message;
}
}

What is the namespace of Google.Apis.Authentication;

I'm trying to use this link to download a file from Google Drive using dotnet.
The problem is, I can't find this namespace in the nuget - using Google.Apis.Authentication; .
I'v downloaded everything that has the name 'Google' in nuget, but no luck.
Any idea where it can hide? Thanks
To access Google drive the only nugget package you need to download is PM> Install-Package Google.Apis.Drive.v2. It will automatically add anything else you need.
My download from drive method
/// <summary>
/// Download a file
/// Documentation: https://developers.google.com/drive/v2/reference/files/get
/// </summary>
/// <param name="_service">a Valid authenticated DriveService</param>
/// <param name="_fileResource">File resource of the file to download</param>
/// <param name="_saveTo">location of where to save the file including the file name to save it as.</param>
/// <returns></returns>
public static Boolean downloadFile(DriveService _service, File _fileResource, string _saveTo)
{
if (!String.IsNullOrEmpty(_fileResource.DownloadUrl))
{
try
{
var x = _service.HttpClient.GetByteArrayAsync(_fileResource.DownloadUrl );
byte[] arrBytes = x.Result;
System.IO.File.WriteAllBytes(_saveTo, arrBytes);
return true;
}
catch (Exception e)
{
Console.WriteLine("An error occurred: " + e.Message);
return false;
}
}
else
{
// The file doesn't have any content stored on Drive.
return false;
}
}
code ripped from google drive sample project
I think that there is a better sample for you (in the official samples repo, https://github.com/google/google-api-dotnet-client-samples/blob/master/Drive.Sample/Program.cs#L154).
...
await DownloadFile(service, uploadedFile.DownloadUrl);
...
/// <summary>Downloads the media from the given URL.</summary>
private async Task DownloadFile(DriveService service, string url)
{
var downloader = new MediaDownloader(service);
var fileName = <PATH_TO_YOUR_FILE>
using (var fileStream = new System.IO.FileStream(fileName,
System.IO.FileMode.Create, System.IO.FileAccess.Write))
{
var progress = await downloader.DownloadAsync(url, fileStream);
if (progress.Status == DownloadStatus.Completed)
{
Console.WriteLine(fileName + " was downloaded successfully");
}
else
{
Console.WriteLine("Download {0} was interpreted in the middle. Only {1} were downloaded. ",
fileName, progress.BytesDownloaded);
}
}
}
More documentation about media download is available here:
https://developers.google.com/api-client-library/dotnet/guide/media_download

Command line SFTP "Local to local copy not supported" error

I'm running a program written in C# from the command line as administrator that should generate the batch file (which it does), and then it should sFTP the file to a remote site. I have verified the username and password are correct. When I run the utility (C# program) to do this it says it's transferring the file and then immediately gives me this
ERROR: Local to local copy not supported.
However, I can manually (through Filezilla) move the file from our server to their site. It's probably something silly, but I just can't seem to figure it out. Any help is appreciated!
There are many files to this program, but here is where the most of the FTP stuff is in the code. I hope it helps:
if (pars.ContainsKey("ftp"))
{
var env = (pars.ContainsKey("ftp") ? pars["ftp"] : null) ?? "default";
entities = entities ?? new SBLStagingEntities();
settings = settings ?? new SettingReader(entities, env).GetSetting();
var filename = Path.GetFileName(pars["path"]);
Console.WriteLine("Transfering {0} using sFTP ................................\t\t", filename);
var processors = new SblFtpTransport(settings);
processors.ProcessFile(pars["path"]);
Console.Write("sFTP Done\n");
}
///-----------------------a different class that is called from the first one------///
public SblFtpTransport(Settings settings)
{
_settings = settings;
}
/// <summary>
/// this method is called by file watcher for each new file dropped in the watched folder
/// </summary>
/// <param name="file"></param>
public void ProcessFile(string file)
{
var fileName = Path.GetFileName(file);
if (!File.Exists(file) || string.IsNullOrEmpty(fileName))
{
Console.Error.WriteLine("file does not exist");
return;
}
//ftp the file and record the result in db
var result = FtpFile(file);
Log(fileName, result);
Console.Write("{0}", result);
Archive(result, file);
}
///-------------------------------another class that is used--------------///
public class WatcherSettings
{
public IFileProcessor CreateProcessor()
{
return new SblFtpTransport(new Settings()
{
AchiveFolder = #"C:\Docs\place\Watcher\Archived",
FtpPort = "22",
FtpServer = "xxxxx.someplace.net",
FtpTargetPath = "/StudentBatchLoad_FW",
FtpUsername = "xxx",
Password = "xxxxxxx",
});
}
public string WatcherPath { get; set; }
}
As far as I can tell, you never call CreateProcessor(). And it appears you need to call that so the settings get created properly with the remote host, and that's why you get an error that you're trying to copy to local host. So change your code to call that.
But your code is extremely disjointed and hard to read. Spend some time cleaning it, and step through it with a debugger to see exactly what's happening.

How to create analog for such C# StreamProxyApplication function in C++ using Boost library?

I haven't done work in C/C++ for a little bit and was just wondering if any one can help me with porting this .Net C# code into C++ using Boost library (Boost.Asio)
So I have one function:
private const int bufferSize = 8192;
/// <summary>
/// The entry point of the application.
/// </summary>
/// <param name="args">The input arguments of the application. The first argument should be a valid http address of the source server. The second argument should be a valid http address of the destination server.</param>
public static void Main(string[] args)
{
// Check the amount of the arguments.
if (args.Length < 2)
{
Console.Write("Not enough arguments. Expected 2, recieved " + args.Length + ".");
return;
}
// Try to get the source server URI.
Uri sourceUri;
try
{
sourceUri = new Uri(args[0]);
}
catch (UriFormatException)
{
Console.Write("Invalid source server address.");
return;
}
// Try to get the destination server URI.
Uri destinationUri;
try
{
destinationUri = new Uri(args[1]);
}
catch (UriFormatException)
{
Console.Write("Invalid destination server address.");
return;
}
// Try to connect to the source server.
Stream sourceStream;
try
{
sourceStream = WebRequest.Create(sourceUri).GetResponse().GetResponseStream();
}
catch (Exception exception)
{
Console.Write("Cannot connect to the source server. Details: " + exception.Message);
return;
}
// Try to connect to the destination server.
NetworkStream destinationStream;
try
{
destinationStream = new TcpClient(destinationUri.Host, destinationUri.Port).GetStream();
}
catch (Exception exception)
{
Console.Write("Cannot connect to the destination server. Details: " + exception.Message);
return;
}
// Start redirecting the data.
int read = 0;
byte[] buffer = new byte[bufferSize];
do
{
try
{
read = sourceStream.Read(buffer, 0, bufferSize);
}
catch (Exception exception)
{
Console.Write("An error occurred while receiving data from the source server. Details: " + exception.Message);
return;
}
try
{
destinationStream.Write(buffer, 0, read);
}
catch (Exception exception)
{
Console.Write("An error occurred while sending data to the destination server. Details: " + exception.Message);
return;
}
}
while (read > 0);
Console.Write("All data redirected.");
}
How to create its simple\general analog in C++?
main Idea here is to transmit live streaming data from one broadcasticg source to another.
I don't think this is going to be simple at all to port to C++. What takes one line in C# requires a whole bunch of infrastructure in C++, because you have the entire .Net framework at your fingertips in the CLR. I believe boost::asio is a lower-level interface than you need for this. If you do have to do this in C++ then there is stuff in ATL Server or WinHTTP which could prove useful.
Is there any way you can encapsulate the code from here that you need in a small C# assembly, and call into it from C++ using COM interop?

Acessing a url from windows application

Does anyone know how to acess a url from a windows application?.
I have an address http://serverport/Page.I want to acess this page from my windows application.
Regards,
Harsh Suman
It's not clear what you want to do with the page.
If you want to display it on the form, you can use a WebBrowser control.
If you want to get the response and process it, use the System.Net.WebClient class.
If you want to download an HTML or any file you can use the WebClient class.
Example:
/// <summary>
/// Downloads a file from the given location
/// </summary>
/// <param name="url">Location of the file</param>
/// <param name="dest">The destination of the downloaded file</param>
/// <returns>False if there was an error, else True</returns>
public bool DownLoad(string url, string dest)
{
WebClient client = new WebClient();
try
{
//Downloads the file from the given url to the given destination
client.DownloadFile(url, dest);
return true;
}
catch (WebException)
{
// Handle exception
return false;
}
catch (System.Security.SecurityException)
{
// Handle exception
return false;
}
catch (Exception)
{
// Handle exception
return false;
}
}
I'm not sure what you're asking for,so I just give the answer to yet another way to interpret the question.
If you simply want to launch the default browser (to display a local or online html manual etc.), in windows (and probably similar in other OS'es) you can use some kind of "execute interface" to execute a correctly formatted url as the command, this will usually launch the default browser:
According to this page this code should launch a browser:
string targeturl= "http://stackoverflow.com";
try
{
System.Diagnostics.Process.Start(targeturl);
}
catch
(
System.ComponentModel.Win32Exception noBrowser)
{
if (noBrowser.ErrorCode==-2147467259)
MessageBox.Show(noBrowser.Message);
}
catch (System.Exception other)
{
MessageBox.Show(other.Message);
}
(It looks pretty ugly with magic numbers for error codes, though...)

Categories

Resources