四、测试公共类及http模拟客户端实现
接口请求公共类及http模拟客户端:
1、 接口请求公共类代码:
public static boolean push(String cases,String classroomId,String name, String loginToken,String questionUrl,String questionId,String correct, String courseLevelId,String answers) { String url="xxxx"; Map<String, String> headers=new HashMap<String,String>(); Map<String, String> params=new HashMap<String,String>(); params.put("classroomId", classroomId); params.put("name", name); params.put("loginToken", loginToken); JSONObject question=new JSONObject(); question.put("questionUrl", questionUrl); question.put("questionId", questionId); question.put("correct", correct); question.put("courseLevelId", courseLevelId); question.put("answers", answers); params.put("question", question.toString()); String responseContent = PublicClient.getInstance().sendHttpGet(url, params,headers); System.out.println(cases+"result:" + responseContent); JSONObject js=JSONObject.fromObject(responseContent); String rscode=js.getString("code"); System.out.println(rscode); //判断返回值 if (rscode.equals("10001")) return false; if (rscode.equals("10101")) return false; if (rscode.equals("10002")) return false; if (rscode.equals("10103")) return false; if (rscode.equals("10102")) return false; if (rscode.equals("10104")) return false; if (rscode.equals("11001")) return false; if (rscode.equals("11002")) return false; if (rscode.equals("999999")) return false; return true; } |
2、 模拟客户端代码:
public String sendHttpGet(String httpUrl,Map<String, String> params,Map<String, String> Headers) { // StringBuffer param = new StringBuffer(); // int i = 0; if(params != null && !params.isEmpty()){ List<NameValuePair> pairs = new ArrayList<NameValuePair>(params.size()); for(Map.Entry<String,String> entry : params.entrySet()){ String value = entry.getValue(); if(value != null){ pairs.add(new BasicNameValuePair(entry.getKey(),value)); }else { pairs.add(new BasicNameValuePair(entry.getKey(),"")); } } try { httpUrl += "?" + EntityUtils.toString(new UrlEncodedFormEntity(pairs, "UTF-8")); } catch (ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (UnsupportedEncodingException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } HttpGet httpGet = new HttpGet(httpUrl); if (Headers != null && !Headers.isEmpty()) { for (Entry<String, String> entry : Headers.entrySet()) { // 排除掉空值 if (entry.getValue() != null) { httpGet.addHeader(entry.getKey(), entry.getValue() .toString()); } else { httpGet.setHeader(entry.getKey(), ""); } } } return sendHttpGet(httpGet); } /** * 发送Get请求 * @param httpGet * @return */ private String sendHttpGet(HttpGet httpGet) { CloseableHttpClient httpClient = null; CloseableHttpResponse response = null; HttpEntity entity = null; String responseContent = null; try { // 创建默认的httpClient实例. httpClient = HttpClients.createDefault(); httpGet.setConfig(requestConfig); // 执行请求 response = httpClient.execute(httpGet); entity = response.getEntity(); responseContent = EntityUtils.toString(entity, "UTF-8"); } catch (Exception e) { e.printStackTrace(); } finally { try { // 关闭连接,释放资源 if (response != null) { response.close(); } if (httpClient != null) { httpClient.close(); } } catch (IOException e) { e.printStackTrace(); } } return responseContent; } |