Url works when pinging but causes UriFormatException - c#

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.

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).

Related

Hortonworks webhdfs i try to list all folder it will work on Hortonworks console using curl command but not in C#

I try to list the name of the folder using webhdfs in C#. URL working fine using curl in sandbox but not in C# in my laptop
Error Message-
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
Code:
static void Main(string[] args)
{
Uri myUri = new Uri("http://hostname:50070/webhdfs/v1/user/hive/warehouse");
string userName = "myuser";
WebHDFSClient hdfsClient = new WebHDFSClient(myUri, userName);
string strDirectoryPath= "/user/hive/warehouse";
ArrayList l = new ArrayList();
l.Add(hdfsClient.GetDirectoryStatus(
strDirectoryPath).Result.Directories);
}
few issues:
your URI is "http://hostname:50070/webhdfs/v1/user/hive/warehouse", and your strDirectoryPath= "/user/hive/warehouse" - so your duplicating the path to be "http://hostname:50070/webhdfs/v1/user/hive/warehouse/user/hive/warehouse" which does not exist
you user "myuser" may not have permissions access to /user/hive/warehouse - check its permissions by "hdfs dfs -ls /user/hive/warehouse"
(just checking) the 'hostname' in the URI - is just for the question and not in the actual code, right? you'll need the hostname/ip of the sandbox

SftpClient using Url instead of IP as host

I am using the following code to upload a file to an sFTP server. I have done a test upload using FileZilla and the file is uploaded successfully.
try
{
var client = new SftpClient(host, port, username, password);
client.Connect();
client.ChangeDirectory(workingDir);
var listDirectory = client.ListDirectory(workingDir);
foreach (var file in files)
{
var fileStream = new FileStream(file, FileMode.OpenOrCreate);
client.BufferSize = 4 * 1024; // bypass payload error large files
client.UploadFile(fileStream, Path.GetFileName(file));
Log.Info(string.Format("File [{0}] upload complete",file));
}
}
catch (Exception ex)
{
Log.Error(ex.Message);
}
But in the code above, I cannot seem to get the upload done because I get the following error:
No such host is known
And that's maybe because I am using the Url value of my host instead of IP? (I have done a test with another FtP server using IP and that also seems to be working) I'm wondering if that's the case? If so, is there a way to let SftpClient (Ssh.Net) handle the Url?
The host parameter may be an IP address or a host name that can be resolved to an IP address. It may not be a URL, that is something completely different from a technical point of view.
The fact that some full application accepts something else than a host name says little: it splits that string into separat tokens and connects to the IP address it gets returned when resolving the host name. But that does not mean that you can open any network connection to a URL or some arbitrary string. It is only possible to open a connection to an IP address.
So if you successfully test some URL like sftp://ftp.example.com/ContactImport in some "program", this does not mean that you can internally use that string as host parameter in your code. You need to use the host name that is a part of such URL, so ftp.example.com in this case, since only that can be successfully resolved to an IP address.

Use SignalR with IP Address instead of computer name

