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