I am trying to create a connection to a remote server to upload a file, the connection needs to be over SSL and I have only a bunch of ports available on routers from both sides. The connection seems to succeed, however the Stream requestStream = request.GetRequestStream(); times out. After inspecting tracing log, the following line seems suspicious:
System.Net Information: 0 : [7144] FtpControlStream#33675143 - Created connection from [myIP]:64230 to [theirIP]:990.
Although it says connection was created, I know that port 64230 is not open, and my guess is that's why it fails. So is it possible to specify which port to create a local connection from? Or maybe I'm missing something else?
[EDIT]:
I set up FTP site on IIS on my local machine for testing, to see what could possibly be going wrong, but I'm getting the same problem. What's interesting is that I can connect on pretty much any port with the same settings, except 990. I can now rule out the firewall, it doesn't seem to be the problem. Any other guesses? Here is the sample code:
string fileName = "file.pdf";
FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://[host]:990/file.pdf");
request.Method = WebRequestMethods.Ftp.UploadFile;
request.EnableSsl = true;
request.UsePassive = true;
ServicePointManager.ServerCertificateValidationCallback = AcceptAllCertifications;
request.Credentials = new NetworkCredential("[host]|[username]", "[password]");
Stream requestStream = request.GetRequestStream();
FileStream stream = File.OpenRead(fileName);
const int bufferLength = 1024;
byte[] buffer = new byte[bufferLength];
int count = 0;
int readBytes = 0;
do
{
readBytes = stream.Read(buffer, 0, bufferLength);
requestStream.Write(buffer, 0, readBytes);
count += readBytes;
}
while (readBytes != 0);
stream.Close();
requestStream.Close();
Console.WriteLine("Upload complete");
Shouldn't your connection string be:
ftps://[host]:990/file.pdf
Also I know you said you could rule out the firewall, but it often a headache with FTPS maybe this serverfault answer might be of use:
What firewall ports do I need to open when using FTPS?
Related
I am currently working on a keylogger which is saving the users input to a text document. The document is updated each time the user presses a button.
I want the FTP to constantly update the text document on the server. The issue is that each time it is uploading the text document, it stops until the upload is complete and then continues logging.
I would like to know how can I prevent this from happening.
I read somewhere there is a way to do this by using an ASYNC function or something like that but I do not know where it was.
I would greatelly appreciate any help.
Here is the FTP code I created.
private static void ftp(String name)
{
FtpWebRequest request = (FtpWebRequest)FtpWebRequest.Create(
"ftp://ftp.drivehq.com/test.txt");
request.Method = WebRequestMethods.Ftp.UploadFile;
request.Credentials = new NetworkCredential(username, pass);
request.UsePassive = true;
request.UseBinary = true;
request.KeepAlive = false;
FileStream stream = File.OpenRead(name);
byte[] data = new byte[stream.Length];
stream.Read(data, 0, data.Length);
stream.Close();
Stream reqStream = request.GetRequestStream();
reqStream.Write(data, 0, data.Length);
reqStream.Close();
}
I managed to find a way how to fix it. I simply used timers and I am uploading every 10 seconds. The issue was not with FTP interrupting execution, but FTP flooding the program with constant uploading.
As I said. The timers fixed it.
My concept is downloading a image from url and sending the image(Linked Resource)to mail message to MSMQ!, I can sucessfully download the image , but i cannot able to send it to MSMQ, i need to serialize the Alternative Views, which i could not able to do?
Here is the code
MailMessage m = new MailMessage();
string strBody="<h1>This is sample</h1><image src=\"cid:image1\">";
m.Body = strBody;
AlternateView av1 = AlternateView.CreateAlternateViewFromString(strBody, null, MediaTypeNames.Text.Html);
Here I am Downloading the Image from url
Stream DownloadStream = ReturnImage();
LinkedResource lr = new LinkedResource(DownloadStream, MediaTypeNames.Image.Gif);
lr.ContentId = "image1";
av1.LinkedResources.Add(lr);
m.AlternateViews.Add(av);
private Stream ReturnImage()
{
try
{
HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(urlForImage);
webRequest.ProtocolVersion = HttpVersion.Version10;
webRequest.KeepAlive = false;
webRequest.Timeout = 1000000000;
webRequest.ReadWriteTimeout = 1000000000;
using (HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse())
{
Stream k = webResponse.GetResponseStream();
MemoryStream ms = new MemoryStream();
int count = 0;
do
{
byte[] buf = new byte[1024];
count = k.Read(buf, 0, 1024);
ms.Write(buf, 0, count);
} while (k.CanRead && count > 0);
return ms;
}
}
catch (WebException e)
{
return null;
}
Can you guys give solution for serializing the Alternative views so that i can able to send and Receive MSMQ !
I do not think that you should take this approach.
MSMQ is designed to be ligtwheight, so sending huge data like images was not the intention - although this is technically possible, of course.
Also bear in mind that MSQM has a limit of 4 MB per message. Depending on the size of your images this might become problematic (or might not).
Instead I suggest that you save the images to a place that can be accessed by all participating application/ services/ etc., e.g. a network share, file server, or web server, or ...
Then you send only the URIs in your MSMQ message. This will be very fast to process on both sender and receiver side. Also, this will be much, much lighter on the MSMQ infrastructure.
I have trouble with webbrowser or may be ftp. I am uploading a picture and when I navigate the webbrowser it shows me the old photo, yet the picture im uploading gets to the ftp and gets overwrite. Here is the code:
webBrowser1.Refresh(WebBrowserRefreshOption.Completely);
webBrowser1.Navigate("www.google.com");
openFileDialog1.ShowDialog();
string filename = Path.GetFullPath(openFileDialog1.FileName);
FileInfo toUpload = new FileInfo(#"upload.jpg");
FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://fingercube.co.cc/public_html/objimg/" + toUpload.Name);
request.Method = WebRequestMethods.Ftp.UploadFile;
request.Credentials = new NetworkCredential("username", "pass");
Stream ftpStream = request.GetRequestStream();
FileStream file = File.OpenRead(filename);
int lenght = 2;
byte[] buffer = new byte[lenght];
int bytesRead = 0;
do
{
bytesRead = file.Read(buffer, 0, lenght);
ftpStream.Write(buffer, 0, bytesRead);
}
while (bytesRead != 0);
file.Close();
ftpStream.Close();
webBrowser1.Navigate("http://fingercube.co.cc/objimg/"+toUpload.Name);
It shows me the old photo everytime, but the photo is uploaded every time. :(
If the caching suggestion doesn't work try doing the following.
this.webBrowser1.Navigate("about:blank");
HtmlDocument doc = this.wbbFinalise.Document;
doc.Write(string.Empty);
Then navigate to your ftp location.
I had a similar issue while trying to refresh a locally generated HTTP page in the web browser and this fixed the issue.
The image is cached to IE cache. You must clear the cache before refreshing the control. Have a look here: http://www.gutgames.com/post/Clearing-the-Cache-of-a-WebBrowser-Control.aspx
Also, a related question on SO: WebBrowser control caching issue
got the solution .. the problem was with the cache easy solution to it was to make new request everytime .
I have a windows form application in which i am using a background worker to ftp upload files. After uploading 209 files successfully it gave error on file which only had size of 7.8kb that While Processing Img1.jpg Unable to write data to the transport connection. An existing connection was forcibly closed by the remote host.
string uri1;
ftpInfoUpload = LoadHostedSiteData(hs);
ftpInfoUpload[5] = imgRow["Filename"].ToString();
uri1 = String.Format("ftp://{0}/{1}/images/{2}", ftpInfoUpload[1], ftpInfoUpload[2], ftpInfoUpload[5]);
requestUpload = (FtpWebRequest)WebRequest.Create(uri1);
requestUpload.UsePassive = false;
requestUpload.UseBinary = true;
requestUpload.Method = WebRequestMethods.Ftp.UploadFile;
requestUpload.Credentials = new NetworkCredential(ftpInfoUpload[3], ftpInfoUpload[4]);
requestUpload.ContentLength = memStream.Length;
byte[] buff = new byte[bufferSize];
int contentLen;
// Stream to which the file to be upload is written
Stream strm = requestUpload.GetRequestStream();
memStream.Seek(0, SeekOrigin.Begin);
contentLen = memStream.Read(buff, 0, bufferSize);
// Till Stream content ends
while (contentLen > 0)
{
// Write Content from the file stream to the FTP Upload Stream
strm.Write(buff, 0, contentLen);
contentLen = memStream.Read(buff, 0, bufferSize);
}
//Close the file stream and the Request Stream
strm.Close();
strm.Dispose();
ftpStream.Close();
memStream.Close();
//responseUpload.Close();
responseDownload.Close();
And ideas whats happening?
I have set ftprequest.KeepAlive=true & set ftprequest.ConnectionGroupName = "Some Value", so that underlying code does not have to new create connection which have the same ftp server. I found this solution here. I also found this helpful. Also make sure not to create a new NetworkCredential object everytime you transfer a file that can cause exception. I have tested my code twice transferring 300 files and seems to work perfectly and quick. Setting KeepAlive=false can make transfers slow
I have following problem:
I'm using C# ftp methods to automate sending files to FTP servers. The program is constantly running, checking predefined directories. If it finds files in a directory, it is supposed to upload them to a defined ftp server. There are about 80 directories and most of the time the program has something to do. Everything works fine, except from the following scenario:
The file is being uploaded.
An error occurs while uploading: the remote host is down, the quota is exceeded or something similar.
An attempt is made to upload the file once again and the error occurs once again.
An attempt is made to upload file for the third, fourth, ... time and then this line of code:
Stream requestStream = ftpRequest.GetRequestStream();
throws WebException with status: Undefined, and description: the time limit for this operation has been reached (or something similar - I had to translate this from polish). While this WebException is being thrown, ftp request to other servers (files from different directories) succeed, so it looks like there is only problem with connection to this one ftp server.
It all results in no further attempt to upload file being successful. Uploading this file through other ftp client is, however, possible. When I restart the program, everything runs smoothly. Should I somehow manually release ftp resource? I'm using KeepAlive property of FtpWebRequest set to true..
I would very appreciate if someone could shed some light on this problem.
Edited:
I followed David's hint and found the place where I didn't call Close() method on the requestStream, I fixed it, but the problem reoccurred.
I will paste some code then. Here is part of the method, that uploads the file to the server. If it fails, another attempt is being made, something like:
while (retryCounter++ < retryNumber)
{
//upload file,
//if succeeded, break
}
inside while-block there is:
FtpWebRequest ftpRequest = (FtpWebRequest)WebRequest.Create(remoteFileName);
ftpRequest.UseBinary = true;
ftpRequest.UsePassive = true;
ftpRequest.Credentials = new NetworkCredential(UserName, Password);
ftpRequest.ReadWriteTimeout = SendTimeout;
ftpRequest.Timeout = ConnectTimeout;
ftpRequest.KeepAlive = true;
ftpRequest.Proxy = null;
ftpRequest.Method = WebRequestMethods.Ftp.UploadFile;
Stream requestStream = null;
try
{
using (MemoryStream fileStream = new MemoryStream(localFile))
{
byte[] buffer = new byte[BufferSize];
int readCount = fileStream.Read(buffer, 0, BufferSize);
int bytesSentCounter = 0;
while (readCount > 0)
{
requestStream.Write(buffer, 0, readCount);
bytesSentCounter += readCount;
readCount = fileStream.Read(buffer, 0, BufferSize);
System.Threading.Thread.Sleep(100);
}
}
requestStream.Close();
requestStream = null;
FtpWebResponse response = (FtpWebResponse)ftpRequest.GetResponse();
FtpStatusCode code = response.StatusCode;
string description = response.StatusDescription;
response.Close();
_logger.Information("Upload file result : status code {0}, status description {1}", code, description);
if (code == FtpStatusCode.ClosingData)
{
_logger.Information("File {0} uploaded successfully", localFileName);
}
else
{
_logger.Error("Uploading file {0} did not succeed. Status code is {1}, description {2}", localFileName, code, description);
}
}
catch (WebException ex)
{
if (requestStream != null)
requestStream.Close();
ftpRequest.Abort();
FtpStatusCode code = ((FtpWebResponse)ex.Response).StatusCode;
string description = ((FtpWebResponse)ex.Response).StatusDescription;
_logger.Error("A connection to the ftp server could not be established. Status code: {0}, description: {1} Exception: {2}. Retrying...", code, description, ex.ToString());
}
So, again the scenario is following:
1. The file is being uploaded and System.Net.Sockets.SocketException occurrs.
2. Another attempt is being made and System.Net.Sockets.SocketException reoccurs.
3. Always before I call the uploadfile method, I check whether the remote server is up and everything is ok by trying to list the remote directory. And from now, by calling
ftpRequest.GetResponse() (where ftpRequest.Method is WebRequestMethods.Ftp.ListDirectory) I get WebException with status: Undefined, and description: the time limit for this operation has been reached. When I restart the application, the problem disappears.
I can find no other place where the streams are not released properly, and have no idea what to do next..
Are you sure you're closing the stream correctly and thus release all underlying resources properly?
If not, this might be the cause of your problem, when you hit a limit (e.g. open sockets) after repeated failures.
You need to ensure you put the Stream.Close(), Request.Close() and Response.Close() in a finally block otherwise they will be skipped whenever there is an error. Keep in mind when dealing with external resources such as over a network, you need to program to expect errors since they will certainly crop up at some point.
try
{
processing here...
}
finally
{
requestStream.Close();
requestStream = null;
}
One possible source of error that I've run into is creating a new "NetworkCredential" over and over again. Then you'll get an exception after awhile.