Reputation: 24395
I have a mail server I'm trying to connect to with exchange web services. If I ping the server, it works, but it gets a UriFormatException
when provided in code.
Urls that work in command prompt but fail in c#
myserver.mydomain.com
myserver
192.168.100.1
(my server's ip)Urls that can be parsed into URIs but fail to be pinged
http://myserver.mydomain.com
http://192.168.100.1
I also tried adding \\
to the beginning but had no luck.
We do have a bit of a weird setup with connecting to our domain when on-network that I believe is what is causing http://myserver.mydomain.com
to fail in ping. How can I turn the base url (without the http://) into a string that will be valid for a c# Uri
?
Code:
var serverUrl = "myserver.mydomain.com"; //base string I'd like to use
_exchange.Url = new Uri(serverUrl); //causes UriFormatException: Invalid URI: The format of the URI could not be determined.
Upvotes: 0
Views: 311
Reputation: 100527
To consturct Uri from host name use UriBuilder
:
var builder = new UriBuilder();
builder.Host = "myserver.mydomain.com";
var uri = builder.Uri;
Note that what you call "uri" (myserver.mydomain.com) is actually "host name" or "DNS name" which is what get resolved to IP and than used to Ping. "http://myserver.mydomain.com" is absolute Uri for particular protocol (HTTP).
Upvotes: 1