Requesting https using HttpURLConnection - android

I am using HttpURLConnection to request server.
Previously server was using http only protocol, while now its using https protocol.
My question is I am not using HttpsURLConnection still my code is working fine, how is it possible when I am not adding certificate still its working.
Below is my code
//url is https://.......
URL m_url = new URL(p_url);
m_httpConnection = (HttpURLConnection) m_url.openConnection();
m_httpConnection.setConnectTimeout(CONNECTION_TIMEOUT);
m_httpConnection.setReadTimeout(READ_TIMEOUT);
System.setProperty("http.keepAlive", "false");
m_httpConnection.setUseCaches(false);
m_httpConnection.setDoOutput(p_isDoOutput);
if (p_contentType != null)
{
m_httpConnection.setRequestProperty("content-type", p_contentType);
}
m_httpConnection.setRequestProperty("Accept-Encoding", "gzip,deflate,sdch");

HttpsURLConnection extends HttpURLConnection, and your connection is an instance of both. When you call openConnection() the function actually returns an HttpsURLConnection. However, because the https object extends the http one, your connection is still an instance of an HttpURLConnection. This is why your cast works. You could also cast the connection to an HttpsURLConnection without issue.
Basically think of it like this. openConnection() returns a URLConnection object that DOES NOT support the HTTP protocol. However, as you seem to be aware in your code, that URLConnection is actually an HttpURLConnection that DOES support the HTTP protocol. In the same sense your HttpURLConnection is actually an HttpsURLConnection that DOES support SSL/TLS.

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

Using HttpURLConnection or URLConnection with https domains

I'm using HttpURLConnection and URLConnection to connect to some domains on my apps to download configurations, images, etc...
Now, I must switch all the domains to https (because IOS needs it). Should my code on Android using HttpURLConnection and URLConnection work with the new https domains or it is mandatory to migrate the source code to be compatible with https domains?
When you open a https URL, a HttpsURLConnection is returned which extends HttpURLConnection. Your code should work fine, but consider handling SSL Exceptions.

Using Header and BasicHeader with HttpUrlConnection in android

In android 6.0 the org.apache.http.legacy library is deprecated, so i try to migrate a project to use HttpURLConnection.
In the project, i used the Header and the BasicHeader. I didn't found how i can replace these classes with HttpUrlConnection. I want just to know how can i add a header in a request using HttpUrlConnection.
URL url=new URL("YOURURL");
HttpURLConnection connection= (HttpURLConnection) url.openConnection();//establishing connection from url
connection.setDoOutput(true);
connection.setRequestMethod("POST");//method type
connection.setRequestProperty("Content-Type","application/json");// setting headers

Can't completely avoid redirection through HttpURLConnection

Eventhough I have set to not follow redirects:
HttpURLConnection urlConnection = url.openConnection();
urlConnection.setInstanceFollowRedirects(false);
I still can't get my POST request to work. I get response code: 302 (it means the website offers me redirection) and I think this is the reason because everything works perfectly with other websites that doesn't redirect. How to completely avoid redirection so to make my POST request to work?

Categories

Resources