Issue load webpage after basic auth android - java

I need to load webpage after basic auth, but the sessionid in my code is not concatenated to url (I get this 10-08 10:17:27.424 20143-20143/com.example.marco.bella D/WebView: loadUrl=https://unimol.esse3.cineca.it/auth/Logon.do;jsessionid=
)...i don't know why...i show you my code!
variable cookie get the correct sessionID.
Thanks in advance!
URL url = null;
try {
url = new URL("https://unimol.esse3.cineca.it/auth/Logon.do");
} catch (MalformedURLException e) {
}
HttpURLConnection httpRequest = null;
try {
httpRequest = (HttpURLConnection) url.openConnection();
} catch (IOException e) {
}
try {
httpRequest.setRequestMethod("GET");
} catch (ProtocolException e) {
}
String cookie = "";
httpRequest.setDoInput(true);
String authString = "user" + ":" + "pass";
byte[] authEncBytes = android.util.Base64.encode(authString.getBytes(), android.util.Base64.DEFAULT);
String authStringEnc = new String(authEncBytes);
httpRequest.addRequestProperty("Authorization", "Basic " + authStringEnc);
System.out.println("auth:" + "" + authStringEnc);
DefaultHttpClient httpclient = new DefaultHttpClient();
List<Cookie> cookies = httpclient.getCookieStore().getCookies();
if (cookies.isEmpty()) {
System.out.println("None");
} else {
for (int i = 0; i < cookies.size(); i++) {
System.out.println("- " + cookies.get(i).toString());
cookie=cookies.get(i).toString();
}
}
webView.loadUrl("https://unimol.esse3.cineca.it/auth/Logon.do;jsessionid="+cookie);

