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





發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章