HttpURLConnection.connect can connect to "https" but not "http" - android

I am trying to connect a http webpage using the following codes in Android system. The http webpage can not be connected, but https such as https://wwww.google.com can be successfully connected. Using getHeaderFields(), can get correct header for https://wwww.google.com, but get {} for http websites.
String link = "http://www.android.com/";
URL url = new URL(link);
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
conn.connect();

Related

Android App HTTP connection to server (Server upgrade to HTTPS)

I has an android app that AndroidManifest set to usesCleartextTraffic="true" because it is using http to connection to server.
But, now i plan to update the server to use HTTPS, and the server will redirect all http to https.
So, i want to know, if server redirect all traffic to HTTPS, will the android app that access the server using HTTP still working?
Example i am using the java code below to access server content. So will app still able to read the content at the app.php, if server update to use HTTPS? (server set to redirect all http to https)
String urls = "http://www.example.com/app.php";
URL url = new URL(urls);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestProperty("Cache-Control", "no-cache");
content = readStream(conn.getInputStream());
Thanks in advance

Uploading a file through HTTPS port using httpsurlconnection class

I have a server that only opens a port using port number 443. I need to upload a file to that server using Android client. My question is:
is it possible to connect and upload to such server using httpsurlconnection or do i need to use other class?
if it is possible, can you please show me an example of it?
thanks.
You should be able to do that simply by creating a URL with 'https' instead of 'http'.
Example from documentation:
URL url = new URL("https://www.android.com/");
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
try {
InputStream in = new BufferedInputStream(urlConnection.getInputStream());
readStream(in);
} finally {
urlConnection.disconnect();
}
Refer to the documentation for details:
https://developer.android.com/training/articles/security-ssl.html#HttpsExample
https://developer.android.com/reference/java/net/HttpURLConnection.html

Android application connect to local area Wamp Server

I need to connect my app to the wamp server located in my machine within my LAN, in order to test an api I am currently developing. While using the phone internet browser, I can access the ip address for api viewing, but using something similar to the following code does not work.
URL url = new URL("192.168.1.1/api/v1/home");
urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestMethod("GET");
Please give some suggestions in order to be able to test this.
Try adding http before the link
URL url = new URL("http://192.168.1.1/api/v1/home");
urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestMethod("GET");
Are you using the emulator? If so change your IP to this: http://10.0.2.2

Read json data from azure website

I try to read data from azure website for my android application. But when i try to reach it gives me an error.
But when i look with this website i can see json data.
I can see all json data so truely. But somehow when i enter the url my browser i can't see the data. And my url is http://tcm-test-api.azurewebsites.net/api/movie/search?term=%22Batman%22&page=1
Try setting Content-Type to application/json;charset=utf-8 in your Http request header. In android when you create the HttpUrlConnection do it as follows
URL myURL = new URL(serviceURL);
HttpURLConnection myURLConnection = (HttpURLConnection)myURL.openConnection();
myURLConnection.setRequestMethod("GET");
myURLConnection.setRequestProperty("Content-Type", "application/json;charset=utf-8");

Openssl handshake failed when using getInputStream() on Facebook profile picture in android sdk

I am trying to create user profile page for my app.
I succeed with connecting to Facebook, so i have the user basic profile using userProfile = Profile.getCurrentProfile();
Now i am trying to create a drawable of the user profile picture to put in a image view. For this I created a inputstream to fetch the image(running on asyncTask)
Bitmap x;
url = new URL("https://graph.facebook.com/939344862743414/picture?height=200&width=200");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestProperty("User-agent","Mozilla/4.0");
connection.setConnectTimeout(100000);
connection.connect();
BufferedInputStream buf = new BufferedInputStream(connection.getInputStream());
x = BitmapFactory.decodeStream(buf);
However no matter what I do,the app failed and crash when it reach the connection.getInputStream(). The error is javax.net.ssl.SSLProtocolException: SSL handshake aborted
I couldn't find any help on the web .It doesn't seem as an authorization problem because I can connect to Facebook and fetch data.
What I am doing wrong?
The issue is created by the url. As the URL contains https which means the web server certificate is issued by a well known CA, so that you can make a secure request. As you are using HttpURLConnection it doesn't support secured URL.
So you can enable the access to secured url (https://..) by using URLConnection in the place of HttpURLConnection. So modify the following line:
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
with this one:
URLConnection connection = (URLConnection) url.openConnection();
Detail information can be found here.

Categories

Resources