Using HttpURLConnection or URLConnection with https domains - android

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.

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

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

Requesting https using HttpURLConnection

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.

Use HttpUrlConnection with volley

I read everywhere that volley will use either HttpUrlConnection for newer versions api or HttpClient for older version and I'm trying to tell volley to only use the HttpUrlConnection.
My main goal is to setup volley to execute requests using a cookie I have in store, and to do that I know I need to set the HttpUrlConnection with the cookie and then pass it to volley to use as the default implementation.
So far so good but I have no idea how to start HttpUrlConnection and add the cookie to it.
Can someone please give me a small example of how to initialise HttpUrlConnection and add a cookie to it and then pass it on to volley?
I was able to perform such a request on HttpUrlConnection itself and it worked but how do I set it up to use with volley?
URL urlLink = new URL(url2);
HttpURLConnection conenction = (HttpURLConnection)urlLink.openConnection();
conenction.setRequestProperty("Cookie", cookie);
I read everywhere that volley will use either HttpUrlConnection for newer versions api or HttpClient for older version and I'm trying to tell volley to only use the HttpUrlConnection.
This is correct. See lines 54-60 of the Volley source. If your app is running on a device using Gingerbread (API level 9) or higher, it is already using HttpUrlConnection for all requests.
If you really want to use your own HttpUrlConnection instance for your requests, you will need to implement your own HttpStack (see Volley's HurlStack for an example). You can use Volley# newRequestQueue(Context, HttpStack) to tell Volley to use your custom stack.
There are a number of alternatives for sending cookies however. I recommend checking out this question for some of those alternatives.

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