哪位大神能告诉我怎么用java封装get和post请求
哪位大神能告诉我怎么用java封装get和post请求
/**以get的方法处理接口请求调用
* @param url
* @param params
* @return
*/
public static String doGet(String url,Map<String, String> params) {
List<BasicNameValuePair> parameters = new ArrayList<BasicNameValuePair>();
// 取出所有参数
Set<String> keys = params.keySet();
// 通过循环将参数保存到lst集合
for(String name:keys) {
String value = params.get(name);
parameters.add(new BasicNameValuePair(name, value));
}
String parameter = URLEncodedUtils.format(parameters, "UTF-8");
url+="?"+ parameter;
HttpGet httpGet = new HttpGet(url);
HttpClient httpClient = HttpClients.createDefault();
String result ="";
try {
HttpResponse httpResponse = httpClient.execute(httpGet);
result =EntityUtils.toString(httpResponse.getEntity());
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
欢迎来到testingpai.com!
注册 关于