Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android如何在http頭信息裡設置參數

Android如何在http頭信息裡設置參數

編輯:關於Android編程

在使用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() + "");


  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved