I want to upload a file securely from client machine to a webserver using C# client. It will be helpful to get some sample application of this. Also I want to know how can I achieve this with ssl certificate.
Thanks,
Abdul
WebClient webClient = new WebClient();
string webAddress = null;
try
{
webAddress = #"http://myCompany/ShareDoc/";
webClient.Credentials = CredentialCache.DefaultCredentials;
WebRequest serverRequest = WebRequest.Create(webAddress);
WebResponse serverResponse;
serverResponse = serverRequest.GetResponse();
serverResponse.Close();
webClient.UploadFile(webAddress + logFileName, "PUT", logFileName);
webClient.Dispose();
webClient = null;
}
catch (Exception error)
{
MessageBox.Show(error.Message);
}
Related
I am sending a request to third party website by PostAsJsonAsync as below:
var nbParamsJson = Newtonsoft.Json.JsonConvert.SerializeObject(nbParams);
var httpContent = new StringContent(nbParamsJson);
try
{
var response = await client.PostAsJsonAsync(siteUrl, httpContent);
}
catch (Exception exp)
{
}
which throws an exception: [The request was aborted: Could not create SSL/TLS secure channel.]
I know my website in test environment does not have a SSL certificate and probably is happening because the third-party website has SSL certificate.
I want to know is there any way to create SSL channel without moving to SSL.
I just added
System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
before PostAsJsonAsync and it solved my issue.
I'm not sure of your async, but here is an adaptation from working code to send JSON data to an SSL website that you should be able to adapt to your specific case:
var baseurl = "https://example.com/api/upload/123";
var rqst = (HttpWebRequest)WebRequest.Create(baseurl);
rqst.Method = "PUT";
rqst.Accept = "application/json";
var authInfo = "authtoken";
rqst.Headers["Authorization"] = "Token " + authInfo;
rqst.UserAgent = "curl/7.37.0";
rqst.ContentType = "application/json; charset=utf-8";
using (var streamWriter = new StreamWriter(rqst.GetRequestStream()))
{
streamWriter.Write(GetJsonData());
}
try
{
var resp = rqst.GetResponse();
using (var sr = new StreamReader(resp.GetResponseStream() ?? throw new InvalidOperationException()))
{
var txt = sr.ReadToEnd();
textBox1.Text = txt;
}
}
catch (Exception ex)
{
textBox1.Text = ex.Message;
}
I am trying to download a xml file from an url but I get this error:
"The remote server returned an error (401) unauthorized". Also, this webpage needs some credentials.
I searched on different topics before coming to ask you, but I didn't find a solution to this...
Here are two versions of codes I tried but didn't work:
try
{
WebClient wClient = new WebClient();
wClient.Credentials = new NetworkCredential(nni, pwd);
var dlString = wClient.DownloadString(url);
//Stream data = wClient.OpenRead(url);
//var reader = new XmlTextReader(wClient.OpenRead(url));
//doc.LoadXml();
doc.Load(wClient.OpenRead(url));
Console.WriteLine(doc.InnerText);
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
try
{
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(xmlUrl);
WebClient client = new WebClient();
client.Credentials = new NetworkCredential(nni, pwd);
string htmlCode = client.DownloadString(xmlUrl);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
Can you guys please help me on this?
Wrote a code to upload file to a https folder as below
WebClient webClient = new WebClient();
string webAddress = null;
try
{
webAddress = #"https://www.example.net/mydocs";
webClient.UseDefaultCredentials = true;
webClient.Credentials = CredentialCache.DefaultCredentials;
WebRequest serverRequest = WebRequest.Create(webAddress);
WebResponse serverResponse;
serverResponse = serverRequest.GetResponse();
serverResponse.Close();
webClient.UploadFile(webAddress , "PUT", #"C:\d\1.xml");
webClient.Dispose();
webClient = null;
}
catch (Exception error)
{
MessageBox.Show(error.Message);
}
the line webClient.UploadFile(webAddress , "PUT", #"C:\d\1.xml");
returning an error
The remote server returned an error: (405) Method Not Allowed.
Looks like the method PUT is not supported on the server. Make sure it's the correct method supported. You can try with POST
webClient.UploadFile(webAddress , "POST", #"C:\d\1.xml");
I am trying to open a url from my winforms application and i am getting "407 Proxy Authentication Required" error. I am able to open a sample application that was deployed on IIS in my machine. but if i try to access any other URL getting this error.
Here is the source code. Any suggestions please.
string url = "http://google.co.in";
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
HttpWebRequest.DefaultWebProxy.Credentials = CredentialCache.DefaultCredentials;
request.Method = "POST";
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Console.WriteLine(response.StatusDescription);
Stream dataStream = response.GetResponseStream();
StreamReader reader = new StreamReader(dataStream);
string responseFromServer = reader.ReadToEnd();
Console.WriteLine(responseFromServer);
MessageBox.Show(responseFromServer);
reader.Close();
dataStream.Close();
response.Close();
That would indicate that the proxy set in your system settings requires you to log in before you're able to use it. If it works in your browser, you've most likely done so in the past. Either disable the proxy in your system's network settings, or add the appropriate headers to authenticate against the proxy.
Try this, it worked for me
string url = "http://google.co.in";
IWebProxy proxy = new WebProxy("your proxy server address", port number ); // port number is of type integer
proxy.Credentials = new NetworkCredential("your user name", "your password");
try
{
WebClient client = new WebClient();
client.Proxy = proxy;
string resp = client.DownloadString(url);
// more processing code
}
}
}
catch (WebException ex)
{
MessageBox.Show(ex.ToString());
}
}
IWebProxy proxy = new WebProxy("proxy address", port number);
proxy.Credentials = new NetworkCredential("username", "password");
using (var webClient = new System.Net.WebClient())
{
webClient.Proxy = proxy;
webClient.DownloadString("url");
}
I want to upload file to a server. I wrote this function to upload the file to localhost server (I am using wamp server):
private void button1_Click_1(object sender, EventArgs e)
{
FileStream fstream = new FileStream(#"C:\Users\Albert\Documents\10050409_3276.doc", FileMode.OpenOrCreate);
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://localhost/upload_file");
request.Method = "PUT";
request.ContentLength = fstream.Length;
request.AllowWriteStreamBuffering = true;
Stream request_stream = request.GetRequestStream();
byte[] indata = new byte[1024];
int bytes_read = fstream.Read(indata, 0, indata.Length);
while (bytes_read > 0)
{
request_stream.Write(indata, 0, indata.Length);
bytes_read = fstream.Read(indata, 0, indata.Length);
}
fstream.Close();
request_stream.Close();
request.GetResponse();
MessageBox.Show("ok");
}
So when i click on the button the exception apper said that:
Additional information: The remote server returned an error: (405) Method Not Allowed.
I tried to use "POST" instead of "PUT" so the program works and the message box appears to say 'ok', but when i open the localhost->upload_file(folder) Ididn't find any files.
I tested my program with wamp server => the problem occured.
I tested my program with real server and put in the network credentials and tried to upload to folder that has (777) permission => the problem occured.
So where is the problem exactly?
Thanks :)
try with webClient
WebClient client = new WebClient();
byte[] bret = client.UploadFile(path, "POST", FilePath);
//path==URL
//FilePath==Your uploading file path
or
WebClient webClient = new WebClient();
string webAddress = null;
try
{
webAddress = #"http://localhost/upload_file/";
webClient.Credentials = CredentialCache.DefaultCredentials;
WebRequest serverRequest = WebRequest.Create(webAddress);
serverRequest.Credentials = CredentialCache.DefaultCredentials;
WebResponse serverResponse;
serverResponse = serverRequest.GetResponse();
serverResponse.Close();
webClient.UploadFile(path, "POST", FilePath);
webClient.Dispose();
webClient = null;
}
catch (Exception error)
{
MessageBox.Show(error.Message);
}
(code in or part i didn't tried )