Get specific parts of a string that contains garbage data - java

So I have these strings that I'm getting that contains a lot of garbage data which i don't want
"http://v20.lscache8.c.youtube.com/videoplayback?id=271de9756065677e&itag=17&ip=0.0.0.0&ipbits=0&expire=999999999999999999"&sparams=ip,ipbits,expireip,ipbits,expire,id,itag&signature=3DCD3F79E045F95B6AF661765F046FB0440FF01606A42661B3AF6BAF046F012549CC9BA34EBC80A9"
So basicly I just want it to search trough the string for videoplayback?id=
*and just copy whats between videoplayback?id= and &
271de9756065677e
and then continue to go trough the string and Grab the signature in the same way
So anyone can help me with the logic and examples how to do this ?

Since your "string that contains garbage data" is actually a URL you should use the URL class
Have a look at the tutorial Parsing a URL
import java.net.*;
import java.io.*;
public class ParseURL {
public static void main(String[] args) throws Exception {
String url = "http://v20.lscache8.c.youtube.com/videoplayback?id=271de9756065677e&itag=17&ip=0.0.0.0&ipbits=0&expire=999999999999999999"&sparams=ip,ipbits,expireip,ipbits,expire,id,itag&signature=3DCD3F79E045F95B6AF661765F046FB0440FF01606A42661B3AF6BAF046F012549CC9BA34EBC80A9";
URL aURL = new URL(url);
System.out.println("protocol = " + aURL.getProtocol());
System.out.println("authority = " + aURL.getAuthority());
System.out.println("host = " + aURL.getHost());
System.out.println("port = " + aURL.getPort());
System.out.println("path = " + aURL.getPath());
System.out.println("query = " + aURL.getQuery());
}
}
The output should be:
protocol = http
authority = v20.lscache8.c.youtube.com:80
host = v20.lscache8.c.youtube.com
port = 80
path = /videoplayback
query = id=271de9756065677e&itag=17&ip=0.0.0.0&ipbits=0&expire=999999999999999999"&sparams=ip,ipbits,expireip,ipbits,expire,id,itag&signature=3DCD3F79E045F95B6AF661765F046FB0440FF01606A42661B3AF6BAF046F012549CC9BA34EBC80A9
In order to parse the query, use URLEncodedUtils
String url = "http://v20.lscache8.c.youtube.com/videoplayback?id=271de9756065677e&itag=17&ip=0.0.0.0&ipbits=0&expire=999999999999999999"&sparams=ip,ipbits,expireip,ipbits,expire,id,itag&signature=3DCD3F79E045F95B6AF661765F046FB0440FF01606A42661B3AF6BAF046F012549CC9BA34EBC80A9";
List<NameValuePair> params = URLEncodedUtils.parse(new URI(url), "UTF-8");
for (NameValuePair param : params) {
System.out.println(param.getName() + "=" + param.getValue());
}
The output should be:
id=271de9756065677e
itag=17
ip=0.0.0.0
ipbits=0
expire=999999999999999999"
sparams=ip,ipbits,expireip,ipbits,expire,id,itag
signature=3DCD3F79E045F95B6AF661765F046FB0440FF01606A42661B3AF6BAF046F012549CC9BA34EBC80A9

Related

com.google.code.gson cannot parse tamil results

So, I'm trying to fetch JSON results from https://api-thirukkural.vercel.app/api?num=1139 using Java-Telegram-Bot-Api and send it to telegram. I use com.google.code.gson dependency for parsing JSON.
The expected results from API:
{"number":1139,"sect_tam":"காமத்துப்பால்","chapgrp_tam":"களவியல்","chap_tam":"நாணுத் துறவுரைத்தல்","line1":"அறிகிலார் எல்லாரும் என்றேஎன் காமம்","line2":"மறுகின் மறுகும் மருண்டு.","tam_exp":"என்னைத் தவிர யாரும் அறியவில்லை என்பதற்காக என் காதல் தெருவில் பரவி மயங்கித் திரிகின்றது போலும்!","sect_eng":"Love","chapgrp_eng":"The Pre-marital love","chap_eng":"Declaration of Love's special Excellence","eng":"My perplexed love roves public street Believing that none knows its secret","eng_exp":"And thus, in public ways, perturbed will rove"}
Here is a piece of my java code:
String results = "";
Random random = new Random();
SendMessage message = new SendMessage();
String apiUrl = "https://api-thirukkural.vercel.app/api?num=" + random.nextInt(1329 + 1);
try {
URL url = new URL(apiUrl);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
Scanner sc = new Scanner(url.openStream());
while (sc.hasNext()) {
results += sc.nextLine();
}
sc.close();
JSONArray jsonArray = new JSONArray("[" + results + "]");
JSONObject object = jsonArray.getJSONObject(0);
message.setChatId(update.getMessage().getChatId().toString());
message.setText("Number: " + object.getInt("number") + "\n\n" + object.getString("line1") + "\n"
+ object.getString("line2") + "\n\n" + object.getString("tam_exp") + "\n\n" + object.getString("eng_exp"));
conn.disconnect();
execute(message);
} catch (Exception e) {
e.printStackTrace();
}
The result in telegram:
Number: 1139
அறிகிலார� எல�லார�ம� என�றேஎன� காமம�
மற�கின� மற�க�ம� மர�ண�ட�.
என�னைத� தவிர யார�ம� அறியவில�லை என�பதற�காக என� காதல� தெர�வில� பரவி மயங�கித� திரிகின�றத� போல�ம�!
And thus, in public ways, perturbed will rove
Is this a problem in gson dependency? Can someone help me fix this? Thanks.
You need to specify the Charset on Scanner. That is probably the problem.
Example:
new Scanner(url.openStream(), StandardCharsets.UTF_8.name());
You should use the Charset that fits.

Win32Exception: The parameter is incorrect

