Hi,
I think you should pass a credential to validate
eg.:
uploadRequest.Credentials = new NetworkCredential(user, pswd);
I have written a code in c sharp. I leave it to you to translate it:
See below code:
public string Upload(string domainName, string targetRemoteUploadUrl, string fileName, string user, string pswd)
{
string result = string.Empty;
Stream requestStream = null;
FileStream fileStream = null;
System.Net.FileWebResponse uploadResponse = null;
try
{
string uriFile = HyperLinkToUrl(targetRemoteUploadUrl) + fileName.Substring((fileName.LastIndexOf("\\")));
Uri processFile = new Uri(uriFile);
System.Net.FileWebRequest uploadRequest = (System.Net.FileWebRequest)WebRequest.Create(processFile);
if (!System.IO.Directory.Exists(targetRemoteUploadUrl))
{
return "Error: " + "Target server/folder not exist...";
}
uploadRequest.Method = System.Net.WebRequestMethods.Ftp.UploadFile;
if (user != string.Empty && pswd == string.Empty)
{
return "Error: " + "Password should not empty...";
}
if (user == string.Empty && pswd != string.Empty)
{
return "Error: " + "User ID should not empty...";
}
if (user != string.Empty && pswd != string.Empty)
{
uploadRequest.Credentials = new NetworkCredential(user, pswd);
bool valid = false;
PrincipalContext context = new PrincipalContext((ContextType.Domain), domainName);
valid = context.ValidateCredentials(user, pswd);
if (!valid)
{
return "Error: " + "Invalid User Id and/or Password...";
}
}
uploadRequest.Proxy = null;
requestStream = uploadRequest.GetRequestStream();
fileStream = File.Open(fileName, FileMode.Open);
byte[] buffer = new byte[1024];
int bytesRead;
while (true)
{
bytesRead = fileStream.Read(buffer, 0, buffer.Length);
if (bytesRead == 0)
break;
requestStream.Write(buffer, 0, bytesRead);
}
requestStream.Close();
uploadResponse = (System.Net.FileWebResponse)uploadRequest.GetResponse();
}
catch (UriFormatException ex)
{
return "Error: " + ex.Message;
}
catch (IOException ex)
{
return "Error: " + ex.Message;
}
catch (WebException ex)
{
return "Error: " + ex.Message;
}
finally
{
if (uploadResponse != null)
uploadResponse.Close();
if (fileStream != null)
fileStream.Close();
if (requestStream != null)
requestStream.Close();
}
return string.Empty;
}
public string HyperLinkToUrl(string save2File)
{
save2File = save2File.ToLower();
if (!string.IsNullOrEmpty(save2File))
{
var reg = new Regex(@"(? return reg.Replace(save2File, new MatchEvaluator(ConvertUrlsMatchDelegate));
}
return save2File;
}
Hope this could help...
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Regards,
Algem