(I get this 10-08 10:17:27.424 20143-20143/com.example.marco.bella
D/WebView:
loadUrl=https://unimol.esse3.cineca.it/auth/Logon.do;jsessionid= )...i
don't know why
The problem I can think of by looking only the portion you have posted here is that you are Overriding cookie variable inside your for loop and there is a high possibility on any iteration of cookies, one or more than 1 cookie exist whose value is blank.
for (int i = 0; i < cookies.size(); i++) {
System.out.println("- " + cookies.get(i).toString());
cookie=cookies.get(i).toString(); // This line is the culprit
}
You should check if the value of the cookie is null or empty.
Replace the culprit line with below line
I am using a utility of Strings class (isNullOrEmpty) of Guava library to check if value is null or empty, you can use your own implementation for this,
if(Strings.isNullOrEmpty(cookies.get(i).toString())) {
continue;
} else {
cookie=cookies.get(i).toString();
}
Now if during iteration you get id which you confirmed that you are getting in console statement then it will definitely get loaded up.

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.

Call Firebase function from Java via POST

So I've got this TypeScript function which writes document to firestore
exports.register = functions.https.onRequest((req:any, res:any) => {
if (req.method === 'PUT') {
res.status(403).send('Forbidden!');
return;
}
cors(req, res, () => {
const name = req.query.name;
//validations
if (!name) {
res.status(200).send("Please enter name.");
return;
}
//Other input validations....
const vkey = Math.random()*1000000000000000;
//check if user already exists in firestore
const userRef = admin.firestore().collection('users')
let userExists;
userRef.where('email', '==', email).get()
.then((snapshot: { size: any; }) => {
userExists = snapshot.size;
console.log(`user by email query size ${userExists}`);
//send error if user exists
if(userExists && userExists > 0){
res.status(200).send("Account with the same email exists");
return;
}
//add user to database
admin.firestore().collection('users').add({
name: name,
email: email,
password: password,
user: user,
vkey: vkey,
verified: 0,
token: 0
}).then((ref: { id: any; }) => {
console.log('add user account', ref.id);
res.status(200).send("Registered");
return;
});
})
.catch((err: any) => {
console.log('error getting user by email', err);
res.status(200).send("System error, please try again.");
});
});
});
And I need to call this function from my java code via POST request. I already did so via GET but the data I have to send is larger than GET can handle.(I know that register function is working correctly since I'm getting responses and when testing with GET and less data to send it writes the document to firestore no problem) My current code to achieve POST looks like this:
public class UtilsUpdateUser extends AsyncTask<String,Void,String> {
#Override
protected String doInBackground(String... urls) {
try {
String jsonString = Utils.userToString(Utils.USER);
String data = URLEncoder.encode("name", "UTF-8") + "=" +
URLEncoder.encode(usernameString, "UTF-8");
data += "&" + URLEncoder.encode("password", "UTF-8") + "=" +
URLEncoder.encode(Utils.PASSWORD, "UTF-8");
data += "&" + URLEncoder.encode("user", "UTF-8") + "=" +
URLEncoder.encode(jsonString, "UTF-8");
data += "&" + URLEncoder.encode("email", "UTF-8") + "=" +
URLEncoder.encode(emailString, "UTF-8");
URL url = new URL(urls[0]);
URLConnection conn = url.openConnection();
conn.setDoOutput(true);
OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
wr.write(data);
wr.flush();
BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
StringBuilder sb = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
sb.append(line);
}
return sb.toString();
} catch (Exception e) {
return "Internet";
}
}
#Override
protected void onPostExecute(String s) {
Log.i("MyData",s);
}
}
But this code always results in "Please enter name." Any help is greatly appreciated.
The POST call was right the problem was that in the cloud function I used
req.query.VARIABLE_NAME; this would be fine for GET put in case of POST it;s req.body.VARIABLE_NAME;

Java - Generating Views with Proxys

so im trying to generate Views on a personal page for some hours now. Im not getting why it isnt working. So my current code: (Yes im using the spigot API but that does not matter now).
#Override
public boolean onCommand(CommandSender cs, Command cmd, String label, String[] args) {
if(args.length == 2){
String link = args[0];
String views = args[1];
int view = 0;
try{
view = Integer.parseInt(views);
}catch(Exception ex){
ex.printStackTrace();
cs.sendMessage("" + views + " is not a number.");
}
execute(link, view);
}else{
System.out.println("More arguments");
}
return true;
}
private int o = 1;
private void execute(String link, int views){
new BukkitRunnable() {
int i = views;
#Override
public void run() {
i--;
try{
String fullProxy = Viewbot.getInstance().getProxys().get(i);
String proxy, port;
String[] fullProx;
fullProx = fullProxy.split(":");
proxy = fullProx[0];
port = fullProx[1];
/* Proxy proxyCon = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(proxy, Integer.parseInt(port)));
HttpURLConnection connection =(HttpURLConnection)new URL(link).openConnection(proxyCon);
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setRequestProperty("Content-type", "text/xml");
connection.setRequestProperty("Accept", "text/xml, application/xml");
connection.setRequestMethod("POST");
*/
Properties systemProperties = System.getProperties();
systemProperties.setProperty("http.proxyHost",proxy);
systemProperties.setProperty("http.proxyPort",port);
systemProperties.setProperty("https.proxyHost",proxy);
systemProperties.setProperty("https.proxyPort",port);
URL server = new URL(link);
HttpURLConnection connection = (HttpURLConnection)server.openConnection();
connection.setUseCaches(false);
connection.connect();
System.out.println("Respone: "+ connection.getResponseMessage());
System.out.println("View #" + o + " gesendet!");
o++;
connection.disconnect();
System.out.println("Proxy: " + proxy + " Port: " + port);
System.out.println("System using: " + systemProperties.getProperty("http.proxyHost") + ":" + systemProperties.getProperty("http.proxyPort"));
System.out.println("Proxy: " + connection.usingProxy());
}catch(Exception ex){
Properties systemProperties = System.getProperties();
ex.printStackTrace();
Viewbot.getInstance().failProxys.add(systemProperties.getProperty("http.proxyHost") + ":" + systemProperties.getProperty("http.proxyPort"));
}
if(i == 5){
for(int i = 0; i < Viewbot.getInstance().failProxys.size(); i++){
String failed = Viewbot.getInstance().failProxys.get(i);
Viewbot.getInstance().getProxys().remove(failed);
}
Path file = Paths.get(Viewbot.getInstance().getDataFolder() + "/HTTPS.txt");
try {
Files.write(file, Viewbot.getInstance().getProxys(), Charset.forName("UTF-8"));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("File saved");
}
if(i == 0){
cancel();
}
}
}.runTaskTimerAsynchronously(Viewbot.getInstance(), 0, 2);
}
A sample output of that whole thing:
[00:34:45 INFO]: Response: OK
[00:34:45 INFO]: View #430 sent!
[00:34:45 INFO]: Proxy: 41.76.44.76 Port: 3128
[00:34:45 INFO]: System using: 45.63.16.86:8080
[00:34:45 INFO]: Proxy: false
The output now is:
[00:57:58 INFO]: Proxy: 36.80.34.225 Port: 3128
[00:57:58 INFO]: System using: 113.18.193.26:8000
[00:57:58 INFO]: Proxy: true
[00:57:58 INFO]: Respone: OK
But when you look here: http://84.200.122.107/ It does not add views..
So its using a different IP Address to connect. Whats the error than?
It also shows that it isn't using a proxy, which should be wrong..
Thanks for any help.
I think you iterate over the list of proxy, each call to BukkitRunnable will take a new one.
So if call to BukkitRunnable in different threads overlap, one set the value and the other reads it.
To use one proxy per connection you should use common http client that is able to do it :
Common HTTPclient and proxy

Merge two URL in JAVA

I merge two url with the following code.
String strUrl1 = "http://www.domainname.com/path1/2012/04/25/file.php";
String arg = "?page=2";
URL url1;
try {
url1 = new URL(strUrl1);
URL reconUrl1 = new URL(url1,arg);
System.out.println(" url : " + reconUrl1.toString());
} catch (MalformedURLException ex) {
ex.printStackTrace();
}
I'm surprise by the result : http://www.domainname.com/path1/2012/04/25/?page=2
I expect it to be (what browser do) : http://www.domainname.com/path1/2012/04/25/file.php?page=2
Tha javadoc about the constructor URL(URL context, String spec) explain it should respect the RFC.
I'm doing something wrong ?
Thanks
UPDATE :
This is the only problem I encountered with the fonction.
The code already works in all others cases, like browser do
"domain.com/folder/sub" + "/test" -> "domain.com/test"
"domain.com/folder/sub/" + "test" -> "domain.com/folder/sub/test"
"domain.com/folder/sub/" + "../test" -> "domain.com/folder/test"
...
You can always merge the String first and then created the URL based on the merged String.
StringBuffer buf = new StringBuffer();
buf.append(strURL1);
buf.append(arg);
URL url1 = new URL(buf.toString());
try
String k = url1+arg;
URL url1;
try {
url1 = new URL(k);
//URL reconUrl1 = new URL(url1,arg);
System.out.println(" url : " + url1.toString());
} catch (MalformedURLException ex) {
ex.printStackTrace();
}
I haven't read through the RFC, but the context (as mentioned in the Java Doc for URL) is presumably the directory of a URL, which means that the context of
"http://www.domainname.com/path1/2012/04/25/file.php"
is
"http://www.domainname.com/path1/2012/04/25/"
which is why
new URL(url1,arg);
yields
"http://www.domainname.com/path1/2012/04/25/?page=2"
The "workaround" is obviously to concatenate the parts yourself, using +.
you are using the constructor of URL here which takes paramter as URL(URL context, String spec). So you dont pass the php page with the URL but instead with the string. context needs to be the directory. the proper way to do this would be
String strUrl1 = "http://www.domainname.com/path1/2012/04/25";
String arg = "/file.php?page=2";
URL url1;
try {
url1 = new URL(strUrl1);
URL reconUrl1 = new URL(url1,arg);
System.out.println(" url : " + reconUrl1.toString());
} catch (MalformedURLException ex) {
ex.printStackTrace();
}
Try this
String strUrl1 = "http://www.domainname.com/path1/2012/04/25/";
String arg = "file.php?page=2";
URL url1;
try {
url1 = new URL(strUrl1);
URL reconUrl1 = new URL(url1,arg);
System.out.println(" url : " + reconUrl1.toString());
} catch (MalformedURLException ex) {
ex.printStackTrace();
}
When you read the java doc it mentions about the context of the specified URL
Which is the domain and the path:
"http://www.domainname.com" + "/path1/2012/04/25/"
Where "file.php" is considered the text where it belongs to the context mentioned above.
This two parameter overloaded constructor uses the context of a URL as base and adds the second param to create a complete URL, which is not what you need.
So it's better to String add the two parts and then create URL from them:
String contextURL = "http://www.domainname.com/path1/2012/04/25/";
String textURL = "file.php?page=2";
URL url;
try {
url = new URL(contextURL);
URL reconUrl = new URL(url, textURL);
System.out.println(" url : " + reconUrl.toString());
} catch (MalformedURLException murle) {
murle.printStackTrace();
}

how to get a value by key from an json (or xml) string?

In the android app I get an xml or json string returned, However, I cant seem to figure out any way on how to get an value from the string in any way by entering an key.
In PHP you just use something like $myArray['parent']['child'] but I have no clue on how this works in java.
Any idea's would be greatly appreciated! (an example for both XML and JSON even more ;) )
Here's what I would do:
locate an XML/JSON library (there's tons) (google-gson for json)
read the documentation to find a parse method ((new JsonParser()).parse(text))
read the documentation to find out what the return value is (JsonElement)
decide what you want to do with the parsed data (myJsonObj.get(...))
write the code
public class parsingjsontest2 extends Activity {
/** Called when the activity is first created. */
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(main);
String str = connect("http://rentopoly.com/ajax.php?query=Bo"));
System.out.println("String::"+str);
}
}
private String connect(String url)
{
// Create the httpclient
HttpClient httpclient = new DefaultHttpClient();
// Prepare a request object
HttpGet httpget = new HttpGet(url);
// Execute the request
HttpResponse response;
// return string
String returnString = null;
try {
// Open the webpage.
response = httpclient.execute(httpget);
if(response.getStatusLine().getStatusCode() == 200){
// Connection was established. Get the content.
HttpEntity entity = response.getEntity();
// If the response does not enclose an entity, there is no need
// to worry about connection release
if (entity != null) {
// A Simple JSON Response Read
InputStream instream = entity.getContent();
// Load the requested page converted to a string into a JSONObject.
JSONObject myAwway = new JSONObject(convertStreamToString(instream));
// Get the query value'
String query = myAwway.getString("query");
**// Make array of the suggestions
JSONArray suggestions = myAwway.getJSONArray("suggestions");
// Build the return string.
returnString = "Found: " + suggestions.length() + " locations for " + query;
for (int i = 0; i < suggestions.length(); i++) {
returnString += "\n\t" + suggestions.getString(i);
}
// Cose the stream.
instream.close();
}
}
else {
// code here for a response othet than 200. A response 200 means the webpage was ok
// Other codes include 404 - not found, 301 - redirect etc...
// Display the response line.
returnString = "Unable to load page - " + response.getStatusLine();
}
}
catch (IOException ex) {
// thrown by line 80 - getContent();
// Connection was not established
returnString = "Connection failed; " + ex.getMessage();
}
catch (JSONException ex){
// JSON errors
returnString = "JSON failed; " + ex.getMessage();
}
return returnString;
}
private static String convertStreamToString(InputStream is) {
/*
* To convert the InputStream to String we use the BufferedReader.readLine()
* method. We iterate until the BufferedReader return null which means
* there's no more data to read. Each line will appended to a StringBuilder
* and returned as String.
*/
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line = null;
try {
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return sb.toString();
}
}
As you didn't specify what kind of xml you are trying to read, I'm answering based on what I know.
In Android, if you were talking about the layout and strings.xml files, you use a dot (.) operator, like R.string.appname.
Please post more details about your specific problem, if this is not what you were looking for.

Categories

Resources