Invalid Uri: invalid port specified C# - c#

I recently created a FTP Server locally with FileZilla Server. With the admin port 14147 by default. Then with the FileZilla Client I got connected with no problems.
When I try to connect by chrome or ie with
ftp://(ip)
or
ftp://(full machine name)
or
ftp://(user:password)#(machine).
Everything is ok.
My admin interface setting port is 14147.
My listing port by default is 21
No problems at all with Internet browsers even with telnet.
Now I created an application in C# and when I use the Uri class
Uri target = new Uri(strUri);
I got an exception error: Invalid Uri: invalid port specified
I saved the ftp address in a database like this and I tried many variations but nothing happens:
ftp://user:password#FullMachineNameWithDomain
ftp://user:password#FullMachineNameWithDomain:21
ftp://user:password#FullMachineNameWithDomain:14147
What am I doing wrong?

AFAIK Username and password are not supposed to be part of the URL.
You should pass them separately as credentials when you're using something like FtpWebRequest class. E.g.
request.Credentials = new NetworkCredential(username, password);

Related

Modify an attribute value in Active Directory via ldaps

I executed the code as below to modify an attribute value in Active Directory via ldaps.It worked properly.In addition, I found the packets were encrypted when I analyzed the packets captured by tcpdump via WireShark.
using (DirectoryEntry entry = new DirectoryEntry("LDAP://192.168.109.4:636/OU=People,DC=dev,DC=com", "dev\\user", "password"))
{
entry.Properties["description"].Value = "a new description";
entry.CommitChanges();
entry.Close();
}
However, I have one question.I guess that the statement below is requried to encrypt packets via ldaps.
entry.AuthenticationType = AuthenticationTypes.SecureSocketsLayer;
In this case, it worked well without the statement as above.
Does anyone know the reason?
Did you see the SSL handshake when the connection opened? Usually SSL won't even work when you're accessing it with an IP address. It can also encrypt using Kerberos, which will work on port 389 using an IP address, although you usually have to specify AuthenticationTypes.Sealing for that.
However, it does know that port 636 is the LDAPS port, so if you specify port 636, it will automatically do the SSL handshake.
You can also exclude the port and specify AuthenticationTypes.SecureSocketsLayer, and it will automatically connect via port 636:
new DirectoryEntry(
"LDAP://dev.com/OU=People,DC=dev,DC=com",
"dev\\user", "password",
AuthenticationTypes.Secure | AuthenticationTypes.SecureSocketsLayer
)
AuthenticationType is Secure by default.
SSL is a server-side configuration so the LDAP Server Admin must have enabled SSL.

Why is WebClient DownloadData working on IIS Express but not on IIS?

I have the following piece of code that is getting some data from a url.
WebClient webClient = new WebClient();
byte[] imageBytes = webClient.DownloadData(url);
The url variable is just a string but didn't work even if I make it a Uri.
While this is working fine when I'm running the site using IIS Express, when I load it via IIS the webClient.DownloadData(url) call is throwing an exception with message: "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 IP:port", where IP and port are the values for the ip and port of the url I'm trying to access.
Also, if I go directly to that url using a browser it works fine. I turned the firewall completely off in case it was blocking the response from the external site but didn't make any difference.
Thanks

Re-writing the Hosts file does not update the next IP connection

We have a written a C# application that communicates with any one of a group of IP in the cloud.
Any one of which may not be working. We use the URL of the address as the IIS server is expecting a Host Header Name in order to route to the correct application interface.
So we set the Hosts file to point the URL at an IP.
We then send a command at the URL to get back the server time.
This tells us the connection is working.
If we don't get a response we assume the connection is dead. We then write a new IP from a list into the Hosts file and we try again.
This is where we hit a bug. The application doesn't seem to see the Hosts file has changed and uses the old (bad) IP.
There is no caching built into the application so we are assuming that Windows is caching for us.
We've tried to flush caches with:
ipconfig /flushdns
arp -d *
nbtstat -R
We still get the same problem.
Any thoughts on how to clear the cache?
If you can't address this at the server end (e.g. a load balancer, etc), then just use the IP address list in your code:
var req = HttpWebRequest.Create("http://" + IPAdd.ToString() + "/path_to_query_time");
((HttpWebRequest)req).Host = "yourhostheaderhere";
var resp = req.GetResponse();
//If things have gone wrong here, change IPAdd to the next IP address and start over.
Don't go messing with the users settings to try and solve a problem in your application that's of your own making.

IP address v/s machine name