I have a SignalR service setup to run self hosted in a Windows Service.
When I enter this address:
http://mycomputer.mydomain.net:8789/signalr/signalr/negotiate
I get some xml (for negotiation) displayed in the browser (showing that hit the service correctly.)
But if I enter this address (that has mycomputer.mydomain.net's ip address in place of the computer name):
http://10.92.15.6:8789/signalr/signalr/negotiate
or this
http://localhost:8789/signalr/signalr/negotiate
I get this error:
Bad Request - Invalid Hostname
HTTP Error 400. The request hostname is invalid.
I have tried also using:
http://10.92.15.6.mydomain.net:8789/signalr/signalr/negotiate
But I just get:
This site can’t be reached
10.92.15.6.mydomain.net’s server DNS address could not be found.
Is there someway to be able to make SignalR work using an IP Address instead of the computer name?
I had to change my startup from this:
string url = "http://" + machineName + ".mydomain.net:8789";
server = WebApp.Start<Startup>(url);
to this:
string url = "http://*:8789";
var startOptions = new StartOptions(url);
server = WebApp.Start<Startup>(startOptions);

UriBuilder points to localhost instead of domain name

My site has a public section that is accessed by http and a https part to requires logging in. When logging out of the site, it redirects to the http public index page.
Previously I had done this with stating the full url to point too. Recently I had to get rid of such things so the site can be run on numerous domains, for testing.
I tried using UriBuilder to convert https links to http link so that a website no longer has to use direct pointing to a specific url. This should allow the site to use any domain name. Right now it points to the computer name.
if (Request.IsSecureConnection)
{
UriBuilder ub = new UriBuilder();
ub.Path = "/html/index.html";
ub.Scheme = Uri.UriSchemeHttp;
ub.Port = -1; // use default port for scheme
Response.Redirect(ub.Uri.ToString(), true);
//An old direct link to the site
//Response.Redirect("http://www.someaddress.com/html/index.html");
}
When the code is triggered remotely on the test server instead of pointing to the right domain it returns me to the address
http://localhost/html/index.html
Instead of
http://testserver/html/index.html
I have no idea why it is doing this instead of returning the address I am connecting to the server via.
If you don't specify host than default host ("localhost") will be used - see UriBuilder() constructor article on MSDN.
Fix: specify host (probably based on incoming request's host).
ub.Host = GetMeIncomingHost();
Because in the URI to which you are redirecting, you haven't specified an authority (host). Thus, your redirect sends a 302 Found HTTP status and the response contains a location: header that looks like something like this:
location: /html/index.html
That is a relative URI, relative to the current URI from which the redirected request originated. That means it inherited the scheme and authority component of the requesting page (which, obviously, in your case, was an http://localhost:xx/....
To fix this, seed your UriBuilder in its constructor with HttpContext.Current.Request.Url. That should about do it:
UriBuilder ub = new UriBuilder( HttpContext.Current.Request.Url );
ub.Path = "/html/index.html";
Response.Redirect(ub.Uri.ToString(), true);

How do I force a relative URI to use https?

I have a relative URI:
Uri U = new Uri("../Services/Authenticated/VariationsService.svc",
UriKind.Relative);
The problem is that depending on whether the user has typed https:// or http:// into their web browser to get to the silverlight application, it may use either http or https when trying to contact the service.
I want to force the program to use https for connecting to the service eitherway.
Initially I tried this:
Uri U = new Uri("../Services/Authenticated/VariationsService.svc",
UriKind.Relative);
string NU = U.AbsoluteUri;
U = new Uri(NU.Replace("http://", "https://"), UriKind.Absolute);
But it fails at U.AbsoluteUri because it can't convert the relative Uri into an absolute Uri at that stage. So how do I change the Uri Scheme to https?
The relative path has to be converted to absolute first. I do that using the Uri of the excuting Silverlight XAP file.
There might be ways to reduce this a bit (it feels wrong doing string operations with Uris) but it's a start:
// Get the executing XAP Uri
var appUri = App.Current.Host.Source;
// Remove the XAP filename
var appPath = appUri.AbsolutePath.Substring(0, appUri.AbsolutePath.LastIndexOf('/'));
// Construct the required absolute path
var rootPath = string.Format("https://{0}{1}", appUri.DnsSafeHost, appUri.AbsolutePath);
// Make the relative target Uri absolute (relative to the target Uri)
var uri = new Uri(new Uri(rootPath), "../Services/Authenticated/VariationsService.svc");
This does not include transferring a portnumber (which you might want to do in other circumstance). Personally I would put the above code in a helper method that also handles the port (and whatever you want to do differently when running localhost).
Hope this helps.
Instead, you should change your ASPX file which hosts your silverlight, and force user to redirect to SSL only if he/she is logged in using non SSL url. Because ideally it would be perfect if silverlight opens connection only to the same domain and scheme it loaded from.
The protocol is a separate component, so I think that you can just put it in front of your relative address:
Uri U = new Uri("https:../Services/Authenticated/VariationsService.svc", UriKind.Relative);
"Scheme" does not have any meaning in a relative URI. You'll have to convert it to an absolute URI at some point to change the scheme.

Categories