Https get call results in 400 Bad Request in android - android

So i have a webpage which i trying to access via a HttpsURLConnection. The certificate issued is by Digicert CA-3.However I always get a response as 400.The android developer website, wikipedia example works fine.This website also opens on chrome on the device.I want to know if this could a be a problem with how I am invoking the call.or what could be the issue
URL url = new URL(strUrl);
HttpsURLConnection urlConnection = (HttpsURLConnection) url
.openConnection();
SSLContext sslcontext = SSLContext.getDefault();
CookieSyncManager.createInstance(context);
CookieManager cookieManager = CookieManager.getInstance();
cookieManager.removeAllCookie();
urlConnection.setDoOutput(false);
urlConnection.setDoInput(true);
urlConnection.setSSLSocketFactory(sslcontext.getSocketFactory());
int code = urlConnection.getResponseCode(); //always 400 :(

Probably you should double check the URL you are using.. It is mostly because of client's Bad Request..

Related

Android - Error 403 when using Clearbit logo API

I have been trying to implement the retrieval of a company logo using Clearbits.Logo.
url = new URL(Constants.URL_PREFIX_LOGO + shop.getDomainName());
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setDoOutput(true);
httpURLConnection.connect();
int responseCode = httpURLConnection.getResponseCode();
if (responseCode == HttpURLConnection.HTTP_FORBIDDEN) {
//Download picture from URL
..
}
However, for a reason I simply cannot understand, connect() always returns a 403 error with no further explanations.
The device is connected to the internet and has a permission to access it, the URL is exact, and the most bizarre of all, the URL can be accessed through the device browser without any error.
The URL doesn't require any authentification
https://logo.clearbit.com/carrefour.eu?size=600
I can't find the reason why this 403 keeps occuring ..
has anyone an idea ?

Does HttpURLConnection work for sdk22?

Does HttpURLConnection that works for android API 23 also work for android API22?
I'm trying to program for android version over 4.0.
I am unsure if the difference between sdk22 and sdk23 will cause a problem
// http client
URL url = new URL(url1);
HttpURLConnection httpClient = (HttpURLConnection) url.openConnection();
httpClient.setRequestMethod("POST");
httpClient.setUseCaches(false);
httpClient.setDoInput(true);
httpClient.setDoOutput(true);
httpClient.setRequestProperty("Connection", "Keep-Alive");
httpClient.addRequestProperty("Content-length", reqEntity.getContentLength() + "");
OutputStream os = httpClient.getOutputStream();
reqEntity.writeTo(httpClient.getOutputStream());
os.close();
httpClient.connect();
if (httpClient.getResponseCode() == HttpURLConnection.HTTP_OK) {
return readStream(httpClient.getInputStream());
Yes it will work without any issues, and the difference will not cause any problem
HttpUrlConnection is available in from API Level 1 , you can further get more insight from the docs.
Its a good practice to use HttpUrlConnection over deprecated Apache HttpClient classes,

HttpsUrlConnection Does not Support SetRequestMethod?

I wrote a code to establish an HttpsUrlConnection and setRequestMethod as PUT. While debugging I see the method as GET. My SetRequestMethod is not working. I don't know why HttpsUrlConnection method in default GET.
My code looks like this
DisbleSSL disble = new DisbleSSL();
disble.disableSSLVerification();
URL url = new URL(url_string);
httpsUrlConnection = (HttpsURLConnection) url.openConnection();
httpsUrlConnection.setDoOutput(true);
httpsUrlConnection.setRequestMethod("PUT");
httpsUrlConnection.connect();
HttpsUrlConnection definitely support setrequestMethod.Actually but it may be Wrong Labled in Debugg console.
enter image description here
HttpsURLConnection use internal objects ->
protected DelegateHttpsURLConnection delegate
Try setRequestMethod before setDoOutput

Hide the URL when using HttpURLConnection

I have the following code:
urlString = "..."
URL url = new URL(urlString);
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
How can I make urlString secure and still working? By secure I mean from viewing the APK and seeing the url itself, or is it a lost cause because any simple sniffer will know the URL anyway? if so, what can be done about it?

HttpURLConnection with server redirections

i'm trying to get the source code from a site using this code
conn = (HttpURLConnection)url.openConnection();
conn.setReadTimeout(10000);
conn.setConnectTimeout(20000);
conn.setRequestMethod("GET");
conn.setDoInput(true);
conn.addRequestProperty("User-Agent", "Mozilla/5.0");
conn.setRequestProperty("Accept-Encoding", "gzip, deflate");
int response = conn.getResponseCode();
if (response = 307){
String locationHeader = conn.getHeaderField("Location");
URL redirectURL = new URL(locationHeader);
...
}
when the server responds with a 307 code i create a new connection with the same parameters as above with the new url given by the server.
this code works fine while following the first 2 redirects, at the third the server gives a relative url that forces a MalformedURLException when my code executes ' new URL(locationHeader); '.
so i tried to fix that adding the ' http://servername/ ' to the ' locationHeader ' string but doing that creates a loop cause the server then redirects to the first url of his redirection chain.
since my browser gets the source code from that server with no problems is there a way to achieve that with HttpURLConnection?
if someone is interested thanks to Fiddler i worked out a solution to this issue.
first i changed the "User-Agent" property to mimic the one of Mozilla then i manually tweaked the cookie the serer was sending in its reply with the relative path.
that did the trick. thank you Fiddler.

Categories

Resources