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

org.apache.http.client.HttpClient get/post请求

2015-04-14 00:00 204 查看
摘要: org.apache.http.client.HttpClient get post请求实例,请求响应JSONObject 格式数据

请求步骤

1、创建httpclient 对象

2、创建 请求方式,构造函数的值为请求路径

3、调用1中对象的execute() 方法,参数为 2 的对象

4、获取请求响应数据

5、释放连接资源

6、处理数据

一、使用org.apache.http.client.HttpClient 的get请求来实现

1、请求核心代码:

// 创建 httpclient 对象
HttpClient httpclient = new DefaultHttpClient();
//创建请求方式,因为是get请求,所以可以在url后面连接请求参数
HttpGet httpget= new HttpGet("http://localhost:8080/music/user/delete.do?id=110&name=name");
try{
// 执行请求,获取响应数据
HttpResponse response = httpclient.execute(httpget);
// 获取请求实体,
HttpEntity entity = response.getEntity();
// 获取返回实例的内容的长度
long len= entity.getContentLength();
// 获取 content type
Header head= entity.getContentType();
String bodys= head.toString();
System.out.println("内容长度"+len +"head 内容"+bodys);
//实例流的获取
if(entity != null){
InputStream input= entity.getContent();
byte []buffer= new byte[2048];
input.read(buffer);
String body= new String(buffer,"utf-8");
System.out.println("获取资源"+body);
JSONObject json= new JSONObject();
json=JSONObject.fromObject(body);
String ids= json.getString("id");
String names=json.getString("name");
System.out.print(ids);
System.out.print(names);
}
}catch(HTTPException e){
e.printStackTrace();
}catch(IOException ee){
ee.printStackTrace();
}finally{
httpget.releaseConnection();
}

2、服务器端响应请求核心代码:

请求完整路径 url=http://localhost:8080/music/user/delete.do?

