httpClient中的get和post请求

HttpClient,get请求的流程:
//创建HttpClient对象
HttpClient client=new DefaultHttpClient();
//创建HttpGet对象并传入参数url,发送get请求
String url="http://192.......";
HttpGet get=new HttpGet(url);
//处理响应
HttpResponse resp=client.execute(get):

HttpClient,post请求的流程:

//描述一个客户端
HttpClient client=new DefaultHttpClient();
String url="http://192.......";
//新建Httppost,并传入url
HttpPost post=new HttpPost(url);
//设置请求参数
List<NameValuePair>pairs=new ArrayList<NameValuePair>();
pairs.add(new BasicNameValuePair("name","张三“));
//描述实体
HttpEntity entity=new UrlEncodedFormEntity(pairs,"utf-8");
//添加请求消息头
post.setEntity(entity);
post.setHeader("Content-Type","application/x-www-form-urlencoder");
HttpResponse resp=client.execute(post);

转载于:https://my.oschina.net/yourpapa/blog/621599

猜你喜欢

转载自blog.csdn.net/weixin_34279579/article/details/92033272