日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

java获取http状态码_java获取Json和http状态码

發布時間:2025/3/12 编程问答 19 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java获取http状态码_java获取Json和http状态码 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

最近再做接口自動化測試,其中有幾個方法比較重要

1.獲取http狀態碼

/** 返回接口狀態碼

**/

public staticString getHttpCode(String url) {

String code= null;try{

URL u= newURL(url);

URLConnection uc=u.openConnection();

HttpURLConnection huc=(HttpURLConnection)uc;

code= newInteger(huc.getResponseCode()).toString();

}catch(Exception e) {//TODO Auto-generated catch block

e.printStackTrace();

}returncode;

}

2.獲取json

/** 3個參數

**/

public staticString getJson(String base_url, String para1, String value1, String para2, String value2, String para3, String value3) {

String url= base_url + para1 + "=" + value1 + "&"

+ para2 + "=" + value2 + "&"

+ para3 + "=" +value3;

String result= "";

String code=getHttpCode(url);if(!code.startsWith("2")) {

result= "*******接口的狀態碼為:"+code+"*******"+url;

}else{

StringBuilder json= newStringBuilder();try{

URL u= newURL(url);

URLConnection uc=u.openConnection();

BufferedReader bd= new BufferedReader(new InputStreamReader(uc.getInputStream(),"UTF-8"));

String s= null;while((s=bd.readLine())!=null) {

json.append(s);

}

bd.close();

}catch(Exception e) {//TODO Auto-generated catch block

e.printStackTrace();

}

result=json.toString();

}returnresult;

}

3.獲取json中的某個值,如{"a","1"}

public staticString getJsonValue(String json, String name){

String s= null;

JSONObject jobj=JSONObject.fromObject(json);//JSONObject jobj = JSONObject.fromObject("{‘total‘:‘0‘,‘message‘:‘用戶編號不能為空‘,‘data‘:‘‘,‘code‘:‘2‘,‘rows‘:‘‘}");

Iterator it =jobj.keys();while(it.hasNext()){

String key=it.next().toString();

String value=jobj.getString(key);if(key.equals(name)) {

s=value.trim();

}

}returns;

}

4.獲取雙重json中第二維json的某個值,如{"a","1",{"b","1"}}

public staticString getJsonValue(String json,String jdate, String name){

JSONObject jobj=JSONObject.fromObject(json);

JSONObject dataList=jobj.getJSONObject(jdate);

String balance=dataList.getString(name);returnbalance;

}

5.獲取json中包含數組中的第N個json,如{"a","1","b","[{"a2","1"},{"b2","1"}]"}

/*獲取jsonArray*/

public static String getJsonArray(String json, String arr_name, intindex) {

JSONObject jobj=JSONObject.fromObject(json);

JSONArray childs=jobj.getJSONArray(arr_name);

JSONObject job=childs.getJSONObject(index);returnjob.toString();

}

6.連接數據庫

public static ListconnectSqlList(String sql,String userNum, String col) {

String url= "jdbc:mysql://172.16.30.209:3306/a_test";

String name= "gmsd";

Connection con= null;

ResultSet rs= null;

String rssql= null;

List list = new ArrayList();try{

Class.forName("com.mysql.jdbc.Driver").newInstance();

con= DriverManager.getConnection(url,name,"dlnu1234");

PreparedStatement pst=con.prepareStatement(sql);

pst.setString(1, userNum);

rs=pst.executeQuery();while(rs.next()) {

rssql=rs.getString(col);

list.add(rssql);

}

}catch(Exception e) {//TODO Auto-generated catch block

e.printStackTrace();

}finally{try{

rs.close();

con.close();

}catch(SQLException e) {//TODO Auto-generated catch block

e.printStackTrace();

}

}returnlist;

}

原文:http://www.cnblogs.com/qiaoyeye/p/4730930.html

總結

以上是生活随笔為你收集整理的java获取http状态码_java获取Json和http状态码的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。