1. 程式人生 > >java post 請求介面

java post 請求介面

public static JSONObject doPost(String url,JSONObject json){         DefaultHttpClient client = new DefaultHttpClient();         HttpPost post = new HttpPost(url);         JSONObject response = null;         try {           StringEntity s = new StringEntity(json.toString());           s.setContentEncoding("UTF-8");           s.setContentType("application/json");//傳送json資料需要設定contentType           post.setEntity(s);           HttpResponse res = client.execute(post);           if(res.getStatusLine().getStatusCode() == HttpStatus.SC_OK){             HttpEntity entity = res.getEntity();             String result = EntityUtils.toString(res.getEntity());// 返回json格式:             response = JSONObject.fromObject(result);           }         } catch (Exception e) {           throw new RuntimeException(e);         }         return response;       }