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
Related
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();
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
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
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.
I'm trying to set the access token with all of my network calls in my Android app.
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestProperty("Content-Type", "application/json");
conn.setRequestProperty("Authorization", "Token token="
+ accessToken);
conn.setRequestMethod("GET");
With this code, however, the header that our server is seeing is "HTTP_AUTHORIZATION" - not "AUTHORIZATION". After pouring over javadocs, I can't figure out how to not get it to append the "HTTP_".
Thanks!
You can't. The mapping of Authorization to HTTP_AUTHORIZATION is part of the CGI Specification and happens on the server.