您的位置:首页 > 理论基础 > 计算机网络

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

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

1.HttpClient的设置http头的参数

[java]
view plain
copy

print?





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头的参数

[java]
view plain
copy

print?





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




[java]
view plain
copy

print?





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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: