Android如何在http头信息里设置参数

在使用http请求server时常常要传递一些参数给server,如IMEI号、平台号、渠道号、客户端的版本号等一些通用信息,像这些参数我们没有必要每次都拼在url后,我们可以统一添加到http头里。

1.HttpClient的设置http头的参数

  1. HttpClient httpclient = new DefaultHttpClient();  
  2.   
  3. httpclient.getParams().setParameter(  
  4. CoreConnectionPNames.CONNECTION_TIMEOUT, CONN_TIME_OUT);  
  5. HttpGet httpget = new HttpGet(url);  
  6. httpget.addHeader("version", SystemInfo.getVersionChars());  
  7. httpget.addHeader("client_token", SystemInfo.getIMEI());  
  8. httpget.addHeader("platform", SystemInfo.getPlatForm() + "");  
  9. httpget.addHeader("channel_id", SystemInfo.getChannelId() + "");  
	 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头的参数

  1. httpURLConnection.addRequestProperty("version",  
  2.                 SystemInfo.getVersionChars());  
  3.         httpURLConnection.addRequestProperty("client_token",  
  4.                 SystemInfo.getIMEI());  
  5.         httpURLConnection.addRequestProperty("platform",  
  6.                 SystemInfo.getPlatForm() + "");  
  7.         httpURLConnection.addRequestProperty("channel_id",  
  8.                 SystemInfo.getChannelId() + "");  
httpURLConnection.addRequestProperty("version",
				SystemInfo.getVersionChars());
		httpURLConnection.addRequestProperty("client_token",
				SystemInfo.getIMEI());
		httpURLConnection.addRequestProperty("platform",
				SystemInfo.getPlatForm() + "");
		httpURLConnection.addRequestProperty("channel_id",
				SystemInfo.getChannelId() + "");

  1. httpURLConnection.setRequestProperty("version",  
  2.             SystemInfo.getVersionChars());  
  3.     httpURLConnection.setRequestProperty("client_token",  
  4.             SystemInfo.getIMEI());  
  5.     httpURLConnection.setRequestProperty("platform",  
  6.             SystemInfo.getPlatForm() + "");  
  7.     httpURLConnection.setRequestProperty("channel_id",  
  8.             SystemInfo.getChannelId() + "");  
	httpURLConnection.setRequestProperty("version",
				SystemInfo.getVersionChars());
		httpURLConnection.setRequestProperty("client_token",
				SystemInfo.getIMEI());
		httpURLConnection.setRequestProperty("platform",
				SystemInfo.getPlatForm() + "");
		httpURLConnection.setRequestProperty("channel_id",
				SystemInfo.getChannelId() + "");


转载:http://blog.csdn.net/xiechengfa/article/details/42016153

技术交流QQ群6399844





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值