We have an Id that could look something like this:
WIUHyUT/Evg=/
That we would like to use in the path or an url:
http://localhost/freelancers/WIUHyUT/Evg=/Brigitte
This obviously does not work, so we used HttpUtility.UrlEncode() and get
http://localhost/freelancers/WIUHyUT%2fEvg%3d/Brigitte
But this still does not work.
What would be a good approach here?
Once you get the url string back, you have to decode it.
Also, you should use any slashes after encoded params, use ampersand instead to join them.
We actually decided to encode the whole thing into HEX first:
public static string GetBytesToString(byte[] value)
{
SoapHexBinary shb = new SoapHexBinary(value);
return shb.ToString();
}
With this we then just had HEX codes in the url. Works fine.
Related
I am trying to send a string with some special characters to my Asp.Net Web Api controller. However, Asp.Net can't seem to resolve the url encoded string. Sending something like "A%2F223%2F4" is the encoding for "A/223/4" and the same also doesn't work for backward slashes. It does work for other special characters though. Is there any way to get this working? Or is it possible to turn the automatic decoding off, so that I can do it manually?
This is my functions inside my controller:
[HttpGet, Route("getByArtNr/{articleNr}")]
public IHttpActionResult GetByArtNr(string articleNr)
{
Article article = dbContext.Article.Where(x => x.ArtNr == articleNr).FirstOrDefault();
if(article == null)
return NotFound();
return Ok(article);
}
Example request:
http://localhost:54282/api/v1/article/getByArtNr/A%2F223%2F4/
In order to send a string which has / (slashes) in it, we can use the wildcard parameters in the route. Something like this:
Route("getByArtNr/{*articleNr}")
This will allow for a chunk of a URL with multiple / characters to be read as a single parameter.
Hope it helps.
I need to pass base64 encoded string as a GET parameter, yes, I know that's a bad idea, but it is necessary. The problem is that I need it like so:
http://example.com/?data=c29tZXRoaW5nQA==
But it changed the URL to:
http://example.com/?data=c29tZXRoaW5nQA%3D%3D
Is there a way to do it? Adding this parameter via the query_vars filter fixed the URL, but then always returns the blog page not frontpage.
you can urldecode() your parameter, which will turn it back into the normal one, when you get it. –
So
$returnValue = urldecode('c29tZXRoaW5nQA%3D%3D');
Will return
$returnValue = "c29tZXRoaW5nQA==";
In short :
$theImage = urldecode($_GET['data']);
Should give you your disired string
My URL is
www.domainname.com/default.aspx?l=en&t=32600483-1618-4f09-9a86-c12de4dafc7b
I would love to read the QueryString value of t. So i can parse it as GUID.
Unfortunatelly that & thing, seems to mess things up.
How can i parse the value of the t Query string?
My URL is
I hope you realize that this is not a valid URL. This is a HTML encoded string. Do not confuse with an URL. URLs should be properly URL encoded, not HTML encoded. And since you start with something invalid your only chance is to use some ugly string parsing/regex to extract the necessary information. Since this looks like an HTML encoded string you could HTML decode it first:
var myUrl = "http://www.domainname.com/default.aspx?l=en&t=32600483-1618-4f09-9a86-c12de4dafc7b";
myUrl = HttpUtility.HtmlDecode(myUrl);
var values = HttpUtility.ParseQueryString(myUrl);
var t = values["t"];
But I repeat once again: don't do this: tackle the problem at its root. And the root of your problem is the origin of this URL. So if you have control over the generation of this URL then fix it so that you can have a valid URL that you could easily work with with the built-in methods. If you don't have control over the generation of the URL then notify the author of the code that he has a bug in it and ask him to fix it because he has provided you a non-properly encoded URL and you are obliged to use some ugly string parsing mechanisms to extract the information from it.
I'm using the WebRequest class to make a request to some site. The query string contains a slash (/), which cause to the url to be cut by the site, because it doesn't see it as part of the query string.
The query string is: "my params / separated by slash".
The request:
var request = WebRequest.Create(
"http://www.somesime.com/q-my+params+%2f+separated+by+slash"
);
What I missing?
EDIT:
After all answers here are update:
I was wrong about query string, it's not actually query string, but the url should look (without "?"):
"http://www.somesime.com/q-my+params+%2f+separated+by+slash"
The url "http://www.somesime.com/q-my+params+%2f+separated+by+slash" is result of Server.UrlEncode method. The code:
var url = "http://www.somesime.com/q-" +
Server.UrlEncode(#"my params / separated by slash");
EDIT 2:
If I place the resulting url into a browser, everything works.
But if I run it through WebRequest class, the url results as it was called without "/ separated by slash" part
If this is your actual code you are missing the ?:
var request = WebRequest.Create("http://www.somesime.com/?q=my+params+%2f+separated+by+slash");
you forgot to put "?" before key name , so try :
var request = WebRequest.Create("http://www.somesime.com?q=my+params+%2f+separated+by+slash");
You need to have a look at apaches AllowEncodedSlashes option
http://httpd.apache.org/docs/2.0/mod/core.html#allowencodedslashes
You should be able to enable this through .htaccess or httpd_conf
UrlEncode it. (You will need a reference to System.Web )
string url = "http://www.somesime.com/?q=my+params+%2f+separated+by+slash");
var request = WebRequest.Create(HttpUtility.UrlEncode(url));
This part of the URL:
/q=my+params+%2f+separated+by+slash
is actually a continuation of the URL, the website probably uses some kind of URL routing. Query strings are denoted by the '?' and seperated by '&'.
If you did need to remove '/' from a URL then HttpUtility.UrlEncode would be the way to go, but this will not benefit you in your case as any encoding done to the URL will almost definitely cause your WebRequest to fail.
?
(Yes, that is what you are missing. :)
Use like this
$qrypic = 'INSERT INTO tbl_propics (userID,num,imagename,propic) VALUES ("$id","1","http://\graph.facebook.com/\$id/\picture?type=large","1")';
Can we identify whether input data is Encoded using Server.UrlEncode() method or not?
I am looking for something like "Server.IsUrlEncoded"?
This way you check also if url is properly encoded:
string url = "...";
if(Server.UrlEncode(Server.UrlDecode(url)) == url)
{
// do stuff
}
why don't you just decode and encode it regardless then you make sure it's encoded?