1、httpclient 發送請求的步驟(流程)spring
一、建立httpclient 對象數據庫
二、建立某種鏈接方式的對象 --如 GetMethod PostMethod 等對象,構造函數中是請求地址即url,若是是get請求能夠在url後面添加請求參數apache
如: http://127.0.0.1:8080/music?id=1&name=namejson
三、 調用第一步中建立好的實例的 execute 方法來執行第二步中建立好的 method 實例,也就是這時候發送了請求數組
四、獲取服務器響應的值服務器
五、關閉鏈接,這個和 鏈接數據庫以後釋放資源同樣,執行以後要釋放鏈接資源mvc
六、處理得到的數據app
2、核心代碼:org.apache.commons.httpclient.HttpClient get請求來實現,響應返回的數據格式爲jsonobject格式框架
一、發送請求,請求成功響應數據的處理函數
// 建立 httpclient 對象
HttpClient httpclient= new HttpClient();
//建立請求方式
GetMethod getMethod = new GetMethod("http://localhost:8080/music/user/delete.do?id=1");
// 響應狀態的判斷
try{
int status= httpclient.executeMethod(getMethod);
// 200 ok 請求成功,不然請求失敗
if(status!=HttpStatus.SC_OK){
System.err.println("Method failed: "
+ getMethod.getStatusLine());
}
// 請求成功,使用string獲取響應數據
String info = null;
info = new String(getMethod.getResponseBodyAsString());
// 請求成功,使用 byte數組來獲取響應數據
byte[] responsebody= getMethod.getResponseBody();
// 編碼要和 服務端響應的一致
String response = new String(responsebody, "UTF-8");
// 響應數據格式轉化獲取jsonobject
JSONObject json = new JSONObject();
json=json.fromObject(response);
String ids=json.getString("id");
String names=json.getString("name");
}catch(HTTPException e){
e.printStackTrace();
}catch(IOException ee){
ee.printStackTrace();
}finally{
// 釋放鏈接
getMethod.releaseConnection();
}
二、接收請求,處理請求以及請求的響應
// 完整的請求路徑爲:http://localhost:8080/music/user/delete.do 這裏使用了springmvc 框架
@RequestMapping(value="/delete.do")
// 這裏的id就是請求url後面的那個請求參數,能夠有多個請求參數
public void delete(Integer id,HttpServletResponse response){
System.out.println("請求到達 delete");
Integer yid= id;
System.out.println("請求參數"+yid);
try {
// 獲取輸出流
OutputStream output= response.getOutputStream();
// 設置編碼否則會出現亂碼
response.setCharacterEncoding("UTF-8");
// 這裏傳遞一個json 格式數據,
JSONObject json= new JSONObject();
json.put("id", 10);
json.put("name", "name");
String jstring= json.toString();
// 設置編碼
byte [] b= jstring.getBytes("UTF-8");
// 響應,將相應數據返回 client
output.write(b);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
服務器響應數據格式爲 JSONArray
一、請求代碼:
// 建立 httpclient 對象
HttpClient httpclient= new HttpClient();
//建立請求方式
GetMethod getMethod = new GetMethod("http://localhost:8080/music/user/delete.do?id=1");
// 響應狀態的判斷
try{
int status= httpclient.executeMethod(getMethod);
// 200 ok 請求成功,不然請求失敗
if(status!=HttpStatus.SC_OK){
System.err.println("Method failed: "
+ getMethod.getStatusLine());
}
// 請求成功,使用string獲取響應數據
String info = null;
info = new String(getMethod.getResponseBodyAsString());
// 請求成功,使用 byte數組來獲取響應數據
byte[] responsebody= getMethod.getResponseBody();
// 編碼要和 服務端響應的一致,而這裏的response就是jsonarray 格式的字符串
String response = new String(responsebody, "UTF-8");
// 響應數據格式轉化獲取JSONArray
JSONArray array= new JSONArray();
array=array.fromObject(response);
// 獲取JSONArray 數組中的JSONObject
JSONObject json= new JSONObject();
JSONObject json1= new JSONObject();
}catch(HTTPException e){
e.printStackTrace();
}catch(IOException ee){
ee.printStackTrace();
}finally{
// 釋放鏈接
getMethod.releaseConnection();
}
二、服務器端請求處理,響應代碼
// 完整的請求路徑url=http://localhost:8080/music/user/delete.do
@RequestMapping(value="/delete.do")
public void delete(Integer id,HttpServletResponse response){
System.out.println("請求到達 delete");
// 請求參數
Integer yid= id;
System.out.println("請求參數"+yid);
try {
// 獲取輸出流
OutputStream output= response.getOutputStream();
// 編碼設置
response.setCharacterEncoding("UTF-8");
// 這裏傳遞一個json 格式數據
JSONObject json= new JSONObject();
json.put("id", 10);
json.put("name", "name");
JSONObject json1= new JSONObject();
json1.put("id", 11);
json1.put("name", "name1");
// 將jsonobject 放進jsonobject
JSONArray arr= new JSONArray();
arr.add(json);
arr.add(json1);
String jstring= arr.toString();
// 設置編碼,若是編碼方式不一樣會出現亂碼
byte [] b= jstring.getBytes("UTF-8");
// 請求響應,
output.write(b);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
org.apache.commons.httpclient.HttpClient post請求來實現,響應返回的數據格式爲jsonobject格式
get請求和post請求以後參數的傳遞不一樣,其餘的流程以及處理方式都是同樣的,因此咱們這裏只寫 多個請求參數返回JSONObject 的代碼
一、請求代碼:
// 建立 httpclient 對象
HttpClient httpclient= new HttpClient();
//建立請求方式
PostMethod postMethod = new PostMethod("http://localhost:8080/music/user/delete.do");
//參數的設置,get的請求參數在url後面可是post的請求參數在請求主體中,所以這樣寫
NameValuePair[] data = { new NameValuePair("id","110"), new NameValuePair("name", "yourName") };
// 將請求參數設置到 請求方法中
postMethod.setRequestBody(data);
// 設置請求編碼方式,其中的 charset 就設置了編碼方式。
postMethod.addRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=gb2312");
// http client 執行這個post請求方法
try{
// 服務器端返回的狀態信息
int status= httpclient.executeMethod(postMethod);
if(status!=HttpStatus.SC_OK){
System.err.println("Method failed: "
+ postMethod.getStatusLine());
}
String info = null;
// 獲取響應信息
info = new String(postMethod.getResponseBodyAsString());
System.out.println("使用commons 的get方法請求獲取的值"+info);
// 獲取響應信息
byte[] responsebody= postMethod.getResponseBody();
// 設置編碼
String response = new String(responsebody, "UTF-8");
// 若是服務器段返回的是jsonobject格式則能夠按照這種方式
JSONObject json = new JSONObject();
json=json.fromObject(response);
String ids=json.getString("id");
String names=json.getString("name");
System.out.println("id:"+ids +" name:"+names );
System.out.println("獲取內容"+responsebody.toString()+"string 類型"+response);
}catch(HTTPException e){
e.printStackTrace();
}catch(IOException ee){
ee.printStackTrace();
}finally{
// 釋放鏈接
postMethod.releaseConnection();
}
return "index";
}
二、服務器端請求處理,響應代碼
@RequestMapping(value="/delete.do")
// 接受post請求參數 id name
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");
// 這裏傳遞一個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) { e.printStackTrace(); } }