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() + "")
//添加http头信息
httppost.addHeader("Authorization", "your token")//认证token
httppost.addHeader("Content-Type", "application/json")
httppost.addHeader("User-Agent", "imgfornote")
给http post传参,参考以下二个实例://serverURL url地址
HttpPost httpPost = new HttpPost(serverURL)
//param 为参数
StringEntity entity = new StringEntity(param)
entity.setContentType("application/x-www-form-urlencoded")
httpPost.setEntity(entity)
HttpResponse httpResponse = httpClient.execute(httpPost)
还可以用map作为参数
List<NameValuePair>formparams = new ArrayList<NameValuePair>()
if(param!=null){
Set set = param.keySet()
Iterator iterator = set.iterator()
while (iterator.hasNext()) {
Object key = iterator.next()
Object value = param.get(key)
formparams.add(new BasicNameValuePair(key.toString(), value.toString()))
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)