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() + "");
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() + "");
或
[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() + "");
转载:http://blog.csdn.net/xiechengfa/article/details/42016153
技术交流QQ群:6399844
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
相关文章推荐
- 详解HttpURLConnection
- libnet入门
- XMLHttpRequest Level2
- 扒网页内容
- POJ 2135 Farm Tour (最小费用最大流)
- 数据帧和网络编址
- 浅谈浏览器http的缓存机制
- 关于TCP三次握手链接,四次握手关闭
- ed2k 网络中搜索资源并选择资源下载的分析及eMule源码梳理
- 网络基本功:TCP重传
- HttpClient——Get请求
- HttpClient——Post请求
- C#网络编程之---TCP协议的同步通信(二)
- C#网络编程之--TCP协议(一)
- tcp 状态以及三次握手
- HttpURLConnection请求
- 如何使用gson解析泛型形参并返回相对应的类
- HttpClient 四种请求访问代码 HttpGet HttpPost HttpPut HttpDelete
- TCP SYN洪泛攻击的原理及防御方法
- Okhttp使用