I am getting a URL from a message that is in the form https://example.com/eUjKSv, however I need to insert a "tag" /raw/ in between .com/ and eUjKSv.
I was wondering what would be the easiest way to do it, currently I have a very "hacky" way to achieve it, new URL("https://example.com/raw" + new URL(link).getPath()), I know it's pretty awful and only works if I know exactly the URL. Any suggestions on how to make this better? I thought about regex but couldn't think of a good one to capture it.
You can use either the URL class or the URI class. They both work for this.
URL baseUrl = new URL("https://example.com/eUjKSv");
URL rawUrl = new URL(baseUrl, "/raw" + baseUrl.getPath());
System.out.println("baseUrl = " + baseUrl);
System.out.println("rawUrl = " + rawUrl);
URI baseUri = new URI("https://example.com/eUjKSv");
URI rawUri = baseUri.resolve("/raw" + baseUri.getPath());
System.out.println("baseUri = " + baseUri);
System.out.println("rawUri = " + rawUri);
Output
baseUrl = https://example.com/eUjKSv
rawUrl = https://example.com/raw/eUjKSv
baseUri = https://example.com/eUjKSv
rawUri = https://example.com/raw/eUjKSv
Related
I am using Java-8, I would like to check whether the URL is valid or not based on pattern.
If valid then I should get the attributes bookId, authorId, category, mediaId
Pattern: <basepath>/books/<bookId>/author/<authorId>/<isbn>/<category>/mediaId/<filename>
And this is the sample URL
URL => https:/<baseurl>/v1/files/library/books/1234-4567/author/56784589/32475622347586/media/324785643257567/507f1f77bcf86cd799439011_400.png
Here Basepath is /v1/files/library.
I see some pattern matchings but I couldn't relate with my use-case, probably I was not good at reg-ex. I am also using apache-common-utils but I am not sure How to achieve it either.
Any help or hint would be really appreciable.
Try this solution (uses named capture groups in regex):
public static void main(String[] args)
{
Pattern p = Pattern.compile("http[s]?:.+/books/(?<bookId>[^/]+)/author/(?<authorId>[^/]+)/(?<isbn>[^/]+)/media/(?<mediaId>[^/]+)/(?<filename>.+)");
Matcher m = p.matcher("https:/<baseurl>/v1/files/library/books/1234-4567/author/56784589/32475622347586/media/324785643257567/507f1f77bcf86cd799439011_400.png");
if (m.matches())
{
System.out.println("bookId = " + m.group("bookId"));
System.out.println("authorId = " + m.group("authorId"));
System.out.println("isbn = " + m.group("isbn"));
System.out.println("mediaId = " + m.group("mediaId"));
System.out.println("filename = " + m.group("filename"));
}
}
prints:
bookId = 1234-4567
authorId = 56784589
isbn = 32475622347586
mediaId = 324785643257567
filename = 507f1f77bcf86cd799439011_400.png
I can already tell this is a stupid question, but I'm trying to login to a web page using postUrl() and when I explicitly write the post data like this:
String postData = "username=johndoe&password=mypassword";
myWebview.postUrl("https://moodle.domain.com/login/index.php", postData.getBytes());
It works, but if I construct the postData using variables:
String postData = "username="+user+"&password="+pass;
myWebview.postUrl("https://moodle.domain.com/login/index.php", postData.getBytes());
It doesn't work. Wrong credentials. I've verified that both strings have the exact same value and I tried using UTF-8.
I just don't understand, this doesn't make any sense.
It look Like, the 'user' or the 'pass' has invisible symbols. You need:
1) Check string using equals:
String postData1 = "username=johndoe&password=mypassword";
String postData2 = "username="+user+"&password="+pass;
System.out.println(postData1.equals(postData2));
2) Try to find invisible symbols, for example:
String postData1 = "username=johndoe&password=mypassword";
String postData2 = "username="+user+"&password="+pass;
char[] arr1 = postData1.toCharArray();
char[] arr2 = postData1.toCharArray();
for (int i=0; i< Math.max(arr1.length(), arr2.length(); i++ ) {
if(arr1[i] != arr2[i]) {
System.out.println("Different i " + i);
}
}
Try the below
String postData = "username=" + URLEncoder.encode(my_username, "UTF-8") + "&password=" + URLEncoder.encode(my_password, "UTF-8");
webview.postUrl("https://moodle.domain.com/login/index.php",postData.getBytes());
after a little bit of trying I managed to get results from the ldap-server at my company. Now I have a little problem and I seem to be too dump to find any documentation about it.
Command objCmd = new Command();
Recordset RS = new Recordset();
objCmd.setActiveConnection(conn);
objCmd.setCommandText("<LDAP://scdldap.siemens.net:389>;(&(objectClass=scdInternetPerson)(mail=" + email + "));" +searchKeyword+";subTree");
RS = objCmd.Execute();
if (RS.getBOF())
System.out.printf(email + ";" + "null" + "\n");
else {
RS.MoveFirst();
System.out.printf(email + ";" + RS.getFields().getItem(0).getValue() + "\n");
}
This works fine as long as I print the result out to the console. But I would need to get the value as a String (it is always a String), but I can't make it. Can somebody tell me what I am missing? I know this is some VariableType Error, because the result is of type Variant, but
Variant.toString() or anything else is not possible.
Try to convert Variant to Object and then to String, for example:
Variant From = new Variant(1);
Variant To = new Variant(6);
Object[] args = new Object[]{From, To};
String From1 = args[0].toString();
String To1 = args[1].toString();
I want to write something like this 1/2
JSONObject requestJson =
new JSONObject().put("method", "setShutterSpeed")
.put("params", new JSONArray().put('1' + "\\" + '/' + '2'))
.put("id", id()).put("version", "1.0");
String url = findActionListUrl(service) + "/" + service;
But everything I tried fails.
I always receive something like that:
{"id":14,"method":"setShutterSpeed","version":"1.0","params":["1\\\/2"]}
I try to use the sony cameraremote API with this JSON call
URL url = new URL("http://www.example.com/data.php?q=%FD");
logger.info("url: " + url);
URI uri = url.toURI();
logger.info("uri ASCII: " + uri.toASCIIString());
logger.info("uri str : " + uri.toString());
logger.info("query : " + uri.getQuery());
logger.info("decoded : " + URLDecoder.decode(ur.getRawQuery(), "WINDOWS-1252"));
String scheme = uri.getScheme();
String auth = uri.getAuthority();
String path = uri.getPath();
String query = uri.getQuery();
URI cleanedURI = new URI(scheme, auth, path, query, null);
logger.info("cleaned uri ASCII: " + cleanedURI.toASCIIString());
logger.info("cleaned uri str : " + cleanedURI.toString());
The output is:
url: http://www.example.com/data.php?q=%FD
uri ASCII: http://www.example.com/data.php?q=%FD
uri str : http://www.example.com/data.php?q=%FD
query: q=�
decoded: q=ý
cleaned uri ASCII: http://www.example.com/data.php?q=%EF%BF%BD
cleaned uri str : http://www.example.com/data.php?q=�
So, when I split the URI into parts, and then construct again, I cannot get back the original URL. How can I get back the original URL, which is a correctly percent-encoded, valid URL.
Instead of getting %EF%BF%BD I need to get the original %3F.
(Actually what I am trying to achieve is to manipulate certain parts of the URL in a clean way, such as removing the fragment, but this has not much relation to my question.)
The URL http://www.example.com/data.php?q=? is same as http://www.example.com/data.php?q=%3F
%3F (or numeric 63) is nothing but the ascii code for character '?'.
Check it here: http://grox.net/utils/encoding.html
So, if you hit a browser with URL %3f or '?'; it should behave the same.
If you are much concerned with the how it displays on the console, you could try this.
String query = uri.getQuery();
char charData = query.charAt(0); // fetch the character from String
int asciiValue = (int)charData;
or
you could look into String's getByte() method. A short tutorial is here - http://www.tutorialspoint.com/java/java_string_getbytes.htm