首页 > 代码库 > Android如何在http头信息里设置参数
Android如何在http头信息里设置参数
在使用http请求server时常常要传递一些参数给server,如IMEI号、平台号、渠道号、客户端的版本号等一些通用信息,像这些参数我们没有必要每次都拼在url后,我们可以统一添加到http头里。
1.HttpClient的设置http头的参数
HttpClient httpclient = new DefaultHttpClient(); httpclient.getParams().setParameter( CoreConnectionPNames.CONNECTION_TIMEOUT, CONN_TIME_OUT); HttpGet httpget = new HttpGet(url); httpget.addHeader("version", SystemInfo.getVersionChars()); httpget.addHeader("client_token", SystemInfo.getIMEI()); httpget.addHeader("platform", SystemInfo.getPlatForm() + ""); httpget.addHeader("channel_id", SystemInfo.getChannelId() + "");
2.HttpURLConnection的设置http头的参数
httpURLConnection.addRequestProperty("version", SystemInfo.getVersionChars()); httpURLConnection.addRequestProperty("client_token", SystemInfo.getIMEI()); httpURLConnection.addRequestProperty("platform", SystemInfo.getPlatForm() + ""); httpURLConnection.addRequestProperty("channel_id", SystemInfo.getChannelId() + "");
或
httpURLConnection.setRequestProperty("version", SystemInfo.getVersionChars()); httpURLConnection.setRequestProperty("client_token", SystemInfo.getIMEI()); httpURLConnection.setRequestProperty("platform", SystemInfo.getPlatForm() + ""); httpURLConnection.setRequestProperty("channel_id", SystemInfo.getChannelId() + "");
Android如何在http头信息里设置参数
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。