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
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 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
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 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 know how to check if my apps is connecting to wifi, and get XML string and parse it to my apps, but i want to know how to check if a site with xml ready to parse is already activate from the server so that i can get the XML and parse, because if that site is not activated from the server ill get the error "No Route to Host".
How to Check if that link is ready and activated from the server?
When resource was not found, your web server should set response code to 404.
If you're using HttpUrlConnection
URL url = new URL("www.example.com");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
if(conn.getResponseCode() == HttpURLConnection.HTTP_NOT_FOUND ){
handleError(); // your custom error handler
}