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
Related
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;
}
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?
I am making map app(Android) and i need to make on-click markers with street address in info. After few bad attempts i saw that Geocoder /get from location doesn't work anymore(returns an empty array with null pointer).
I found out here
LINK that i can do a HTTP/S request to google and i will get back JSON/XML.
My question is: how to make the HTTP request(example) when HTTP client is depricated? Can you give me an example of code to send the request and work out the response in JSON(or at least sending only)?
EDIT: I am getting this:android.os.NetworkOnMainThreadException
try {
URL url = new URL(baseUri+builder.toString());
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setReadTimeout(10000);
conn.setConnectTimeout(15000);
conn.setRequestMethod("GET");
conn.setDoInput(true);
conn.connect(); \\exception originated here
is = conn.getInputStream();
reader = new BufferedReader(new InputStreamReader(is));
for (String line = null; (line = reader.readLine()) != null;) {
builder.append(line).append("\n");
}
jsonresponse = new JSONArray(builder.toString());
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (ProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
Deprecated is only Apache HTTP client. But you can always use HttpURLConnection for HTTP requests.
There is a training material with a code example.
http://developer.android.com/training/basics/network-ops/connecting.html
The url which I give gets redirected. When i hit the url with some browser it redirects to another site. When i test it in some rest client i am getting http code 302, which is correct for redirect. But i try the same with below code, it returns 200. Can somebody help me out ?Thanks in advance.
updated url.
try {
URL url = new URL("https://securestore.hbo.com/cart.php?f=pplogin&ppx=1&method=checkout");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.connect();
int code = connection.getResponseCode();
System.out.println(connection.getResponseMessage());
System.out.println("code is" + code);
connection.disconnect();
}
catch (MalformedURLException e) {
e.printStacTrace();
}
catch (IOException e) {
e.printStackTrace();
}
If url1 is being redirected to url2, then you will get 302 status. However if you code is directally calling url2, you are bound to get 200.
Try the following code.
import javax.microedition.io.Connector;
import javax.microedition.io.HttpConnection;
.....
connection = (HttpConnection)Connector.open(url);
responseCode = connection.getResponseCode();
Try adding a response with the 302 code you want. Something like connection.setStatus(statusCode) I may be wrong though.
The HttpURLConnection will follow the redirects by default.
Try setting the following if you do not wish to follow redirects:
HttpURLConnection.setFollowRedirects(false);
For example, in relation to your code:
try {
URL url = new URL("");
HttpURLConnection.setFollowRedirects(false);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.connect();
int code = connection.getResponseCode();
System.out.println(connection.getResponseMessage());
System.out.println("code is" + code);
connection.disconnect();
}
catch (MalformedURLException e) {
e.printStacTrace();
}
catch (IOException e) {
e.printStackTrace();
}
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;
}