I am making an application that displays ur saved browser password(s) (right now I'm using Google Chrome) in an easy way. Everytime I run this code I get an error at byte[] newbyte = Crypt32Util.cryptUnprotectData(mybyte);. The code used is written below. This code provides some context. I never had this problem and after some research I can't find a clear solution. I hope someone can help me with it.
Code:
Connection connection = null;
connection = DriverManager.getConnection("jdbc:sqlite:" + path_to_copied_db);
PreparedStatement statement = connection.prepareStatement("SELECT `origin_url`,`username_value`,`password_value` FROM `logins`");
ResultSet re = statement.executeQuery();
StringBuilder builder = new StringBuilder();
while (re.next()) {
String pass = "";
try {
byte[] mybyte = (byte[])re.getBytes("password_value");
byte[] newbyte = Crypt32Util.cryptUnprotectData(mybyte); //Error on this line:71
pass = new String(newbyte);
}catch(Win32Exception e){
e.printStackTrace();
}
builder.append(user + ": " + re.getString("origin_url") + " " + re.getString("username_value") + " " + re.getBinaryStream("password_value") + "\n");
}
Error:
com.sun.jna.platform.win32.Win32Exception: The parameter is incorrect.
at com.sun.jna.platform.win32.Crypt32Util.cryptUnprotectData(Crypt32Util.java:128)
at com.sun.jna.platform.win32.Crypt32Util.cryptUnprotectData(Crypt32Util.java:103)
at com.sun.jna.platform.win32.Crypt32Util.cryptUnprotectData(Crypt32Util.java:90)
at Client.Client.main(Client.java:71)

Java FTP LIST command

Hello I am in the process of making a FTP client for a uni project and before starting I am experimenting with the basic commands of the FTP protocol. So far I have been able to login successfully to a FTP server. Now, what I want to do is to list the root directory in order to get it's contents.
I have implemented this method to do what I describe but I can't seem to make it work. As far as I can tell it does go into passive mode, but when I issue the list command nothing happens. I cannot understand why this is happening. Can anyone help?
Here is the code in question:
public synchronized boolean list() throws IOException{
sendLine("PASV");
String response = readLine();
if(!response.startsWith("227 "))
throw new IOException("Could not request PASSIVE mode: " + response);
String ip = null;
int port = -1;
int opening = response.indexOf('(');
int closing = response.indexOf(')', opening + 1);
if(closing > 0){
String dataLink = response.substring(opening + 1, closing);
StringTokenizer tokenizer = new StringTokenizer(dataLink, ",");
try{
ip = tokenizer.nextToken() + "." + tokenizer.nextToken() + "." + tokenizer.nextToken() + "." + tokenizer.nextToken();
port = Integer.parseInt(tokenizer.nextToken()) * 256 + Integer.parseInt(tokenizer.nextToken());
}catch(Exception e){
throw new IOException("Received bad data information: " + response);
}
}
sendLine("LIST");
response = readLine();
return (response.startsWith("200 "));
}

Start user's standard mail client with attachment pre-attached

I'm looking for a way that my application can call the user's standard mail application (e.g. Outlook, Thunderbird, etc.). And give it an recipient address, the email text and an attachment.
So, basically the standard email application should pop up have the email ready for me (with recipient, text and attachment) and all that is left to do for me is pressing "send" in my outlook, thunderbird etc.
I've been googling for a while now, but I couldn't find a real solution.
I've been looking into mapi a bit but it seems like 1. it's deprecated and 2. it's mainly built for outlook.
Any help/suggestions/solutions greatly appreciated!
Edit: I have seen the question Start Mail-Client with Attachment but no working answer was provided there and also the question is more than 3 years old.
Edit: Other languages would be ok, too. Has to work on Windows XP, Vista, 7, 8 (both 32 and 64 bit)
UPDATE: It seems to be more difficult than I have thought it to be.
I've been looking into JMAPI, which apparently only works for 32bit Systems.
I've also seen the solutions on codeproject.org (here and here), but I somehow couldn't get them to work.
Now I'm trying to do it with command line:
1. Read user's default mail client
2. Call a batch file according to the email client. (Yes you have to write a batch file for every common mail client.
Example for outlook:
"outlook.exe" /a "F:\test.png" /m "test.test#test.test&cc=test#test.test&subject=subject123&body=Hello, how are you%%3F%%0D%%0Anew line"
--> see my provided answer for futher info on that method
So...
After days of research I gave up to get a general solution.
I came up with a solution working at least for the two most common clients (Thunderbird & Outlook)
My solution is basically calling the application from command line.
For those interested, here is my solution: (I haven't tested it cross platform - works on my old XP laptop though)
import java.io.IOException;
/*
:: Punctuation Hexadecimal equivalent
:: ----------------------------------------------
:: Space ( ) %20
:: Comma (,) %2C
:: Question mark (?) %3F
:: Period (.) %2E
:: Exclamation point (!) %21
:: Colon (:) %3A
:: Semicolon (;) %3B
:: Line feed %0A --> New line %0D%0A
:: Line break (ENTER key) %0D --> New line %0D%0A
*/
public class Main {
static String test = "hi";
private static String attachment;
private static String to;
private static String cc;
private static String subject;
private static String body;
public static void main (String[] args){
attachment = "F:\\pietquest.png";
to = "test#test.de";
cc = "a.b#c.de";
subject = "TestSubject 123";
body = "Hi, what\'s going on%0D%0Anew line";
body = replace(body);
subject = replace(subject);
String[] value = WindowsRegistry.readRegistry("HKEY_LOCAL_MACHINE\\SOFTWARE\\Clients\\Mail", "");
if (value[10].contains("Thunderbird")){
System.out.println("Thunderbird");
String[] pfad = WindowsRegistry.readRegistry("HKEY_LOCAL_MACHINE\\SOFTWARE\\Clients\\Mail\\Mozilla Thunderbird\\shell\\open\\command", "");
String Pfad = pfad[10] + " " + pfad[11];
String argument = Pfad + " /compose \"to=" + to + ",cc=" + cc + ",subject=" + subject + ",body=" + body + ",attachment=" + attachment + "\"";
// System.out.println(argument);
try {
Runtime.getRuntime().exec(argument);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
else if (value[10].contains("Outlook")){
System.out.println("Outlook");
String[] pfad = WindowsRegistry.readRegistry(
"HKEY_LOCAL_MACHINE\\SOFTWARE\\Clients\\Mail\\Microsoft Outlook\\shell\\open\\command", "");
String Pfad = pfad[10];
String argument = Pfad + " /a " + attachment + " /m \"" + to
+ "&cc=" + cc + "&subject=" + subject + "&body=" + body + "\"";
// System.out.println(argument);
try {
Runtime.getRuntime().exec(argument);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public static String replace(String toReplace){
toReplace = toReplace.replace(" ", "%20");
toReplace = toReplace.replace(",", "%2C");
toReplace = toReplace.replace("?", "%3F");
toReplace = toReplace.replace(".", "%2E");
toReplace = toReplace.replace("!", "%21");
toReplace = toReplace.replace(":", "%3A");
toReplace = toReplace.replace(";", "%3B");
return toReplace;
}
}
and this is the Windows Registry Class: (got that from here)
import java.io.IOException;
import java.io.InputStream;
import java.io.StringWriter;
public class WindowsRegistry {
/**
*
* #param location path in the registry
* #param key registry key
* #return registry value or null if not found
*/
public static final String[] readRegistry(String location, String key){
try {
// Run reg query, then read output with StreamReader (internal class)
Process process = Runtime.getRuntime().exec("reg query " +
'"'+ location);
StreamReader reader = new StreamReader(process.getInputStream());
reader.start();
process.waitFor();
reader.join();
// Parse out the value
String[] parsed = reader.getResult().split("\\s+");
if (parsed.length > 1) {
return parsed;
}
} catch (Exception e) {}
return null;
}
static class StreamReader extends Thread {
private InputStream is;
private StringWriter sw= new StringWriter();
public StreamReader(InputStream is) {
this.is = is;
}
public void run() {
try {
int c;
while ((c = is.read()) != -1)
sw.write(c);
} catch (IOException e) {
}
}
public String getResult() {
return sw.toString();
}
}
you can use C#: Example C# or java: Example Java
EDIT
You can use Boost for ssl and send email via smtp

How to parse this URL in java

I have a URL like this
http://my.my.info/action/doning/something?mailParams=iCgGugAIdMW3CqkYbZ/dGYVqljerVjzbKLvTQCyuosHzxisIrgYf8rcKqRhtn90Z0eVGZ+vx43P4g+umFmddNdDufWv/nDwbCgqBwHs9OYVd5g4VKuFO4jTfF1NiW+KjUi3JubtJT+0F7p+wPHEpTRwJJ+O0eevojx6DioK3cLGejz5UdfIrqzOVNT05TaPKFie4yZxbXfA=
I need it in key value pair not as a query string.
my out put should be
mailParams = iCgGugAIdMW3CqkYbZ/dGYVqljerVjzbKLvTQCyuosHzxisIrgYf8rcKqRhtn90Z0eVGZ+vx43P4g+umFmddNdDufWv/nDwbCgqBwHs9OYVd5g4VKuFO4jTfF1NiW+KjUi3JubtJT+0F7p+wPHEpTRwJJ+O0eevojx6DioK3cLGejz5UdfIrqzOVNT05TaPKFie4yZxbXfA=
i could not parse this string since the above is encoded. I have used URLEncodedUtils to parse this but it returns
mailParams = iCgGugAIdMW3CqkYbZ/dGYVqljerVjzbKLvTQCyuosHzxisIrgYf8rcKqRhtn90Z0eVGZ vx43P4g umFmddNdDufWv/nDwbCgqBwHs9OYVd5g4VKuFO4jTfF1NiW KjUi3JubtJT 0F7p wPHEpTRwJJ O0eevojx6DioK3cLGejz5UdfIrqzOVNT05TaPKFie4yZxbXfA
which is not at all relevant can some one help me to do this?
Try using java.net.URL
A sample code below.
URL aURL = new URL("http://my.my.info/action/doning/something?mailParams=iCgGugAIdMW3CqkYbZ/dGYVqljerVjzbKLvTQCyuosHzxisIrgYf8rcKqRhtn90Z0eVGZ+vx43P4g+umFmddNdDufWv/nDwbCgqBwHs9OYVd5g4VKuFO4jTfF1NiW+KjUi3JubtJT+0F7p+wPHEpTRwJJ+O0eevojx6DioK3cLGejz5UdfIrqzOVNT05TaPKFie4yZxbXfA=");
System.out.println("protocol = " + aURL.getProtocol());
System.out.println("authority = " + aURL.getAuthority());
System.out.println("host = " + aURL.getHost());
System.out.println("port = " + aURL.getPort());
System.out.println("path = " + aURL.getPath());
System.out.println("query = " + aURL.getQuery());
System.out.println("filename = " + aURL.getFile());
System.out.println("ref = " + aURL.getRef());
If need to split the link to get the parameters
use String#split which takes the regex as a arguments
String link="http://my.my.info/action/doning/something?mailParams=iCgGugAIdMW3CqkYbZ/dGYVqljerVjzbKLvTQCyuosHzxisIrgYf8rcKqRhtn90Z0eVGZ+vx43P4g+umFmddNdDufWv/nDwbCgqBwHs9OYVd5g4VKuFO4jTfF1NiW+KjUi3JubtJT+0F7p+wPHEpTRwJJ+O0eevojx6DioK3cLGejz5UdfIrqzOVNT05TaPKFie4yZxbXfA=";
String[] mailparams=link.split("\\?");
System.out.print(mailparams[1]);
You can also
Use URL#getQuery
String link="http://my.my.info/action/doning/something?mailParams=iCgGugAIdMW3CqkYbZ/dGYVqljerVjzbKLvTQCyuosHzxisIrgYf8rcKqRhtn90Z0eVGZ+vx43P4g+umFmddNdDufWv/nDwbCgqBwHs9OYVd5g4VKuFO4jTfF1NiW+KjUi3JubtJT+0F7p+wPHEpTRwJJ+O0eevojx6DioK3cLGejz5UdfIrqzOVNT05TaPKFie4yZxbXfA=";
URL aURL = new URL(link);
System.out.println( aURL.getQuery());
OUTPUT:
mailParams=iCgGugAIdMW3CqkYbZ/dGYVqljerVjzbKLvTQCyuosHzxisIrgYf8rcKqRhtn90Z0eVGZ+vx43P4g+umFmddNdDufWv/nDwbCgqBwHs9OYVd5g4VKuFO4jTfF1NiW+KjUi3JubtJT+0F7p+wPHEpTRwJJ+O0eevojx6DioK3cLGejz5UdfIrqzOVNT05TaPKFie4yZxbXfA=
URL aURL = null;
try {
aURL = new URL("http://my.my.info/action/doning/something?mailParams=iCgGugAIdMW3CqkYbZ/dGYVqljerVjzbKLvTQCyuosHzxisIrgYf8rcKqRhtn90Z0eVGZ+vx43P4g+umFmddNdDufWv/nDwbCgqBwHs9OYVd5g4VKuFO4jTfF1NiW+KjUi3JubtJT+0F7p+wPHEpTRwJJ+O0eevojx6DioK3cLGejz5UdfIrqzOVNT05TaPKFie4yZxbXfA=");
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(aURL.getQuery());

Categories

Resources