URL Source code in PC doesn't match in Phone HTTP - java

Using this URL : https://twitter.com/cnn/status/824165048436322304
When I check the view-source in my PC web browser I can find og:type like
<meta property="og:title" content="CNN on Twitter">
but when I try to view the source-code using Java in my Phone I can't find this og:type or og:title properties
Java code (works fine) does show me the full source but no og:type properties:
String urlContent = "";
try {
System.out.println("I'm trying to connect");
URL urlCon = new URL(urlStr);
HttpURLConnection con = (HttpURLConnection) urlCon
.openConnection();
System.out.println("Ok im here ");
con.setReadTimeout(6000);
if (con.getResponseCode() == 200) {
System.out.println("Page is ok!");
urlContent = readStream(con.getInputStream());
return urlContent;
} else {
return urlContent;
}
} catch (UnknownHostException e){
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
return urlContent;
}

Related

how to not show the button if the link is a 404 in java?

I am coming to a issue where I have a button link that goes to a pdf. I need help to check if there is no pdf (404) then do not show the button at all. If it is 200 then show the button. How can I achieve this in java? Thanks !
Here is my code:
Pay
Java
public String jobpayGrade;
public String getJobpayGrade() {
return jobpayGrade;
}
public void setJobpayGrade(String jobpayGrade) {
this.jobpayGrade = "http://WEB_ADDRESS/paygrade/" + jobpayGrade + ".pdf";
}
Here is how you can get the error code of a page
URL url = new URL(jobpayGrade);
HttpURLConnection connection = (HttpURLConnection)url.openConnection();
connection.setRequestMethod("GET");
connection.connect();
int code = connection.getResponseCode();
if(code==200){
//set visible
}else if(code==404){
//set not visible
}
If I was implementing it it would look like this somewhat
if(isValidURLConnection(jobpayGrade)){
button.setVisible(true);
}else {
button.setVisible(false);
}
private boolean isValidURLConnection(String jobPayGrade){
URL url = null;
try {
url = new URL(jobPayGrade);
} catch (MalformedURLException e) {
e.printStackTrace();
}
HttpURLConnection connection = (HttpURLConnection)url.openConnection();
try {
connection.setRequestMethod("GET");
connection.connect();
return connection.getResponseCode()==200;
} catch (IOException e) {
e.printStackTrace();
}
return false;
}

Https connection Android with Http authentication

I want to make a HTTPS connection with a webservice in Android Studio but it doesn't work with the code that I have written. I get a FileNotFound Exception.
My URL is working in the browser.
And response code is 400.
This is my code:
protected Void doInBackground(String... params) {
String https_url = "HERE MY HTTPS URL";
URL url;
try {
url = new URL(https_url);
HttpsURLConnection con = (HttpsURLConnection) url.openConnection();
String userPassword = "USERNAME" + ":" + "PASSWORD";
String encoding = new String(Base64.encode(userPassword.getBytes(), Base64.DEFAULT));
con.setRequestProperty("Authorization", "Basic " + encoding);
con.setRequestProperty("User-Agent","Mozilla/5.0 ( compatible ) ");
con.setRequestProperty("Accept","*/*");
//dumpl all cert info
print_https_cert(con);
//dump all the content
print_content(con);
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
Someone who can help me out?

Load certificate previosly imported manually [ANDROID] [HTTPS]

I added a self signed-certificate (https) manually in Settings-> Security-> Trusted certificates.
And if i try to access to my server using google chrome runs OK (previosly accept message). But in my Android application, using HttpsURLConnection class,crashes.
This is my code:
try {
URL obj = new URL(url);
HttpsURLConnection conn;
conn = (HttpsURLConnection) obj.openConnection();
conn.setConnectTimeout(timeOut);
conn.setRequestMethod("GET");
StatusCode = conn.getResponseCode();
Log.i(TAG_GET, "Status Code: " + StatusCode);
InputStream body = null;
try {
body = conn.getInputStream();
} catch (Exception e) {
e.printStackTrace();
body = conn.getErrorStream();
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
I think that I need load certificates installed on my device, but how can I do?
Thanks

How to handle exception when web service is not available in java

I need to handle an exception when web service is not available. In my app i am requesting an web service what will return me an XML data. My app is working correctly when web service is available. But when the web service is unavailable then my app become crash.how to catch that exception in java. Please note that i am developing an app for android.
when the web service is unavailable the it looks likes the following image
with this you can check webservice available or not
public void isAvailable(){
// first check if there is a WiFi/data connection available... then:
URL url = new URL("URL HERE");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestProperty("Connection", "close");
connection.setConnectTimeout(10000); // Timeout 10 seconds
connection.connect();
// If the web service is available
if (connection.getResponseCode() == 200) {
return true;
}
else return false;
}
This is how i have solved that problem with the help of Krishna's code
public static boolean isAvailable(String link){
boolean available = false;
URL url = null;
try {
url = new URL(link);
} catch (MalformedURLException e) {
e.printStackTrace();
}
HttpURLConnection connection = null;
try {
connection = (HttpURLConnection) url.openConnection();
} catch (IOException e1) {
}
connection.setRequestProperty("Connection", "close");
connection.setConnectTimeout(100000); // Timeout 100 seconds
try {
connection.connect();
} catch (IOException e) {
}
try {
if (connection.getResponseCode() == 200) {
// return true;
available = true;
}
else
available = false;
//return false;
} catch (IOException e) {
e.printStackTrace();
}
return available;
}

How to validate WSDL URL is up and running or not from Java?

I wanted to check WSDL url is valid or not from Java code. So that I can use another url based on that.
So I am checking the code like this.
private boolean isValidWsdlURL(String wsdlUrl)
{
UrlValidator urlValidator = new UrlValidator();
if(urlValidator.isValid(wsdlUrl))
{
return true;
}
logger.info("WSDL URL is not valid...");
return false;
}
But its alwys returning false though I have valid URL.
WSDL URL Ex: http://www.sample.com/MyWsdlService?wsdl
Because URL ends with ?wsdl
How to check the code? Looks we need only "http://www.sample.com" for UrlValidator to pass.
You're trying to validate a WSDL URL? Why not use the java.net.URL class ?
And do something like:
String urlStr = "http://www.example.com/helloService?wsdl";
URL url = null;
try {
url = new URL(urlStr);
URLConnection urlConnection = url.openConnection()
} catch (MalformedURLException ex) {
System.out.println("bad URL");
} catch (IOException ex) {
System.out.println("Failed opening connection. Perhaps WS is not up?");
}
When I inserted bad URLs like htt2p instead of http I got - "bad url" print
Try this.. Its working for me now. Thanks #zaske
public class TestWSDL {
public static void main(String args[]) {
String urlStr = "http://www.example.com:8080/helloService?wsdl";
URL url = null;
URLConnection urlConnection = null;
try {
url = new URL(urlStr);
urlConnection = url.openConnection();
if(urlConnection.getContent() != null) {
System.out.println("GOOD URL");
} else {
System.out.println("BAD URL");
}
} catch (MalformedURLException ex) {
System.out.println("bad URL");
} catch (IOException ex) {
System.out.println("Failed opening connection. Perhaps WS is not up?");
}
}
}
import org.apache.commons.validator.UrlValidator;
public class ValidateUrlExample{
public static void main(String[] args) {
UrlValidator urlValidator = new UrlValidator();
//valid URL
if (urlValidator.isValid("http://www.mkyong.com")) {
System.out.println("url is valid");
} else {
System.out.println("url is invalid");
}
//invalid URL
if (urlValidator.isValid("http://invalidURL^$&%$&^")) {
System.out.println("url is valid");
} else {
System.out.println("url is invalid");
}}
}
for more informations:
https://www.mkyong.com/java/how-to-validate-url-in-java/

Categories

Resources