分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > IT知识

httppost的用法(NameValuePair(简单名称值对节点类型))

发布时间:2023-09-06 01:24责任编辑:熊小新关键词:http

一,案例一

定义了一个list,该list的数据类型是NameValuePair(简单名称值对节点类型),这个代码多处用于Java像url发送Post请求。在发送post请求时用该list来存放参数。发送请求的大致过程如下:

 1 String url="http://www.baidu.com"; 2 HttpPost httppost=new HttpPost(url); //建立HttpPost对象 3 List<NameValuePair> params=new ArrayList<NameValuePair>(); 4 //建立一个NameValuePair数组,用于存储欲传送的参数 5 params.add(new BasicNameValuePair("pwd","2544")); 6 //添加参数 7 httppost.setEntity(new UrlEncodedFormEntity(params,HTTP.UTF_8)); 8 //设置编码 9 HttpResponse response=new DefaultHttpClient().execute(httppost);10 //发送Post,并返回一个HttpResponse对象

二,案例二

 1 ?/** ?2 ?????* 获得HttpPost对象 ?3 ?????* ??4 ?????* @param url ?5 ?????* ???????????请求地址 ?6 ?????* @param params ?7 ?????* ???????????请求参数 ?8 ?????* @param encode ?9 ?????* ???????????编码方式 10 ?????* @return HttpPost对象 11 ?????* @throws UnsupportedEncodingException 12 ?????*/ ?13 ????private static HttpPost getHttpPost(String url, Map<String, String> params, ?14 ????????????String encode) throws UnsupportedEncodingException { ?15 ????????HttpPost httpPost = new HttpPost(url); ?16 ????????if (params != null) { ?17 ????????????List<NameValuePair> form = new ArrayList<NameValuePair>(); ?18 ????????????for (String name : params.keySet()) { ?19 ????????????????form.add(new BasicNameValuePair(name, params.get(name))); ?20 ????????????}21 ????????????22 ????????????UrlEncodedFormEntity entity = new UrlEncodedFormEntity(form, ?23 ????????????????????encode); ?24 ????????????httpPost.setEntity(entity); ?25 ????????}26 ????????27 ????????return httpPost; ?28 ????} ?

三,总结

httpPost其实在服务端模拟浏览器向其它接口发送服务的,一般情况下和httpclient,或者jsonp联合使用,可以把它理解为浏览器就行了,里面封装了http协议的一些东西,所以要对http协议有一定的了解。

httppost的用法(NameValuePair(简单名称值对节点类型))

原文地址:http://www.cnblogs.com/qingruihappy/p/7817958.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved