I have a requirement where the id I use in #RequestMapping accept a value of the format 1234567812345678 and abcxyz/abcxy+z$ (basically a auto generated string with / forward slash in it).
I have tried a mapping of all the 3 below formats but nothing works:
#RequestMapping(value="abc/action/{id: .+[//]*.*}.{format}", method=RequestMethod.PUT) -- Accepts nothing.
#RequestMapping(value=" abc/action/{id:.+}.{format}", method=RequestMethod.PUT) -- Accepts everything except /
#RequestMapping(value=" abc/action/{id:.*}.{format}", method=RequestMethod.PUT) -- Accepts everything except /
However, when I try the same regex in a normal java program, it works like a charm :
package miscellenousTest;
public class RegExTest {
public static void main(String[] args) {
String s1 ="9876543298765432";
String s2 =" abcxyz/abcxy+z$";
System.out.println(s1.matches(".+[//]*.*"));
System.out.println(s2.matches(".+[//]*.*"));
}
}
I have gone through some of the previous posts but nothing gives me the exact solution I am looking for.
It will be great if someone can throw some pointers/solution at this one.
you should not use slashes when sending data in urls. You will need to urlencode it before sending it. It will then be decoded before being given back to you.
Sometimes I make web safe names when i come across this problem. So for example i need to sometimes send ids like "12345/01". When I send them over the web in urls, I say "12345_01", which we refer to as the web safe id. we then convert it by replacing _ with / when we need the real id.
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 am using servlets to allow clients to do CRUD Operations on a list. However I have one servlet, but it's possible to have multiple URL's get to this servlet because I have a wildcard character in the URL-Pattern.
http://localhost:8080/WebServiceDesignStyles3ProjectServer/SpyListCollection
This is the generic way to send a request to the servlet. However, for certain operations
http://localhost:8080/WebServiceDesignStyles3ProjectServer/SpyListCollection/{name}
Is a valid way to send a request to the servlet. I need to be able to get the last portion of that URL. It was told that I should be using getHeader("Accept") to be retrieving that. I've had success using getRequestURI(), but I was hoping someone could provide an example using getHeader(). Or at least an explanation describing the differences of the two.
Thank you for your time,
Kirie
You could split the request path by the separator(/) and check the last part.
String reqURI = req.getRequestURI();
String[] parts = reqURI.split("/");
if (parts[parts.length - 1].equals("SpyListCollection") {
//Generic operation
} else {
String operation = parts[parts.length - 1];
}
I am working on a URL filtering project . I have a database given to me which contain URLs need to be blocked (eg: a.b.com/d/e).
I get uri and domain from http request. I compare what I get with my database and redirect users without any problem. So far so good.
Problems starts with urls that contains query string and other magics with URL. As an example if user enters a.b.com/d/e?junk. What I get won't match with my database, and users will bypass my filter and they will still be able to go a.b.com/d/e.
I tried some useless actions like slicing everything after special chars like "?,#". But having problems with url like : youtube.com/watch?v=12vh55_1ul8, which becames like youtube.com/watch and blocks all youtube. That solution causes me more problems.
Now I am very confused how to handle this problem. Is there any guide or any library which I can use in C++ ?
Try this code:
string str (get_requsted_uri());
string str2 ("http://getaroundfilters.com/article/889/proxy");
if (str.find(str2) != string::npos) {
block();
} else {
get_and_return_webpage(str);
}
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.
To show this fundamental issue in .NET and the reason for this question, I have written a simple test web service with one method (EditString), and a consumer console app that calls it.
They are both standard web service/console applications created via File/New Project, etc., so I won't list the whole code - just the methods in question:
Web method:
[WebMethod]
public string EditString(string s, bool useSpecial)
{
return s + (useSpecial ? ((char)19).ToString() : "");
}
[You can see it simply returns the string s if useSpecial is false. If useSpecial is true, it returns s + char 19.]
Console app:
TestService.Service1 service = new SCTestConsumer.TestService.Service1();
string response1 = service.EditString("hello", false);
Console.WriteLine(response1);
string response2 = service.EditString("hello", true); // fails!
Console.WriteLine(response2);
[The second response fails, because the method returns hello + a special character (ascii code 19 for argument's sake).]
The error is:
There is an error in XML document (1, 287)
Inner exception: "'', hexadecimal value 0x13, is an invalid character. Line 1, position 287."
A few points worth mentioning:
The web method itself WORKS FINE when browsing directly to the ASMX file (e.g. http://localhost:2065/service1.asmx), and running the method through this (with the same parameters as in the console application) - i.e. displays XML with the string hello + char 19.
Checking the serialized XML in other ways shows the special character is being encoded properly (the SERVER SIDE seems to be ok which is GOOD)
So it seems the CLIENT SIDE has the issue - i.e. the .NET generated proxy class code doesn't handle special characters
This is part of a bigger project where objects are passed in and out of the web methods - that contain string attributes - these are what need to work properly. i.e. we're de/serializing classes.
Any suggestions for a workaround and how to implement it?
Or have I completely missed something really obvious!!?
PS. I've not had much luck with getting it to use CDATA tags (does .NET support these out of the box?).
You will need to use byte[] instead of strings.
I am thinking of some options that may help you. You can take the route using html entities instead of char(19). or as you said you may want to use CDATA.
To come up with a clean solution, you may not want to put the whole thing in CDATA. I am not sure why you think it may not be supported in .NET. Are you saying this in the context of serialization?