Conclusion : Unlike User-Agent ,which let the browser describe its meta data and information , the two kinds of client by default will not have a pre-defined field which can function like this.
Solution: (We created a self-defined field in the http header)
For example ,we create a field named Mobile-Agent
In client side:
when we construct the http request ,we add the value of header “Mobile-Agent” to be some value
Android Client code only need to set the “Mobile-Agent” header to be ” Android”.
Code example: (see my highlighted)
- public JSONObject callJSONWebService(String url) {
-
- HttpParams my_httpParams = new BasicHttpParams();
-
- HttpConnectionParams.setConnectionTimeout(my_httpParams, conn_timeout);
-
- HttpConnectionParams.setSoTimeout(my_httpParams, conn_timeout);
-
-
-
- HttpClient httpClient = new DefaultHttpClient(my_httpParams);
-
- HttpGet httpRequest = new HttpGet(url);
-
-
-
- httpRequest.setHeader("User-Agent","bleum agent");
-
- httpRequest.setHeader(“Mobile-Agent”,”Android”);
-
- HttpResponse httpResponse = null;
-
-
-
- JSONObject httpJsonResult = null;
-
- String result = null;
-
- try {
-
- httpResponse = httpClient.execute(httpRequest);
-
- if(httpResponse.getStatusLine().getStatusCode() == 200){
-
- HttpEntity httpEntity = httpResponse.getEntity();
-
- if(httpEntity != null){
-
- InputStream inputStream = httpEntity.getContent();
-
- result = this.convertStreamToString(inputStream);
-
- httpJsonResult = new JSONObject(result);
-
- inputStream.close();
-
- }
-
- }
-
- } catch (Exception e) {
-
- System.out.println("call JsonWebService failed.");
-
- e.printStackTrace();
-
- }
-
- httpClient.getConnectionManager().shutdown();
-
- return httpJsonResult;
-
- }
-
-
IPhone Client code can use Objective-C to do the similar thing ,just set the “Mobile-Agent” header to be “IPhone”
In server side’s servlet :
we only use the Servlet API to resolve “Mobile-Agent” field.
Then solved.
本文转自 charles_wang888 51CTO博客,原文链接:http://blog.51cto.com/supercharles888/835904,如需转载请自行联系原作者