Android HTTP calls not setting Accept-Language headers? - android

I have set my Google Nexus 4 device to the Français (France) setting, and am browsing a site that is localized via the Accept-Language HTTP header from the client.
However, when I look at the Accept-Language settings arriving at the server, I see is is just en-US.
If I use the Dolphin browser to view the same site on the same device, the header comes in as fr-FR,en-US;q=0.8, and the site displays correctly in French.
The Nexus 4 is a development device, running Android 4.4.2; Nexus 4 Build/KOT49H. The version of Chrome is 32.0.1700.99.
Does anyone know how I get Chrome to honor the device's Language setting?
I am developing an Android app, and was expecting that the HTTP client org.apache.http.client.HttpClient would honor setting the Accept-LanguageHTTP header when calling my web service. However it doesn't, and I'm trying to figure out why.
The Chrome browser also exhibits this behavior, so I thought I would see if anyone else has this issue (posting my code is a little convoluted, and not really appropriate).
Do I really have to explicitly set the HTTP headers based on what I get back from Locale.getDefault().getLanguage() ?
It seems that is not the intent of being able to set the user's language via the Android settings.

So are you doing the following steps? (I'd imagine from your question that you already are, but I want to check because this works for me)
HttpClient client = new DefaultHttpClient();
HttpPost request = new HttpPost(webServiceUrl);
request.addHeader("Accept-Language", "fr");
HttpResponse response = client.execute(request);

Related

how to make a faked Android/ios http post request from desktop

Is there a way to make a faked http post request from desktop, but looks like from a real iOS/Android hardware(i.e. iPhone 5s 6.7 square inches, Samsung Galaxy 5.1-inch)?
I have no clue what’s the difference between the requests from deferent device(header,body...), how can a server know where the request from?
Thanks a lot. Merry Christmas.
The only difference is User-Agent.
You should easily be able to change it in your request, whatever the request is.
When your browser makes a request, among different headers there is a "User-Agent" header containing details of your browser and your OS. Take a look here and here.
I would use a REST Client like Postman for Chrome to send a POST Request along with the "User-Agent" string of my choice.
You can also use Chrome Developer Tools (opens with ctrl+shift+i in Windows) for the emulation feature that lets you emulate among a variety of devices. More info about that here.
With the emulation method in Chrome you can easily hit your server/webpage like you would normally by typing the URL in your browser (GET requests) from any of the available devices.

Distinguish between http mobile browser and mobile app request

I read some post like this, this and some other without response. However seems I found here the solution for IOS device.
I need to reject any http request from any app of any device and process only request from web browser, so the mainly question is:
There is a definitive way to tell if an HTTP request is made from a mobile app or from a web browser?
Thanks
Example: I receive all http request start from mobile/tablet (I'm developing application behind wifi hotspot), so i process request from Facebook App, Whatsup App and browser. I have to reject request from Facebook and Whatsup and process only request from browser. Apps User-agent seems the same of native device browser.
You can do one thing. Set custom user-agent from application side.
You can set custom user-agent in both android and iphone during web-service call request.
For Android
DefaultHttpClient httpclient = new DefaultHttpClient();
httpclient.getParams().setParameter(CoreProtocolPNames.USER_AGENT, "android");
For Iphone
NSMutableURLRequest *urlRequest = [[NSMutableURLRequest alloc]
initWithURL:[NSURL URLWithString:[yourURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]];
NSString *userAgent = "iphone";
[urlRequest setValue:userAgent forHTTPHeaderField:#"User-Agent"];
Check in request header if user-agent is "android" or "iphone" is from application and other-wise from browser.
you can check for the user-agent in the request header,
I remember mobile browsers send a different value for user-agent and desktop browsers a different one.
Just debug user-agent header and validate based on the values.

Internet Connection Problems over Cellular

We have discovered a strange bug in on of our Android apps in that the app cannot connect to the internet over cellular on certain Android Models/Mobile Networks (connecting via WiFi works perfectly). The URL which the app is trying is to reach is however accessible using the device's normal browser, which rules out the URL being blocked upstream. Any ideas what could be causing this would be appreciated please.
which rules out the URL being blocked upstream
Not necessarily. As one of the commenters pointed out, you do not indicate how you are using this URL in your app. If this is an HTTP request, and it works in a browser but not your app, try changing the User-Agent HTTP header of your request to match the one from the browser.
You have provided no error log, but from experience I have run into a few inconsistencies when connecting to a server.
Using HttpsUrlConnection a SNI header is set on HoneyComb and above but not on previous versions, which can alter how the server responds. To add to this there are some general SSL handling inconsistencies between API levels, such as handling of wildcard domains in a certificate is buggy in some api levels.
Second some phones/api levels add a header to HttpUrlConnection requests that specifies time the request was sent like so:
X-Android-Sent-Millis=1353085024868
Some servers seem to use these headers to detect mobile traffic and alter the response.
I had problems when using a dual-simcard. Do you have more devices connected? Try to shut them down.

I can't access my Apache server from my Android device

it is my second time asking a question on stackoverflow. I'm trying to access to my local apache server that I've set up on my desktop computer (ip that starts with ie. 192.168.1*.**)on my android phone. but somehow it does not allow me to access to the server at all! :( strangely, sometimes it does allow me to access to the server lol! but it's only one out of 20 for each attempts. Also, it doesn't allow me to access to the server on my laptop either.. I really need some help guys.. it was working fine when i was working locally within my desktop pc by using emulator. I've done quite a lot of research about it but i wasn't able to find any solutions.
Thank you guys
=====================================
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
HttpClient client = new DefaultHttpClient(setHttpParams());
HttpPost request = new HttpPost(PATH + FILENAME);
if (json != null) {
request.setEntity(new ByteArrayEntity(json.toString().getBytes("UTF8")));
request.setHeader("json", json.toString());
} else {
request.setEntity(new UrlEncodedFormEntity(nameValuePairs));
}
HttpResponse response = client.execute(request);
so that is the code, error occurs when it tries to execute the request. request includes the apache server ip PATH (192.168.**) + FILE_NAME(ie. index.php). I know why it causes the error because i cannot access to the 192.168.* server and read the desired file. I need help with this =(
For the computer running the webserver:
Make sure that your personal firewall rules are not preventing access to the web server.
For your phone to access the webserver over the air the server needs to be accessible from the web. It could be that your machine is behind a corporate firewall which is preventing access to it from external sources.
If you simply want to access it from the phone, make sure that both the webserver and phone are on the same network i.e. they have IP addresses from the same address range. You can do that by connecting your machine to wireless AP (either via ethernet or wireless) and making sure that your phone is connected to the same wireless AP.
Moreover I just read your question again and noticed the line
it does not allow access to to the server on my laptop either
If you cannot access the webserver locally on the machine it is runnning on then there is something wrong in the way you configured your web server.
So first make sure that your web server is running and configured properly. Open a browser and go to http://localhost or http://192.168.x.x if everything is configured right you will be able to access your web pages.
Basically this is networking 101, if it is still giving you problem post some details such as what webserver, what port etc.
If you are new to networking concepts and don't know how to configure a web server say Apache just Google how to configure Apache or use something called XAMPP which is a developer friendly bundle containing Apache, MySQL, PHP, FileZilla etc. Note that XAMPP shall never be used in a production environment as its default configuration is done in a way to make it easier to install, configure and run above mentioned packages for new users.

Set android system wide proxy settings

I am looking into writing an Android tablet app as part of a wide parental control solution.
The app should set the tablet to work with a proxy server that will be used system wide (all apps on that device will be forced to go thought the proxy server).
The proxy server job will be to filter and monitor all outgoing connections from the tablet. As part of that, only specific sites will be available while most of the apps will be blocked (including the Google play store or any other communication app installed on the device).
As an extra, I want the user to not be able to change or remove the proxy settings if it's by monitoring and changing the settings back or by blocking the user with a password.
The actual proxy server is already running and functional.
It is important the app will not require a custom ROM and/or root access
Can it be done?
By default the control app wont work without having access to the all system - means a root access- You can try to understand the principle used by Android Anti Virus software and that will help more.
By default as if now it is not possible to set Proxy for Apps ...
I had the same problem for a while. I use a library that does http requests, but I can't acces the source. So I started digging and there were two things that I needed to do. One was to add this piece of code before my own http requests:
HttpClient httpClient = new DefaultHttpClient();
if (useProxy) {
HttpHost proxy = new HttpHost("192.168.1.10", 8080, "http");
httpClient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
}
//use httpClient for a request here
But this did not solve my problem entirely. I couldn't access the source of the library that made it's own requests. I looked for more solutions and I found that you can use the following code to set a default proxy for all requests that your app makes.
System.setProperty("http.proxyHost", "192.168.1.10");
System.setProperty("http.proxyPort", "8080");
After adding this code, the requests from the library go through the proxy as well.

Categories

Resources