I have written a code to hit a particular URL and download data using a WebClient, and have set a proxy which I have purchased from MyPrivateProxies.net to this webclient with credentials and other needed parameters through which it hits the mentioned URL.
But I get this exception every time at the last line.
System.Net.WebException: Unable to connect to the remote server
System.Net.Sockets.SocketException: A connection attempt failed
because the connected party did not properly respond after a period of
time, or established connection failed because connected host has
failed to respond
below is my snippet of code:
WebClient client = new WebClient();
WebProxy webProxy = new WebProxy("my_proxy_with_port");
NetworkCredential netcredit = new NetworkCredential(username, password, domain);
client.Credentials = netcredit;
webProxy.Credentials = netcredit;
webProxy.UseDefaultCredentials = false;
webProxy.BypassProxyOnLocal = false;
client.Proxy = webProxy;
client.Encoding = Encoding.UTF8;
String strURL = "url_to_be_hit";
String jsonString = client.DownloadString(strURL);
Do I need some other settings on my windows 7 system or any config changes in app.config?
Are there other configuration for a console app to use proxies?
I think you just need to assign the proxy credentials to the WebProxy object, not to the WebClient.
I dont know myprivateproxies.net but you have to make sure that you are using a http proxy, not a socks proxy or something else.
got it resolved wasn't my code issue or URI points or any of my strings which I was passing...the port was not opened at my end on local system
Related
(Related to: FTP directory listing returned as HTML instead of simple Linux ls output)
How can I force a C# program (FtpWebRequest) to use a direct IP to get onto the Internet instead of going through an HTTP proxy? (My knowledge of IT networks and related terminology is scant. Apologies in advance.)
To break out from the internal company network there is
Indirect way via the IP of a (HTTP) proxy server.
Direct IP of an internet service (not a proxy server).
I know this because, when using FileZilla with / without the proxy set in Internet Explorer, then on the remote FTP server, the logs either show the IP of the proxy, or the direct IP.
Code using C# FtpWebRequest to connect to FTP server outside of the company.
FtpWebRequest request = WebRequest.Create(uri) as FtpWebRequest;
request.Method = WebRequestMethods.Ftp.ListDirectory;
request.Credentials = server.Credential;
request.KeepAlive = true;
request.UsePassive = true;
request.EnableSsl = false;
//proxy options
//1.
// do nothing
//2.
request.Proxy = null;
//3. setup HTTP proxy
request.Proxy = new WebProxy(proxyuri, true);
request.Proxy.Credentials = System.Net.CredentialCache.DefaultCredentials;
request.Proxy.Credentials = new NetworkCredential("Username", "Password");
Stream responseStream = response.GetResponseStream();
StreamReader reader = new StreamReader(responseStream);
while (!reader.EndOfStream)
{ ... }
When I set the proxy, then the C# program (obviously) uses the proxy server to get to the FTP server. However because this is an HTTP proxy, the directory listing is returned as HTML and deleting, uploading, and making directories is not possible. The program needs to delete files, create folders on the FTP.
Similarly, when not setting the proxy, then the program uses the settings in Internet Explorer, where the proxy is set, which then again uses the HTTP proxy.
In the code, setting the proxy to null (request.Proxy = null OR WebRequest.DefaultWebProxy OR GlobalProxySelection.GetEmptyWebProxy() OR new WebProxy(); ) causes an exception "The remote server returned an error: (550) File unavailable (e.g., file not found, no access)."
This problem only occurs for one particular external FTP server (that happens to be running vsftpd). I tested using a different external FTP and both the proxy and the non proxy / direct connection work.
Questions
1. It seems the C# program can only breakout using the proxy. Why, when the proxy is not set, the program does not use the direct IP?
2. How can I force the program to use the direct IP?
3. Could the problem be due to the FTP server (vsftpd)?
The windows hosts file (%systemroot%\system32\drivers\etc) had erroneous entries. I am not exactly sure which entry was the problem but the file now only contains one line
127.0.0.1 localhost
I try to establish a FTP connection using the FtpWebRequest. I need to establish the connection through a squid proxy server, that requires me to send a CONNECT command first.
I have tried the code below, but this results in a GET request to the proxy server, which it refuses.
FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://" +server);
request.Credentials = new NetworkCredential(user, pwd);
var webproxy = new WebProxy(new Uri("http://" + proxy+ ":" + proxyPort));
webproxy.UseDefaultCredentials = true;
request.Proxy = webproxy;
request.UsePassive = true;
request.Method = WebRequestMethods.Ftp.ListDirectory;
FtpWebResponse response = (FtpWebResponse)request.GetResponse();
Is there any way that I can use the FtpWebRequest to establish my connection through the proxy?
If not, do you know a good and hopefully free .NET FTP client that I can use?
Thank you very much for your help.
FtpWebRequest is a "session-less" client, so it doesn't allow you to interact with the remote end while connected. I examined the code of FtpWebRequest.cs and I don't see how you can get it to send a "CONNECT" command.
"CONNECT" command doesn't seem to be part of the standard FTP command set, so I can't wrap my brain how this fits into something that an FTP library should do.
A good free FTP client that is session-oriented that I use is "SSH.NET Library" at http://sshnet.codeplex.com/ but I believe it only supports sftp. FtpWebRequest supports ftps and ftp. Find out if your FTP server supports sftp and you might be able to use SSH.NET . If your only option is ftps, I can report that I spent a long time looking for a free .net ftps client and I came away using FtpWebRequest.
FtpWebRequest supports HTTP proxy (and CONNECT command is mandatory part of that support). Your code is correct.
Though the HTTP proxy is not supported uploads (it is for all other operations). So maybe your actual problem is the upload, while you do not mention it.
For FTP uploads through HTTP proxy you have to use a 3rd party FTP client.
For example with WinSCP .NET assembly, you can use:
// Setup session options
SessionOptions sessionOptions = new SessionOptions
{
Protocol = Protocol.Ftp,
HostName = "example.com",
UserName = "user",
Password = "mypassword",
};
// Configure proxy
sessionOptions.AddRawSettings("ProxyMethod", "3"); // HTTP proxy
sessionOptions.AddRawSettings("ProxyHost", "proxy");
using (Session session = new Session())
{
// Connect
session.Open(sessionOptions);
// Upload file
string localFilePath = #"C:\path\file.txt";
string pathUpload = "/file.txt";
session.PutFiles(localFilePath, pathUpload).Check();
}
For the options for the SessionOptions.AddRawSettings, see raw settings.
Easier is to have WinSCP GUI generate C# FTP code template for you.
Note that WinSCP .NET assembly is not a native .NET library. It's rather a thin .NET wrapper over a console application.
(I'm the author of WinSCP)
I am using the following code which is working on local machine, but when i tried the same code on server it throws me error
A connection attempt failed because the connected party did not properly respond after a period of time, or established connection
failed because connected host has failed to respond
Here is my code:
WebClient client = new WebClient();
// Add a user agent header in case the
// requested URI contains a query.
//client.Headers.Add ("ID", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)");
Stream data = client.OpenRead("http://" + Request.ServerVariables["HTTP_HOST"] + Request.ApplicationPath + "/PageDetails.aspx?ModuleID=" + ID);
StreamReader reader = new StreamReader(data);
string s = reader.ReadToEnd();
Console.WriteLine(s);
data.Close();
reader.Close();
I am getting error on
Stream data = client.OpenRead("http://" + Request.ServerVariables["HTTP_HOST"] + Request.ApplicationPath + "/PageDetails.aspx?ModuleID=" + ID);
is it due any firewall setting?
I had a similar problem and had to convert the URL from string to Uri object using:
Uri myUri = new Uri(URLInStringFormat, UriKind.Absolute);
(URLInStringFormat is your URL)
Try to connect using the Uri instead of the string as:
WebClient client = new WebClient();
client.OpenRead(myUri);
setting the proxy address explicitly in web.config solved my problem
<system.net>
<defaultProxy>
<proxy usesystemdefault = "false" proxyaddress="http://address:port" bypassonlocal="false"/>
</defaultProxy>
</system.net>
Resolving the “TCP error code 10060: A connection attempt failed…” while consuming a web service
I know this ticket is old, but I just ran into this issue and I thought I would post what was happening to me and how I resolved it:
In my service I was calling there was a call to another web service. Like a goof, I forgot to make sure that the DNS settings were correct when I published the web service, thus my web service, when published, was trying to call from api.myproductionserver.local, rather than api.myproductionserver.com. It was the backend web service that was causing the timeout.
Anyways, I thought I would pass this along.
I know this post was posted 5 years ago, but I had this problem recently. It may be cause by corporate network limitations. So my solution is letting WebClient go through proxy server to make the call. Here is the code which worked for me. Hope it helps.
using (WebClient client = new WebClient())
{
client.Encoding = Encoding.UTF8;
WebProxy proxy = new WebProxy("your proxy host IP", port);
client.Proxy = proxy;
string sourceUrl = "xxxxxx";
try
{
using (Stream stream = client.OpenRead(new Uri(noaaSourceUrl)))
{
//......
}
}
catch (Exception ex)
{
throw;
}
}
Adding the following block of code in web.config solves my problem
<system.net>
<defaultProxy enabled="false" >
</defaultProxy>
</system.net>
In my case I got this error because my domain was not listed in Hosts file on Server.
If in future anyone else is facing the same issue, try making entry in Host file and check.
Path : C:\Windows\System32\drivers\etc
FileName: hosts
Is the URL that this code is making accessible in the browser?
http://" + Request.ServerVariables["HTTP_HOST"] + Request.ApplicationPath + "/PageDetails.aspx?ModuleID=" + ID
First thing you need to verify is that the URL you are making is correct. Then check in the browser to see if it is browsing. then use Fiddler tool to check what is passing over the network. It may be that URL that is being called through code is wrongly escaped.
Then check for firewall related issues.
I had this problem. Code worked fine when running locally but not when on server.
Using psPing (https://technet.microsoft.com/en-us/sysinternals/psping.aspx) I realised the applications port wasn't returning anything.
Turned out to be a firewall issue. I hadn't enabled my applications port in the Windows Firewall.
Administrative Tools > Windows Firewall with Advanced Security
added my applications port to the Inbound Rules and it started working.
Somehow the application port number had got changed, so took a while to figure out what was going on - so thought I'd share this possibility in case it saves someone else time...
I have resolved this below issue
A connection attempt failed because the connected party did not
properly respond after a period of time, or established connection
failed because connected host has failed to respond
Solution
We need to configured proxy setting in code. my scenario using Web.config file
Added Proxy address as below
<system.net>
<defaultProxy>
<proxy proxyaddress="http://XXXXX:XXXX" bypassonlocal="True"/>
</defaultProxy>
</system.net>
Also added proxy credential using below code NetworkCredential - I have used my local credential here.
HttpWebRequest webReq = (HttpWebRequest)WebRequest.Create(url);
webReq.Credentials = new NetworkCredential("XXXX", "XXXXX");
webReq.Proxy.Credentials = new NetworkCredential("XXXX", "XXXX");
It works for me!
First Possibility: The encrypted string in the Related Web.config File should be same as entered in the connection string (which is shown above)
And also, when you change anything in the "Registry Editor" or regedit.exe (as written at Run), then after any change, close the registry editor and reset your Internet Information Services by typing IISRESET at Run. And then login to your environment.
Type Services.msc on run and check:
Status of ASP.NET State Services is started. If not, then right click on it, through Properties, change its Startup type to automatic.
Iris ReportManager Service of that particular bank is Listed as Started or not. If its Running, It will show "IRIS REPORT MANAGER SERVICE" as started in the list. If not, then run it by clicking IRIS.REPORTMANAGER.EXE
Then Again RESET IIS
I know it's an old post but I came across the exact same issue and I managed to use this by turning off MALWAREBYTES program which was causing the issue.
It might be issue by proxy settings in server. You can try by disabling proxy setting,
<defaultProxy enabled="false" />
Íf above solutions don't work for your case. May be your request drop by firewall. Check firewall settings.
We have a background operation (Window service) that we want to use through a proxy server.
Basically, we're doing this:
public WebClient GetWebClient(){
var webClient = new WebClient();
webClient.proxy = new WebProxy(Configuration.ProxyHost, Configuration.ProxyPort);
// add a bunch of headers to the WebClient (sessionids, etc.)
return webClient;
}
The proxy is one that we have configured ourselves using FreeProxy.
I've enabled logging and on the machine I'm testing with, and can confirm that requests are being made to the proxy when using it in Firefox.
No authentication is required for the proxy server, except that the IP has to be within our office (which from the Firefox evidence, I assume is not the problem).
However, within our background process, I don't seem to be using the proxy when I use the webclient:
using(var wc = GetWebClient())
using(var s = wc.OpenRead("someurl"))
using(var sr = new StreamReader(s)){
return sr.ReadToEnd();
}
I receive no errors from the proxy however, it seems like we're just going along without it even though the proxy has explicitly been set.
The information seems to return fine, just not through our proxy.
Is there something I'm missing when using a proxy with a WebClient?
edit: more details. If we disable the proxy service on the server, then we get an exception that we can't connect. So it seems like the webclient is attempting to reach out to the proxy, but that traffic is not actually flowing through the proxy.
Inner Exception: SocketException: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond
It turns out that FreeProxy wasn't accepting HTTPS traffic.
I guess the proxy must return the types of traffic it can route, and if it cannot, the webclient does nothing.
Switched to using the Burp suite as our proxy since it can accept HTTPS.
http://portswigger.net/burp/
You either have to configure windows for using the proxy by default, or set the proxy manually in your code, see: http://msdn.microsoft.com/en-us/library/system.net.webclient.proxy(v=VS.100).aspx
You are using the WebClient class correctly as far as I can tell. I am able to see the following request...
using(var client = new WebClient())
{
client.Proxy = new WebProxy("localhost", 8888);
Console.WriteLine(client.DownloadString("http://www.google.com"));
}
in Fiddler running on my local box. Now if I shut Fiddler down, I get the WebException:
Unable to connect to the remote server
With an inner SocketException of:
No connection could be made because the target machine actively refused it 127.0.0.1:8888
So, with that said, my guess is that your proxy is working as intened but it is just not logging the outgoing HTTP request.
I have an ssl/tls server (nodejs) that acts as a proxy to postfix/sendmail to perform some pre-processing/data aquisition on outgoing mail.
From C#, I can manually connect and authenticate with the following code:
var sslStream = new SslStream(tcpClient.GetStream(), false,
new RemoteCertificateValidationCallback(CertificateValidation),
new LocalCertificateSelectionCallback(CertificateSelectionCallback));
string fn = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "cert.pem");
X509Certificate c = X509Certificate.CreateFromCertFile(fn);
var certs = new X509CertificateCollection();
certs.Add(c);
sslStream.AuthenticateAsClient(System.Environment.MachineName, certs , SslProtocols.Default, false);
However, I can not get the SmtpClient to connect. The top level error is a timeout, but I have debugged into SmtpClient/SmtpConnection and the underlying error is that the stream is not readable, presumably because it never authenticated (I cant hit a break-point in my ssl/tls proxy server with the SmtpClient code, but the manual sslConnection above works just fine).
It would be great if there was a way to manually supply the underlying communication stream to SmtpClient but I cant find a way to do it.
Anyone have an idea as to why the code below wont authenticate?
Here is the test app I have been using to try and connect with SmtpClient without success:
ServicePointManager.ServerCertificateValidationCallback = CertificateValidation;
// Using Ssl3 rather than Tls here makes no difference
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls;
var client = new SmtpClient {
Timeout = 10000,
DeliveryMethod = SmtpDeliveryMethod.Network,
EnableSsl = true,
UseDefaultCredentials = false,
Host = "192.168.15.71",
Port = 10126
};
client.ClientCertificates.Add(c);
client.Credentials = new NetworkCredential("username", "password");
//times out here, except the real exception that doesn't bubble up is the stream
//isnt readable because it never authenticated (when it trys to read the status
//returned by the smtp server, eg: "220 smtp2.example.com ESMTP Postfix")
client.send(email);
Solved a while back, forgot to post the answer.
The .NET smtp client, along with most smtp client libraries, does not support initiating communications via ssl/tls. It requires that the smtp server support initiating communications unencrypted, and then transitioning to an encrypted connection using the "upgrade" command (this is almost always handled behind the scenes by the SMTP client library).
At the time, I was connecting to a semi-custom smtp server that was proxying postfix, and this custom solution only supported connecting initially via ssl/tls. Have since started using Haraka for my SMTP server, which supports all SMTP standards as well as providing me with the plugin capability I needed.