I have created a virtual directory with read and write access to everyone on Machine B. I am running web application 1 on Machine A. Now the requirement is to upload a file to remote http location from this web application. Machine A web application i is configured with anonymous authentication.
i am not able to implement the above requirement successfully. Please suggest whether this approach is correct or not?
public override bool UploadFile()
{
byte[] postData;
try
{
postData = this.FileData;
using (WebClient client = new WebClient())
{
client.Credentials = CredentialCache.DefaultCredentials;
client.Headers.Add("Content-Type", "application/x-www-form-urlencoded");
client.UploadData(this.UrlString, "PUT", postData);
}
return true;
}
catch (Exception ex)
{
throw new Exception("Failed to upload", ex.InnerException);
}
}
i have tried the example given in the below location
http://code.msdn.microsoft.com/CSASPNETRemoteUploadAndDown-a80b7cb5
unfortunately i could not able to make it work. I am using IIS7 on both machine A & B
Please suggest.
Related
I use an javascript picker to get file from my google drive, it's work well and i get then download url and acces_token from my drive
I would like to download bytes array from this file from my server, then i path the url and acces_token to it (with ajax), no problem
on server i would get data with this code (it's worked !!!)
public void DownloadFile(string url, string AccessToken)
{
try
{
byte[] BB;
using (WebClient wc = new WebClient())
{
wc.Headers.Add("Authorization", "Bearer " + AccessToken);
BB = wc.DownloadData(url);
}
}
catch (Exception ex)
{
throw ex;
}
}
now i get an 403 error ??
url are like "https://content.googleapis.com/drive/v2/files.....?key=mykey....."
what's changed on google server ?
thanks
If you want to download a file using the Drive API and setting the access token in the header, you have to do it by calling this url:
https://www.googleapis.com/drive/v2/files/[file-Id]?alt=media
As it's told in the Response section in the Files: get endpoint. It's really important the url parameter alt=media in order to make it work. Don't use an API Key.
I am using a web client class in my source code for downloading a string using http.
This was working fine. However, the clients in the company are all connected now to a proxy server. And the problem started from this.
When I have tested my application I don't think it can pass through the proxy server, as the exception that keeps getting thrown is "no response from xxx.xxx.xxx.xxx which is the proxy server IP address.
However, I can still navigate to the web site URL and it displays the string correctly in the browser when connecting through a proxy server, but not when I use my web client.
Is there something in the web client that I have to configure to allow me to access the url from behind a proxy server?
using (WebClient wc = new WebClient())
{
string strURL = "http://xxxxxxxxxxxxxxxxxxxxxxxx";
//Download only when the webclient is not busy.
if (!wc.IsBusy)
{
string rtn_msg = string.Empty;
try
{
rtn_msg = wc.DownloadString(new Uri(strURL));
return rtn_msg;
}
catch (WebException ex)
{
Console.Write(ex.Message);
return false;
}
catch (Exception ex)
{
Console.Write(ex.Message);
return false;
}
}
else
{
System.Windows.Forms.MessageBox.Show("Busy please try again");
return false;
}
}
My solution:
WebClient client = new WebClient();
WebProxy wp = new WebProxy(" proxy server url here");
client.Proxy = wp;
string str = client.DownloadString("http://www.google.com");
If you need to authenticate to the proxy, you need to set UseDefaultCredentials to false, and set the proxy Credentials.
WebProxy proxy = new WebProxy();
proxy.Address = new Uri("mywebproxyserver.com");
proxy.Credentials = new NetworkCredential("usernameHere", "pa****rdHere"); //These can be replaced by user input
proxy.UseDefaultCredentials = false;
proxy.BypassProxyOnLocal = false; //still use the proxy for local addresses
WebClient client = new WebClient();
client.Proxy = proxy;
string doc = client.DownloadString("http://www.google.com/");
If all you need is a simple proxy, you skip most of the lines above though. All you need is:
WebProxy proxy = new WebProxy("mywebproxyserver.com");
The answer proposed by Jonathan is proper, but requires that you specify the proxy credentials and url in the code. Usually, it is better to allow usage of the credentials as setup in the system by default (Users typically configure LAN Settings anyway in case they use a proxy)...
The below answer has been provided by Davide in earlier answer, but that requires modifying the app.config files. This solution is probably more useful since it does the same thing IN CODE.
In order to let the application use the default proxy settings as used in the user's system, one can use the following code:
IWebProxy wp = WebRequest.DefaultWebProxy;
wp.Credentials = CredentialCache.DefaultCredentials;
wc.Proxy = wp;
This will allow the application code to use the proxy (with logged-in credentials and default proxy url settings)... No headaches! :)
Hope this helps future viewers of this page to solve their problem!
I've encountered the same issue but using a webclient for downloading a file from the internet with a Winform application the solution was adding in the app.config:
<system.net>
<defaultProxy useDefaultCredentials="true" />
</system.net>
The same solution will work for an asp.net app inserting the same rows in web.config.
Hope it will help.
You need to configure the proxy in the WebClient object.
See the WebClient.Proxy property:
http://msdn.microsoft.com/en-us/library/system.net.webclient.proxy(VS.80).aspx
byte[] data;
using (WebClient client = new WebClient())
{
ICredentials cred;
cred = new NetworkCredential("xmen#test.com", "mybestpassword");
client.Proxy = new WebProxy("192.168.0.1",8000);
client.Credentials = cred;
string myurl="http://mytestsite.com/source.jpg";
data = client.DownloadData(myUrl);
}
File.WriteAllBytes(#"c:\images\target.jpg", data);
All previous answers have some merit, but the actual answer only needs ONE line:
wc.Proxy = new WebProxy("127.0.0.1", 8888);
where wc is the WebClient object, and 8888 is the port number of the proxy server located on the same machine.
I'm trying to develop a function that downloads a file in a FTP directory. I try this way
static void descargarFic(string ficFTP, string user, string pass, string dirLocal)
{
Library.WriteErrorLog(ficFTP + " -> " + dirLocal);
try
{
WebClient client = new WebClient();
client.Credentials = new NetworkCredential(user, pass);
client.DownloadFile(ficFTP, dirLocal);
Library.WriteErrorLog("Done");
client.Dispose();
}
catch(Exception e)
{
Library.WriteErrorLog(e.Message);
}
}
The WriteErrorLog write on a file the string passed. So, I'm calling that function in this way:
descargarFic("ftp://something-here/incoming/file.xml.gz", "anonymous", "password", #"C:\folder1\file.xml.gz");
But I receive the exception Exception during a WebClient request for a FTP download when I try to download it.
I'm pretty sure that it's some kind of problem with the URI, but I didn't realize where it is.
EDIT: Using Library.WriteErrorLog(e.InnerException);
The exception given is
The given access path's format is not supported.
I have a code like this in my local exe to upload a file in a certain url:
private static void SaveTToWeb()
{
try {
WebClient client = new WebClient();
client.Credentials = CredentialCache.DefaultCredentials;
client.UploadFile("www.something.com/uploadreceiver.aspx", "POST", "text.txt");
client.Dispose();
myFile = null;
urlForUpload = null;
}
catch (Exception err) { Console.write("error: " + err.Message(); }
}
my question is, in the server side "uploadreceiver.aspx" page, what code should i use to actually receive the file? thank you!
The CSASPNETRemoteUploadAndDownload sample shows how to upload files to and download files from remote server in an ASP.NET application.
You can download the sample code or read more here http://code.msdn.microsoft.com/CSASPNETRemoteUploadAndDown-a80b7cb5
I am using a web client class in my source code for downloading a string using http.
This was working fine. However, the clients in the company are all connected now to a proxy server. And the problem started from this.
When I have tested my application I don't think it can pass through the proxy server, as the exception that keeps getting thrown is "no response from xxx.xxx.xxx.xxx which is the proxy server IP address.
However, I can still navigate to the web site URL and it displays the string correctly in the browser when connecting through a proxy server, but not when I use my web client.
Is there something in the web client that I have to configure to allow me to access the url from behind a proxy server?
using (WebClient wc = new WebClient())
{
string strURL = "http://xxxxxxxxxxxxxxxxxxxxxxxx";
//Download only when the webclient is not busy.
if (!wc.IsBusy)
{
string rtn_msg = string.Empty;
try
{
rtn_msg = wc.DownloadString(new Uri(strURL));
return rtn_msg;
}
catch (WebException ex)
{
Console.Write(ex.Message);
return false;
}
catch (Exception ex)
{
Console.Write(ex.Message);
return false;
}
}
else
{
System.Windows.Forms.MessageBox.Show("Busy please try again");
return false;
}
}
My solution:
WebClient client = new WebClient();
WebProxy wp = new WebProxy(" proxy server url here");
client.Proxy = wp;
string str = client.DownloadString("http://www.google.com");
If you need to authenticate to the proxy, you need to set UseDefaultCredentials to false, and set the proxy Credentials.
WebProxy proxy = new WebProxy();
proxy.Address = new Uri("mywebproxyserver.com");
proxy.Credentials = new NetworkCredential("usernameHere", "pa****rdHere"); //These can be replaced by user input
proxy.UseDefaultCredentials = false;
proxy.BypassProxyOnLocal = false; //still use the proxy for local addresses
WebClient client = new WebClient();
client.Proxy = proxy;
string doc = client.DownloadString("http://www.google.com/");
If all you need is a simple proxy, you skip most of the lines above though. All you need is:
WebProxy proxy = new WebProxy("mywebproxyserver.com");
The answer proposed by Jonathan is proper, but requires that you specify the proxy credentials and url in the code. Usually, it is better to allow usage of the credentials as setup in the system by default (Users typically configure LAN Settings anyway in case they use a proxy)...
The below answer has been provided by Davide in earlier answer, but that requires modifying the app.config files. This solution is probably more useful since it does the same thing IN CODE.
In order to let the application use the default proxy settings as used in the user's system, one can use the following code:
IWebProxy wp = WebRequest.DefaultWebProxy;
wp.Credentials = CredentialCache.DefaultCredentials;
wc.Proxy = wp;
This will allow the application code to use the proxy (with logged-in credentials and default proxy url settings)... No headaches! :)
Hope this helps future viewers of this page to solve their problem!
I've encountered the same issue but using a webclient for downloading a file from the internet with a Winform application the solution was adding in the app.config:
<system.net>
<defaultProxy useDefaultCredentials="true" />
</system.net>
The same solution will work for an asp.net app inserting the same rows in web.config.
Hope it will help.
You need to configure the proxy in the WebClient object.
See the WebClient.Proxy property:
http://msdn.microsoft.com/en-us/library/system.net.webclient.proxy(VS.80).aspx
byte[] data;
using (WebClient client = new WebClient())
{
ICredentials cred;
cred = new NetworkCredential("xmen#test.com", "mybestpassword");
client.Proxy = new WebProxy("192.168.0.1",8000);
client.Credentials = cred;
string myurl="http://mytestsite.com/source.jpg";
data = client.DownloadData(myUrl);
}
File.WriteAllBytes(#"c:\images\target.jpg", data);
All previous answers have some merit, but the actual answer only needs ONE line:
wc.Proxy = new WebProxy("127.0.0.1", 8888);
where wc is the WebClient object, and 8888 is the port number of the proxy server located on the same machine.