The following code works perfectly fine from my own machine (Win7 ISS7) but when I move it to a virtual server running IIS8 on a datacentre then I get the return code 150 (openingdata). I can access the ftp site via IE on this server. Is this a coding issue or configuration. Any help is greatly appreciated.
I've also tried changing UsePassive, UseBinary, no caching, to no effect and place it on an azure machine but to no avail.
private List<string> Browse()
{
// Get the object used to communicate with the server.
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(m_Url);
request.Method = WebRequestMethods.Ftp.ListDirectoryDetails;
result.Add("Timeout = " + request.Timeout.ToString());
// This example assumes the FTP site uses anonymous logon.
request.Credentials = new NetworkCredential(m_Username, m_Password);
request.CachePolicy = new RequestCachePolicy(RequestCacheLevel.NoCacheNoStore);
if (m_Proxy != null)
{
request.Proxy = m_Proxy;
}
bool started = false;
using (FtpWebResponse response = (FtpWebResponse)request.GetResponse())
{
Stream responseStream = response.GetResponseStream();
using (StreamReader reader = new StreamReader(responseStream))
{
string line = reader.ReadLine();
while (line != null)
{
result.Add(line);
line = reader.ReadLine();
}
}
}
return result;
}
Turned out it was a program error
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(m_Url);
should have been
FtpWebRequest request = (FtpWebRequest)*Ftp*WebRequest.Create(m_Url);
Related
I am facing en enconding issue when downloading a file from Sharepoint Online by an Azure function. So I have an Azure HTTP triggered function that calls Sharepoint Online to retrieve a file and download it. Here is how I call Sharepoint:
public dynamic DownloadFile(Guid fileUniqueId)
{
const string apiUrl = "{0}/_api/web/GetFileById('{1}')/$value";
try
{
var fileInfo = GetFileInfo(fileUniqueId);
if (string.IsNullOrEmpty(_sharepointSiteUrl)) return null;
string api = string.Format(apiUrl, _sharepointSiteUrl, fileUniqueId.ToString());
string response = new TokenHelper().GetAPIResponse(api);
if (string.IsNullOrEmpty(response)) return null;
return new {
fileInfo.FileName,
Bytes = Encoding.UTF8.GetBytes(response)
};
}
catch (Exception ex)
{
throw ex;
}
}
And Here is the Azure App function that is called:
string guidString = req.Query["id"];
if (!Guid.TryParse(guidString, out var fileId))
return new BadRequestResult();
var fileManager = new FileManager();
dynamic fileData = fileManager.DownloadFile(fileId);
if (null == fileData) return new NotFoundResult();
var contentType = (((string)fileData.FileName).ToUpper().EndsWith(".PNG") || ((string)fileData.FileName).ToUpper().EndsWith(".JPEG") || ((string)fileData.FileName).ToUpper().EndsWith(".JPG")) ? "image/jpeg" : "application/octet-stream";
return new FileContentResult(fileData.Bytes, contentType)
{
FileDownloadName = fileData.FileName
};
The file is succesfully downloaded but it seems corrupted as it says that the file type is not recognised. I think that it's an issue related to encoding. Does somebody sees what I'm doing wrong ?
Your code is using UTF8.GetBytes() to try and get the file content from SharePoint Online. You should instead use the CSOM method OpenBinaryDirect() like this:
var fileRef = file.ServerRelativeUrl;
var fileInfo = Microsoft.SharePoint.Client.File.OpenBinaryDirect(clientContext, fileRef);
using (var fileStream = System.IO.File.Create(fileName))
{
fileInfo.Stream.CopyTo(fileStream);
}
I'm trying to access JSON on remote server. The code works on a local replica of the database but on the server there is a "HTTP Web Server: Command Not Handled Exception" error. I have lotusscript agents doing similar things so I am thinking it is a problem with java permissions on the server.
As far as I can tell I have all permissions on the server to run the code. I've tried putting the code in to Javascript and a Java managed bean so don't think it is an issue with the code.
The server is running 11.0.1FP1 (I've read there may be issues with pol files in 11)
Any help or hints would be greatly received
public String getJSON(String url)
{
String returnCode = url;
try {
String jsonTxt = null;
URL myURL = new URL(url);
HttpURLConnection myURLConnection = (HttpURLConnection) myURL.openConnection();
if (myURLConnection.getResponseCode() < 400) {
returnCode = "Connection made";
} else {
/* error from server */
jsonTxt = "Can not access remote server";
returnCode = "Connection not made";
}
myURLConnection.disconnect();
} catch(Exception e) {
e.printStackTrace();
}
return returnCode;
}
I have a process that creates an application and application pool using the Server Manager object in the Microsoft.Web.Administration namespace, the application pool is created first and then the application, assigning the newly created app pool to the application, code below.
protected TResult UseServerManagerWrapper<TResult>(Func<ServerManager, TResult> func)
{
using (var serverManager = new ServerManager())
{
return func(serverManager);
}
}
Application creation function
public void CreateApplication(String siteName, String parentApplicationName, String organisationName, String applicationName, String applicationPoolName)
{
UseServerManagerWrapper(serverManager =>
{
var site = serverManager.Sites[siteName];
var newApplication =
site.Applications.Add(
GetApplicationPath(parentApplicationName, organisationName, applicationName),
this.GetGeneratedApplicationPhysicalPath(siteName, parentApplicationName, organisationName, applicationName));
newApplication.ApplicationPoolName = applicationPoolName;
serverManager.CommitChanges();
return true;
});
}
and app pool creation.
public Boolean CreateApplicationPool(String applicationPoolName)
{
return UseServerManagerWrapper(serverManager =>
{
var appPool = serverManager.ApplicationPools.Add(applicationPoolName);
appPool.ManagedPipelineMode = ManagedPipelineMode.Integrated;
appPool.ManagedRuntimeVersion = "";
serverManager.CommitChanges();
return true;
});
}
This all works fine, the only problem I have is that I have to go into the application folder and manually assign permissions for the application pool.
I can't see anything in the ServerManager documentation that can help me and I can't figure out a way to use the Directory.SetAccessControl Method to give an application pool permissions. Is there anyway to do this in code?
Apologies if I'm using wrong terminology or anything, I'm new to publishing in general. Let me know if you need anymore info.
Ok, so after a lot of searching and some trial and error I've found the resolution and it's nothing to do with the ServerManager object. First of all to get this to work in ASP.NET Core 2.1 (1.x/2.x) I needed the System.IO.FileSystem.AccessControl Nuget and the Namespaces below.
using System.Security.AccessControl;
using System.Security.Principal;
These give the ability to modify the ACL of files and folders and then the CreateApplication function becomes the below.
public void CreateApplication(String siteName, String parentApplicationName, String organisationName, String applicationName, String applicationPoolName)
{
UseServerManagerWrapper(serverManager =>
{
var site = serverManager.Sites[siteName];
var generatedPath = this.GetGeneratedApplicationPhysicalPath(siteName, parentApplicationName, organisationName, applicationName);
var newApplication =
site.Applications.Add(
GetApplicationPath(parentApplicationName, organisationName, applicationName),
generatedPath);
newApplication.ApplicationPoolName = applicationPoolName;
var dInfo = new DirectoryInfo(generatedPath);
var acl = dInfo.GetAccessControl();
var acct = new NTAccount($"IIS APPPOOL\\{applicationPoolName}");
acl.AddAccessRule(new FileSystemAccessRule(acct, FileSystemRights.FullControl, InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit, PropagationFlags.NoPropagateInherit, AccessControlType.Allow));
dInfo.SetAccessControl(acl);
serverManager.CommitChanges();
return true;
});
}
The code between "newApplication.ApplicationPoolName = applicationPoolName" and "serverManager.CommitChanges()" gets the ACL from the newly generated directory giving the ability to modify it and reassign with a new FileSystemAccessRule.
I'm trying to use the c# library to download a file from an FTP. The code we are using is straight forward.
static void Main(string[] args)
{
Connect(true, true, true);
}
private static void Connect(bool keepAlive, bool useBinary, bool usePassive)
{
string RemoteFtpPath = "ftp://ftp.xxxx.ac.uk/incoming/testExtractCSVcoursesContacts.csv";
const string Username = "anonymous";
const string Password = "anonymous#xxxx.ac.uk";
var request = (FtpWebRequest)WebRequest.Create(new Uri(RemoteFtpPath));
request.Method = WebRequestMethods.Ftp.DownloadFile;
request.KeepAlive = keepAlive;
request.UsePassive = usePassive;
request.UseBinary = useBinary;
request.Credentials = new NetworkCredential(Username, Password);
request.Timeout = 30000;
try
{
var response = (FtpWebResponse)request.GetResponse();
var responseStream = response.GetResponseStream();
var reader = new StreamReader(responseStream);
var fileString = reader.ReadToEnd();
Console.WriteLine(
$"Success! keepAlive={keepAlive}, useBinary={useBinary}, usePassive={usePassive} Length={fileString.Length}");
reader.Close();
response.Close();
}
catch (Exception e)
{
Console.WriteLine(
$"Failed! keepAlive={keepAlive}, useBinary={useBinary}, usePassive={usePassive}, message={e.Message}");
}
}
`
we also tried to set passive = true with identical results.
When we run it, using wireshark we are getting : Wireshark log c#
Now we tried the same with Python and it's working just fine:
import urllib.request
data = urllib.request.urlretrieve('path')
print(data)
the wireshark log looks quite different:
So tried different things, but not able to sort this out.
Some ftp servers don't support OPTS UTF8 but still transmit file names in UTF8. (Note that 'OPTs UTF8' is NOT required by the FTP Internationalization Standard, although supporting UTF8 file names is.) The .NET Ftp classes will use the default code page if they don't get an OK response to OPTS UTF8... It's unfortunate that MS didn't provide some way to use UTF8 anyway, since this leaves you unable to transmit international file names to and from otherwise UTF8-compliant servers.
The issue is sorted after using a different library as FtpWebRequest doesn't support it
I must access some data from IIS by connecting my IIS to a virtual machine to avoid any further configuration.
The code is running well but after I sideload my app, it seems that my app can't reach my data anymore. For example, I've got a folder called Video in my shared folder and I just changed:
static string adresse ="http://localhost"
into
static string adresse = "http://172.16.1.113";
the app can still run well when I am connected wireless to the network but when I use a device connected with fixed internet I got a message saying it can't connect to the server
public static async Task<List<Uri>> GetMedia()
{
try
{
List<Uri> target = new List<Uri>();
HtmlDocument document = new HtmlDocument();
var httpClient = new HttpClient();
var urlVideos = adresse + "/Videos";
var response = await httpClient.GetAsync(urlVideos);
var result = await response.Content.ReadAsStringAsync();
string htmlString = result;
document.LoadHtml(htmlString);
var collection = document.DocumentNode.DescendantsAndSelf();
foreach (HtmlNode link in collection)
{
if (link.Attributes.Contains("href") && !string.IsNullOrEmpty(link.Attributes["href"].Value.Trim().Trim('/')))
{
target.Add(new Uri(adresse + "" + link.Attributes["href"].Value));
}
}
return target;
}
catch (Exception)
{
string errors = "Proxy.getMedia" + iLine.ToString();
App.ProxyErrors = errors;
throw;
}
}
Any suggestions?
I found a solution I just had to go to app.manifest then capabilities
end enable private networks(Client and Server)