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?
Related
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
I'm working on an Android app that implements the Spotify API to allow the users to listen to music. I've configured the Player that Spotify has created for android devices, but it's incredibly limited in terms of its functionality, so I've had to go through Spotify's Web API to do more advanced features.
I've hit a bug when trying to get a list of the user's own playlists. I'm making a request using:
URL url = new URL("https://api.spotify.com/v1/me/playlists");
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
But instead of this command going through like it does for the other web API requests I've made, it throws the error:
java.io.FileNotFoundException: https://api.spotify.com/v1/me/playlists
at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:242)
at com.android.okhttp.internal.huc.DelegatingHttpsURLConnection.getInputStream(DelegatingHttpsURLConnection.java:210)
at com.android.okhttp.internal.huc.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:25)
at com.tmacstudios.spotifyvoice.WebWrapper$override.searchUserPlaylist(WebWrapper.java:257)
at com.tmacstudios.spotifyvoice.WebWrapper$override.access$dispatch(WebWrapper.java)
at com.tmacstudios.spotifyvoice.WebWrapper.searchUserPlaylist(WebWrapper.java:0)
at com.tmacstudios.spotifyvoice.MainActivity$6.run(MainActivity.java:382)
at java.lang.Thread.run(Thread.java:818)
I'm not entirely sure why I'm getting this error, but I think it may have to do with not having the necessary authorization to make this request. Can anyone please help me solve this problem?
After studying the documentation some more, I was able to figure out a way to solve my problem. I was in fact getting the error since I wasn't using the proper authorization.
You can get the authorization token in OnActivityResult using this code:
AuthenticationResponse response = AuthenticationClient.getResponse(resultCode, intent);
if (response.getType() == AuthenticationResponse.Type.TOKEN) {
authToken = response.getAccessToken();
Log.e("MainActivity","Auth Token: "+authToken.toString());
...
Then when making the URL request, just pass in the authorization token as a header.
URL url = new URL("https://api.spotify.com/v1/me/playlists");
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestProperty("Authorization", "Bearer " + authToken);
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
...
Also note that you must have the permissions you are using enabled as scopes when you are forming the authorization request initially.
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 tried looking for answers for this since last few days with no luck, Even some of the stackoverflow answers did not help.
I am trying to checkin a user after receiving his UserToken via Android. I get a FileNotfoundException at getInputStream(), non authenticated APIs like
"https://api.foursquare.com/v2/venues/categories" work well. Am i missing something?
URL url = new URL("https://api.foursquare.com/v2/checkins/add?oauth_token="+token);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.addRequestProperty("venueId","12238");
conn.setDoInput(true);
conn.setRequestMethod("POST");
conn.setAllowUserInteraction(true);
conn.connect();
InputStream is = conn.getInputStream();
String response = streamToString(is);
return response;
Managed to solve this after lot of effort. See my answer below.
Try adding the oauth_token using the same method you use to add the venueid. Also, your venueid is invalid, so make sure you are checking the user in to a real venue.
The HttpURLConnection class is misleading in that it will throw a FileNotFoundException for any HTTP error code of 400 or above.
So it's not necessarily an incorrect URL (404) it could be 400 (bad request), 403 (forbidden), 500 (internal server error) etc.
Use the getResponseCode method to get a more precise indication of the problem.
first: Yoy have https url, and trying to create HttpURLConnection. You should use HttpsURLConnection.
Second: You can try to add conn.setDoOutput(true), "post" request requires it. And without it server can try give a get request from you despite on conn.setRequestMethod("POST"). Also you can check headers from your browser plugin, and put them into your request.
Okie I finally managed to solve the problem, I don't know what exactly was the problem with my code above but the following worked.
This API requires a POST call but even the venueID must be part of the URL and addRequestProperty does not seem to be sending the venueID properly. Hence I changed the code to
URL url = new URL("https://api.foursquare.com/v2/checkins/add?venueId=12238&oauth_token="+token);
And this solved the problem. Thanks all