My server name : server, my server ip is 192.168.10.200
SPSite site = new SPSite("192.168.10.200:7000") throws the following exception:
The Web application at
http://192.168.10.200:7000 could not
be found. Verify that you have typed
the URL correctly. If the URL should
be serving existing content, the
system administrator may need to add a
new request URL mapping to the
intended application
.
while
SPSite site = new SPSite("server:7000") works perfectly.
any ideas?
OS: Windows 7 64 bits + Sharepoint 2010
Your server is set up for serving more than one website and uses a Host header in the http request to sort out which one a request is for.
If you give it the IP address, it doesn't know which web server is meant.
The site could be configured with a host header for server and thus only answering on that and not on IP.
One clue is that your code says the port number is 7000 but the error message says it is 8000. Is that a typo in your actual code, or did you make a mistake in transcribing the code or message into your Question?
Also, the IP addresses don't match.

System.Net.WebRequest not respecting hosts file

Is there a way to get a System.Net.WebRequest or System.Net.WebClient to respect the hosts or lmhosts file?
For example: in my hosts file I have:
10.0.0.1 www.bing.com
When I try to load Bing in a browser (both IE and FF) it fails to load as expected.
Dns.GetHostAddresses("www.bing.com")[0]; // 10.0.0.1
WebRequest.Create("http://10.0.0.1").GetResponse(); // throws exception (expected)
WebRequest.Create("http://www.bing.com/").GetResponse(); // unexpectedly succeeds
Similarly:
WebClient wc = new WebClient();
wc.DownloadString("http://www.bing.com"); //succeeds
Why would System.Net.Dns respect the hosts file but System.Net.WebRequest ignore it? What do I need to change to make the WebRequest respect the hosts file?
Additional Info:
If I disable IPv6 and set my IPv4 DNS Server to 127.0.0.1, the above code works (fails) as expected. However if I add my normal DNS servers back as alternates, the unexpected behavior resumes.
I've reproduced this on 3 Win7 and 2 Vista boxes. The only constant is my company's network.
I'm using .NET 3.5 SP1 and VS2008
Edit
Per #Richard Beier's suggestion, I tried out System.Net tracing. With tracing ON the WebRequest fails as it should. However as soon as I turn tracing OFF the behavior reverts to the unexpected success. I have reproduced this on the same machines as before in both debug and release mode.
Edit 2
This turned out to be the company proxy giving us issues. Our solution was a custom proxy config script for our test machines that had "bing.com" point to DIRECT instead of the default proxy.
I think that #Hans Passant has spotted the issue here. It looks like you have a proxy setup in IE.
Dns.GetHostAddresses("www.bing.com")[0]; // 10.0.0.1
This works because you are asking the OS to get the IP addresses for www.bing.com
WebRequest.Create("http://www.bing.com/").GetResponse(); // unexpectedly succeeds
This works because you are asking the framework to fetch a path from a server name. The framework uses the same engine and settings that IE frontend uses and hence if your company has specified by a GPO that you use a company proxy server, it is that proxy server that resolves the IP address for www.bing.com rather than you.
WebRequest.Create("http://10.0.0.1").GetResponse(); // throws exception (expected)
This works/fails because you have asked the framework to fetch you a webpage from a specific server (by IP). Even if you do have a proxy set, this proxy will still not be able to connect to this IP address.
I hope that this helps.
Jonathan
I'm using VS 2010 on Windows 7, and I can't reproduce this. I made the same hosts-file change and ran the following code:
Console.WriteLine(Dns.GetHostAddresses("www.bing.com")[0]); // 10.0.0.1
var response = WebRequest.Create("http://www.bing.com/").GetResponse(); // * * *
Console.WriteLine(new StreamReader(response.GetResponseStream()).ReadToEnd());
I got an exception on the line marked "* * *". Here's the exception detail:
System.Net.WebException was unhandled
Message=Unable to connect to the remote server
Source=System
StackTrace:
at System.Net.HttpWebRequest.GetResponse()
at ConsoleApplication2.Program.Main(String[] args) in c:\Data\Projects\ConsoleApplication2\ConsoleApplication2\Program.cs:line 17
InnerException: System.Net.Sockets.SocketException
Message=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 10.0.0.1:80
Source=System
ErrorCode=10060
Maybe it's an issue with an earlier .NET version, that's now fixed in .NET 4 / VS 2010? Which version of .NET are you using?
I also found this thread from 2007, where someone else ran into the same problem. There are some good suggestions there, including the following:
Turn on system.net tracing
Work around the problem by using Dns.GetHostAddresses() to resolve it to an IP. Then put the IP in the URL - e.g. "http://10.0.0.1/". That may not be an option for you though.
In the above thread, mariyaatanasova_msft also says: "HttpWebRequest uses Dns.GetHostEntry to resolve the host, so you may get a different result from Dns.GetHostAddresses".
You should overwrite the default proxy.
HttpWebRequest & WebRequest will set a default proxy if present in Internet Explorer and your file hosts will be bypassed.
request.Proxy = new WebProxy();
The following is just an example of code:
try
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("www.bing.com");
request.Proxy = new WebProxy();
request.Method = "POST";
request.AllowAutoRedirect = false;
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
if (response.StatusCode == HttpStatusCode.OK)
{
//some code here
}
}
catch (exception e)
{
//Some other code here
}

Categories

Resources