区分不同手机平台客户端
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 aself-defined fieldin the http header) For example ,we create a field namedMobile-Agent In client side: when we construct the http request ,we add the value of header “Mobile-Agent” to be some value Android Client codeonly need to set the “Mobile-Agent” header to be ”Android”. Code example: (see myhighlighted) publicJSONObjectcallJSONWebService(Stringurl){ HttpParamsmy_httpParams=newBasicHttpParams(); HttpConnectionParams.setConnectionTimeout(my_httpParams,conn_timeout); HttpConnectionParams.setSoTimeout(my_httpParams,conn_timeout); HttpClienthttpClient=newDefaultHttpClient(my_httpParams); HttpGethttpRequest=newHttpGet(url); httpRequest.setHeader("User-Agent","bleumagent"); httpRequest.setHeader(“Mobile-Agent”,”Android”); HttpResponsehttpResponse=null; JSONObjecthttpJsonResult=null; Stringresult=null; try{ httpResponse=httpClient.execute(httpRequest); if(httpResponse.getStatusLine().getStatusCode()==200){ HttpEntityhttpEntity=httpResponse.getEntity(); if(httpEntity!=null){ InputStreaminputStream=httpEntity.getContent(); result=this.convertStreamToString(inputStream); httpJsonResult=newJSONObject(result); inputStream.close(); } } }catch(Exceptione){ System.out.println("callJsonWebServicefailed."); e.printStackTrace(); } httpClient.getConnectionManager().shutdown(); returnhttpJsonResult; } IPhoneClient codecan 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,如需转载请自行联系原作者