@RequestMapping(value="/delete.do")
public void delete(String id,String name,HttpServletResponse response){
System.out.println("请求到达 delete");
String yid= id;
String yname=name;
System.out.println("请求参数"+yid);
System.out.println("请求参数name:"+name);
try {
OutputStream output= response.getOutputStream();

// 编码的设置
response.setCharacterEncoding("UTF-8");

// 主题类型的设置
response.setContentType("text/html");
// 这里传递一个json 格式数据
JSONObject json= new JSONObject();
json.put("id", 10);

7fe0
json.put("name", "name");
String jstring= json.toString();

// 编码设置
byte [] b= jstring.getBytes("UTF-8");

// 响应数据输出
output.write(b);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

二、使用org.apache.http.client.HttpClient 的get请求来实现,自己拼凑url 以及多个请求数据

1、请求发送核心代码

// 创建 httpclient 对象 这里使用httpclient
HttpClient httpclient = new DefaultHttpClient();
// 创建请求方法对象
HttpGet httpget = new HttpGet();
try{
//请求参数对象的创建以及参数的设置
List<NameValuePair> qparams = new ArrayList<NameValuePair>();
qparams.add(new BasicNameValuePair("id", "110110"));
qparams.add(new BasicNameValuePair("name", "you name"));
qparams.add(new BasicNameValuePair("age", "10"));
qparams.add(new BasicNameValuePair("sex", "man"));
// url的拼装,以及请求参数编码的设置
java.net.URI uri = URIUtils.createURI("http", "localhost:8080/music/user", -1, "/delete.do",
URLEncodedUtils.format(qparams, "UTF-8"), null);
// 给请求路径设置值,据访问路径
httpget.setURI(uri);
System.out.println(httpget.getURI());
// 执行请求,获取响应数据
HttpResponse response = httpclient.execute(httpget);
// 获取请求实体,
HttpEntity entity = response.getEntity();
// 获取返回实例的内容的长度
long len= entity.getContentLength();
// 获取 content type
Header head= entity.getContentType();
String bodys= head.toString();
System.out.println("内容长度:"+len +" head 内容:"+bodys);
//实例流的获取
if(entity != null){
InputStream input= entity.getContent();
byte []buffer= new byte[2048];
input.read(buffer);
String body= new String(buffer,"utf-8");
System.out.println("获取资源"+body);
JSONObject json= new JSONObject();
json=JSONObject.fromObject(body);
String ids= json.getString("id");
String names=json.getString("name");
System.out.print(ids);
System.out.print(names);
}
}catch(HTTPException e){
e.printStackTrace();
}catch(IOException ee){
ee.printStackTrace();
}catch(Exception eee){
eee.printStackTrace();
}finally{
httpget.releaseConnection();
}

2、接收请求,响应请求核心代码

请求完整路径:url=http://localhost:8080/music/user/delete.do

@RequestMapping(value="/delete.do")
public void delete(String id,String name,HttpServletResponse response){
System.out.println("请求到达 delete");
String yid= id;
String yname=name;
System.out.println("请求参数"+yid);
System.out.println("请求参数name:"+name);
try {
OutputStream output= response.getOutputStream();

// 输出内容编码的设置
response.setCharacterEncoding("UTF-8");
response.setContentType("text/html");
// 这里传递一个json 格式数据
JSONObject json= new JSONObject();
json.put("id", 10);
json.put("name", "name");
String jstring= json.toString();

// 编码的设置
byte [] b= jstring.getBytes("UTF-8");
output.write(b);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

三、使用org.apache.http.client.HttpClient 的post请求来实现

1、请求端核心代码

// 创建 httpclient 对象 这里使用httpclient
HttpClient httpclient = new DefaultHttpClient();
// 请求参数的组装
List<NameValuePair> qparams = new ArrayList<NameValuePair>();
qparams.add(new BasicNameValuePair("id", "110110"));
qparams.add(new BasicNameValuePair("name", "you name"));
qparams.add(new BasicNameValuePair("age", "10"));
qparams.add(new BasicNameValuePair("sex", "man"));
try{

// UrlEncodedFormEntity 实例将会使用URL编码来编码参数

UrlEncodedFormEntity entity = new UrlEncodedFormEntity(qparams,"UTF-8");
HttpPost httppost= new HttpPost("http://localhost:8080/music/user/delete.do");
// 请求参数的设置
httppost.setEntity(entity);
// 请求执行,以及响应数据的获取
HttpResponse response = httpclient.execute(httppost);

// 获取请求响应
HttpEntity entitys = response.getEntity();

// 响应内容的长度
long len= entity.getContentLength();

// 响应内容的类型
Header head= entity.getContentType();
String bodys= head.toString();
System.out.println("内容长度:"+len +" head 内容:"+bodys);
if(entitys != null){

// 获取响应数据
InputStream input= entitys.getContent();
byte []buffer= new byte[2048];
input.read(buffer);

// 编码设置
String body= new String(buffer,"utf-8");
System.out.println("获取资源"+body);
}
}catch(Exception e){
e.printStackTrace();
}

2、接收请求,响应请求核心代码

请求完整路径: url=http://localhost:8080/music/user/delete.do

@RequestMapping(value="/delete.do")
public void delete(String id,String name,String age,HttpServletResponse response){
System.out.println("请求到达 delete");
String yid= id;
String yname=name;
String ages=age;
System.out.println("请求参数"+yid);
System.out.println("请求参数name:"+name);
System.out.println("请求参数age:"+ages);
try {
OutputStream output= response.getOutputStream();

//响应内容编码的设置
response.setCharacterEncoding("UTF-8");
response.setContentType("text/html");
// 这里传递一个json 格式数据
JSONObject json= new JSONObject();
json.put("id", 10);
json.put("name", "name");
String jstring= json.toString();
byte [] b= jstring.getBytes("UTF-8");
output.write(b);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: