Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Tuesday, November 22, 2011 4:10 PM
YO
im doing
WebClient client=new WebClient();
client.useDefaultCredentials=false;
client.Credentials=new NetworkCredential("username","password");
client.UploadFile("http://ip/dir/","POST",pathToFile);
im using http://www.sharing-file.com/download.htm
trial version (dont think that is the problem. but cant say for sure)
on the same pc
i created a folder and gave gusts upload access
and it worked
but when i created a user and gave him access to upload files (removed guest)
i get an exception
i also tried to connect to ssl
then i got an exception about the certificate
but could'nt find anything about that
All replies (6)
Thursday, November 24, 2011 1:52 PM ✅Answered | 1 vote
...
found a workaround
public static void SetBypassSslCertificateValidation()
{
ServicePointManager.ServerCertificateValidationCallback
+= new RemoteCertificateValidationCallback(BypassSslCertificateValidation);
}
private static bool BypassSslCertificateValidation(object sender, X509Certificate cert, X509Chain chain, SslPolicyErrors error)
{
return true;
}
but now the problem is that i am sending the uploaded file to asd.php
and it needs method = POST
but with that i get
The remote server returned an error: (401) Unauthorized.
if i use method = PUT
it goes through
but in asd.php
$_FILES
will not get any info
have to use
$asd = fopen("php://input", "r");
while ($data = fread($asd, 1024))
echo $data;
to see anything
and that wont work
i tried to do a html page with form method = POST
and uploaded the file succesfully
question:
why do i get 401 error?
and how can i fix it?
...
new thread
Thursday, November 24, 2011 3:30 PM ✅Answered | 2 votes
Hi,
It's all about authentication issue, any at first figure out what type of authentication your using, the windows authentication or form base authentication anyway try this link:
http://www.asp.net/mvc/tutorials/authenticating-users-with-windows-authentication-cs
Thanks
Md. Marufuzzaman Don't forget to click [Vote] / [Good Answer] on the post(s) that helped you.
I will not say I have failed 1000 times; I will say that I have discovered 1000 ways that can cause failure – Thomas Edison.
Tuesday, November 22, 2011 4:26 PM
Hi,
Can we see the exception message and the stack trace ?
Kind regards,
Tuesday, November 22, 2011 5:40 PM
dont have access to the code right now
but i tried to recreate it as accurate as possible
and its much slower on this pc (could be a difference)
the exceptions i get:
Exception: The underlying connection was closed: An unexpected error occurred on a receive.
Inner Exception: Unable to read data from the transport connection
stack trace:
at System.Net.WebClient.UploadFile(Uri address, String method, String fileName)
at System.Net.WebClient.UploadFile(String address, String fileName)
at ConsoleApplication1.Program.Main(String[] args) in c:\users\niklas\documents\visual studio 2010\Projects\ConsoleApplication1\ConsoleApplication1\Program.cs:line 22
when i use https instead of http
SSL exception:
The underlying connection was closed: Could not establish trust relationship for the SSL/TLS secure channel.
The remote certificate is invalid according to the validation procedure.
string[] files = Directory.GetFiles("C:\Users\Niklas\Desktop\files");
foreach (string file in files)
{
try
{
WebClient client = new WebClient();
client.UseDefaultCredentials = false;
client.Credentials = new NetworkCredential("asd", "asd");
client.UploadFile("http://ip/usertemp/", file);
client.Dispose();
}
catch(Exception e)
{
e.ToString();
}
}
Wednesday, November 23, 2011 8:18 AM
this is the exception i get with the real code
"An exception occurred during a WebClient request."
"Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host."
"An existing connection was forcibly closed by the remote host"
but the code does the same thing as my code above basicly
void uploadFiles(string path)
{
if(File.Exists(path))
{
try
{
WebClient client = new WebClient();
client.UseDefaultCredentials = false;
client.Credentials = new NetworkCredential("name", "pass");
byte[] bytes = client.UploadFile(uploadUrl, WebRequestMethods.Http.Post, path);
client.Dispose();
}
catch(Exception e)
{
e.ToString();
}
}
else if (Directory.Exists(path))
{
string[] dirs = Directory.GetDirectories(path);
string[] files = Directory.GetFiles(path);
foreach (string dir in dirs)
{
uploadFiles(dir);
}
foreach (string file in files)
{
uploadFiles(file);
}
}
}
public void startUpload()
{
string[] dirs = Directory.GetDirectories("Logs\");
dirs = Directory.GetDirectories(dirs[dirs.Length - 1]);
uploadFiles(dirs[dirs.Length - 1]);
}
Wednesday, November 23, 2011 1:25 PM
I have also tried this:
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Net;
using System.Security.Cryptography.X509Certificates;
namespace SendFilesToServer
{
class Program
{
static void Main(string[] args)
{
try
{
//doWebClientStuff();
doHttpRequestStuff();
}
catch (Exception e)
{
StreamWriter sw = new StreamWriter(File.OpenWrite(filePath));
sw.Write(e.ToString());
sw.Close();
}
}
static public void doHttpRequestStuff()
{
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create("https://ip/");
request.ClientCertificates.Add(new X509Certificate("www.sharing-file.crt"));
request.UseDefaultCredentials = false;
request.PreAuthenticate = true;
request.Credentials = new NetworkCredential("name", "pass");
StreamReader sr = new StreamReader(request.GetResponse().GetResponseStream());
StreamWriter sw = new StreamWriter(File.OpenWrite(filePath));
sw.Write(sr.ReadToEnd());
sr.Close();
sw.Close();
request.Abort();
}
static public void doWebClientStuff()
{
MyWebClient client = new MyWebClient();
string[] files = Directory.GetFiles("files");
client.UseDefaultCredentials = false;
client.Credentials = new NetworkCredential("name", "pass");
WebResponse wr = client.asd(new Uri("https://ip/APLog/")).GetResponse();
StreamReader sr = new StreamReader(wr.GetResponseStream());
//Console.WriteLine(sr.ReadToEnd());
//Console.Read();
foreach (string file in files)
{
client.UploadFile("https://ip/APLog/", WebRequestMethods.Http.Post, file);
}
client.Dispose();
}
}
class MyWebClient : WebClient
{
protected override WebRequest GetWebRequest(Uri address)
{
HttpWebRequest request = (HttpWebRequest)base.GetWebRequest(address);
request.ClientCertificates.Add(new X509Certificate("www.sharing-file.crt"));
return request;
}
public WebRequest asd(Uri addr)
{
return this.GetWebRequest(addr);
}
}
}
but i get this:
System.Net.WebException: The underlying connection was closed: Could not establish trust relationship for the SSL/TLS secure channel. > System.Security.Authentication.AuthenticationException: The remote certificate is invalid according to the validation procedure.
at System.Net.Security.SslState.StartSendAuthResetSignal(ProtocolToken message, AsyncProtocolRequest asyncRequest, Exception exception)
at System.Net.Security.SslState.ProcessReceivedBlob(Byte[] buffer, Int32 count, AsyncProtocolRequest asyncRequest)
at System.Net.Security.SslState.StartReceiveBlob(Byte[] buffer, AsyncProtocolRequest asyncRequest)
at System.Net.Security.SslState.ProcessReceivedBlob(Byte[] buffer, Int32 count, AsyncProtocolRequest asyncRequest)
at System.Net.Security.SslState.StartReceiveBlob(Byte[] buffer, AsyncProtocolRequest asyncRequest)
at System.Net.Security.SslState.ProcessReceivedBlob(Byte[] buffer, Int32 count, AsyncProtocolRequest asyncRequest)
at System.Net.Security.SslState.StartReceiveBlob(Byte[] buffer, AsyncProtocolRequest asyncRequest)
at System.Net.Security.SslState.ProcessReceivedBlob(Byte[] buffer, Int32 count, AsyncProtocolRequest asyncRequest)
at System.Net.Security.SslState.StartReceiveBlob(Byte[] buffer, AsyncProtocolRequest asyncRequest)
at System.Net.Security.SslState.ProcessReceivedBlob(Byte[] buffer, Int32 count, AsyncProtocolRequest asyncRequest)
at System.Net.Security.SslState.StartReceiveBlob(Byte[] buffer, AsyncProtocolRequest asyncRequest)
at System.Net.Security.SslState.ForceAuthentication(Boolean receiveFirst, Byte[] buffer, AsyncProtocolRequest asyncRequest)
at System.Net.Security.SslState.ProcessAuthentication(LazyAsyncResult lazyResult)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Net.TlsStream.ProcessAuthentication(LazyAsyncResult result)
at System.Net.TlsStream.Write(Byte[] buffer, Int32 offset, Int32 size)
at System.Net.PooledStream.Write(Byte[] buffer, Int32 offset, Int32 size)
at System.Net.ConnectStream.WriteHeaders(Boolean async)
End of inner exception stack trace
at System.Net.HttpWebRequest.GetResponse()
at SendFilesToServer.Program.doHttpRequestStuff() in C:\Users\praktik\Documents\Visual Studio 2008\Projects\SendFilesToServer\SendFilesToServer\Program.cs:line 39
at SendFilesToServer.Program.Main(String[] args) in C:\Users\praktik\Documents\Visual Studio 2008\Projects\SendFilesToServer\SendFilesToServer\Program.cs:line 18