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
Related
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
Warnings for HttpClient,HttpGet().
The code is running but it is not getting any response from the Esp8266(I have used Esp8266 as a server in my application).
Is it related to the warnings of HttpClient or HttpGet() as shown in the image or related to the Esp8266 coding?
Please guide!
HTTPClient is deprecated since API 22. You can try using HttpURLConnection instead.
URL url = new URL(urlString);
HttpURLConnection connection = (HttpURLConnection)url.openConnection();
connection.setRequestProperty("User-Agent", "");
connection.setRequestMethod("POST");
connection.setDoInput(true);
connection.connect();
More info in this answer : https://stackoverflow.com/a/29889139/5040796
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.
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.
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.