How do I create a dot prefixed cookie uri? - c#

I am trying to pass a Uri of new Uri(".example.com")
Invalid URI: The format of the URI could not be determined.
or new Uri("http://.example.com")
Invalid URI: The hostname could not be parsed.
I need to be able to use the CookieContainer.SetCookies function which only has one overload taking a Uri.
According to this page, .NET 4.0 should support dot prefixed cookies now, but it seems the Uri class does not?

In this case, you need to pass a proper uri to the function, and the Uri parser is correctly rejecting the malformed string you are trying to use.
I would advise using the Cookie Constructor that takes 4 parameters - allowing you to set the domain to a dot-prefixed one.
Cookie(string name, string value, string path, string domain);

Related

Correct way of manipulating the url coming from App.config

I have a URL ("http://localhost:2477/") on which I do get and post request. I have stored this URL in the app.config file of my project.
In the code, depending on the function, I add the string "getValue?id={0}" or "postValue" to this URL. But I later ran into an issue when I changed the URL to "http://localhost:2477" (no forward slash in the end) in the app.config.
Took me some embarrassing amount of time to figure out this issue, which made me wonder if there is a good way to handle this case.
Irrespective of the case when there is a forward slash or not in the URL, I want my code to change it to a proper URL.
Always use Path.Combine(string, string). This method will conform a valid path and should add the / if needed.
edit
I realized my answer does not work for URL, just for file paths.
What you’re looking for is Uri constructor instead.
Uri baseUri = new Uri("http://www.contoso.com");
Uri myUri = new Uri(baseUri, "catalog/shownew.htm");
Using the Uri class you can modify your URL more elegantly. You can access the Host, Port, Query, etc. with ease. A similar question was asked here.
Try to use the UriBuilder, it's far more flexible as the Uri Constructor.
See https://stackoverflow.com/a/20164328/10574963

Uri validation in C#

I am trying to implement uri validation as follows.
For the sake of testing, I assigned my url as an empty string "", and tested the following code, but it returns me True.
I wonder what I am doing wrong/missing?
var a = "";
if (Uri.IsWellFormedUriString(a, UriKind.RelativeOrAbsolute))
{
Debug.WriteLine("True");
}
else
{
Debug.WriteLine("Error");
}
See what RFC2396 says about URIs:
4.2. Same-document References
A URI reference that does not contain a URI is a reference to the
current document. In other words, an empty URI reference within a
document is interpreted as a reference to the start of that document,
and a reference containing only a fragment identifier is a reference
to the identified fragment of that document. Traversal of such a
reference should not result in an additional retrieval action.
However, if the URI reference occurs in a context that is always
intended to result in a new request, as in the case of HTML's FORM
element, then an empty URI reference represents the base URI of the
current document and should be replaced by that URI when transformed
into a request.
Casually, see what MSDN says:
The string is considered to be well-formed in accordance with RFC 2396
and RFC 2732 by default. If International Resource Identifiers (IRIs)
or Internationalized Domain Name (IDN) parsing is enabled, the string
is considered to be well-formed in accordance with RFC 3986 and RFC
3987
So how to handle the empty URI scenario?
The answer is up to you. While an empty URI can be considered as valid, maybe in your scenario isn't valid. Thus, you need to add a condition in your if statement:
if (!string.IsNullOrEmpty(a) && Uri.IsWellFormedUriString(a, UriKind.RelativeOrAbsolute))
{
}
Empty string is a valid relative URI.

C# Uri class - stripping off a port number from Authority

I'm trying tu use System.Uri to strip off various information.
E.g. it gets me Uri.Authority.
When my URI is http://some.domain:52146/something, Uri.Authority.ToString() gives me "some.domain:52146".
I'd rather have "some.domain" and the port with a separate call.
Any ideas whow I could strip off the :port_number stuff most elegantly, either with a Uri-method I don't know of or with some string manipulation?
And getting back the http:// would also be useful (to know for example whether it's http or https).
Use Uri.Host and Uri.Port:
Uri uri = new Uri("http://some.domain:52146/something");
string host = uri.Host; // some.domain
int port = uri.Port; // 52146
Since I learnt about the properties Uri.Host and Uri.Port now I know that Uri.Scheme gives me the protocol.

Invalid URL as a Valid URL in C#

how can i add the invalid url (but its valid as it is internal URL) as a valid URL, i am getting an error when i am passing it to System.Uri();
Here is my Uri Code
new System.Uri("mailto:DFO%20ABNS%20Techn/DD-DWA/IND#ADW-NGP", true)
According to this http://www.ietf.org/rfc/rfc6068.txt / should be %-encoded in mailto 'address-part'. .Net will happily take:
new System.Uri("mailto:DFOTechn/DD-DWA/IND#ADW-NGP");
But it is all considered as part of the host.
encoding the '/' characters gives:
new System.Uri("mailto:DFO%20ABNS%20Techn%2FDD-DWA%2FIND#ADW-NGP")
Which .Net correctly parses with ADW-NGP as the host.

C# decodes URL containing %2F on path, is there any way to instruct API to send the URL as it is?

I am having a URL in below format
abcd.com/xyz/pqr%2Fss/abc
I want this to be send to server as it is.
When I build Uri using System.Uri it converts it to abcd.com/xyz/pqr/ss/abc
and it fails as I don't have a URL with the specified path.
When I tried with double encoding
(abcd.com/xyz/pqr%252Fss/abc) it send the Uri as it is but it fails as server side it is converted to (abcd.com/xyz/pqr%2Fss/abc)
If you construct your uri as such:
Uri u = new Uri("http://abcd.com/xyz/pqr%2Fss/abc")
Access the encoded string like this:
u.OriginalString
I had this problem too, but I found the solution: when you use HttpUtility.UrlEncode to be sure that the application will read the url right you have to construct the link this way:
http://www.abcd.com/xyz?val=pqr%2Fss
and not like this
http://www.abcd.com/xyz/pqr%2Fss
where pqr%2Fss is the result of the HttpUtility.UrlEncode("SOME STRING")

Categories