Use UriBuilder and construct httpRequest - c#

I try to build the following uri
http://localhost:8080/TestService.svc/RunTest
I do it as following
var uriBuilder = new UriBuilder();
uriBuilder.Host = "localhost:8080/TestService.svc";
uriBuilder.Path = String.Format("/{0}", "RunTest");
string address = uriBuilder.ToString()
//In debugger the address looks like http://[http://localhost:8080/TestService.svc]/RunTest
HttpWebRequest request = (HttpWebRequest) WebRequest.Create(address);
The above generates an exception
Invalid URI: The hostname could not be parsed.
I`ll appreciate your help in solving the issue

When using the Uri builder you need to put the host, port & path as it's own line. Also TestService.svc is also part of the path and not the host, you can get away with it if not using port but with port they have to be separated out.
var uriBuilder = new UriBuilder();
uriBuilder.Host = "localhost";
uriBuilder.Port = 8080;
uriBuilder.Path = String.Format("/{0}/{1}", "TestService.svc", "RunTest");
var address = uriBuilder.ToString();

When I run your code, I also see square brackets as the value for the address variable as you point out, but I don't see PerfTestService in the generated Uri, nor do I see why this would be?! I see:
http://[localhost:8080/TestService.svc]/RunTest
Since you already know the host and the path, I suggest you construct it as a string.
var uriBuilder = new UriBuilder("http://localhost:8080/TestService.svc/RunTest");
string address = uriBuilder.ToString();
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(address);

Related

Unable to build uri with ":" in host address for async api call

I am attempting to make a put call using HttpClient.PutAsync to an external api with an address that contains a ":" in the host name, e.g. "https://xx-xx-xxx:000/orders/create".
If I pass the url string directly into HttpClient.PutAsync(apiUrl, apiContent) or build a Uri to pass into it, the host name is truncated at the ":", e.g."https://xx-xx-xxxx/orders/create" . If I try forcing the ":" into the Uri using UriBuilder, it throws an exception of: "Invalid URI: The hostname could not be parsed."
Do I need to build a custom UriParser class to do this? If so, I am unsure of how to go about this and where to register it.
HttpResponseMessage response = new HttpResponseMessage();
try
{
//this causes exception
UriBuilder builder = new UriBuilder();
builder.Scheme = "https";
builder.Host = "xx-xx-xx00:000";
builder.Path = "/orders/create";
Uri apiUri = builder.Uri;
//this gets truncated if passed into PutAsync
string apiUrl = "https://xx-xx-xx00:000/orders/create";
//this also truncates
Uri uri = new Uri(apiUrl);
HttpClientHandler handler = new HttpClientHandler();
handler.ServerCertificateCustomValidationCallback = delegate { return true; };
HttpClient client = new HttpClient(handler);
client.DefaultRequestHeaders.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", encodedUidPwd);
response = await client.PutAsync(apiUri, apiContent);
}
EDIT: Apparently this is just for a 3 digit port number, ie "https://xx-xx-xxx:0000/orders/create" does not truncate. However, I am fairly certain the port I'm trying to access has 3 digits
Try setting the port number on the UriBuilder's Port property rather than putting it in the Host property. More information here.

How to get IPaddess from given String

I have String like this:
http://192.168.xx.xx/abc/abcd.php
and want to fetch only ipaddress from it.
The Expected output should be:
192.168.xx.xx
I can do this by splitting it by '/' but is there any easy way out.
Here, give this a try:
var url = "http://192.168.1.1/abc/abcd.php";
Uri uri = new Uri(url);
var ip = Dns.GetHostAddresses(uri.Host)[0];
Console.WriteLine(ip.ToString());
You will need System.Net for this.

C# add scheme to URI

I'm trying to add the HTTP Protocol to this URI "example:8888"
What I've done :
var uriBuilder = new UriBuilder("example:8888")
{
Scheme = Uri.UriSchemeHttp,
};
var uri = uriBuilder.Uri;
The Output is
http:0.0.34.184
What I'm doing wrong :S ?
Your string is being parsed as the URL path, not a hostname.
To force it to parse as a hostname, you need to add a scheme to the string.

Uri constructor with dontEscape is obsolete, what is alternatieve?

My question is regarding passing an URL to HttpWebRequest without escaping, I searched the forums and internet, but I didn't find a good solution for it.
I have following URL:string URL= www.website.com/sub/redirec\t\bs\dd
So when I create an uri like this:
Uri uri = new Uri(URL);
HttpWebRequest request = (HttpWebRequest) WebRequest.Create(uri);
In this case on a get method I will get following URL:www.website.com/sub/redirect%5Ct%5Cbc%5Cdd
This sign "\" will be replaced by "%5C". What is crucial for me not to happen?
I can avoid that by:
Uri uri = new Uri(URL, true); //bool dontEscape
But this constructor is obsolete. How to have same effect without using obsolete?
use this
Uri uri = new Uri(Uri.EscapeUriString(URL));

.NET URI: How can I change ONE part of a URI?

Often I want to change just one part of a URI and get a new URI object back.
In my current dilemma, I want to append .nyud.net, to use the CoralCDN.
I have a fully qualified URI fullUri. How can I, in effect, do this:
fullUri.Host = fullUri.Host + ".nyud.net";
This needs to work for almost any URL, and the PORT of the request needs to be maintained.
Any help would be much appreciated.
You can use an UriBuilder to modify individual parts of an Uri:
Uri uri = new Uri("http://stackoverflow.com/questions/2163191/");
UriBuilder builder = new UriBuilder(uri);
builder.Host += ".nyud.net";
Uri result = builder.Uri;
// result is "http://stackoverflow.com.nyud.net/questions/2163191/"

Categories

Resources