HttpUtil

Http常用调用方式:get 和 post

get调用:

public static String doGet(String url, Map param) {

		// 创建Httpclient对象
		CloseableHttpClient httpclient = HttpClients.createDefault();

		String resultString = "";
		CloseableHttpResponse response = null;
		try {
			// 创建uri
			URIBuilder builder = new URIBuilder(url);
			if (param != null) {
				for (String key : param.keySet()) {
					builder.addParameter(key, param.get(key));
				}
			}
			URI uri = builder.build();

			// 创建http GET请求
			HttpGet httpGet = new HttpGet(uri);

			// 执行请求
			response = httpclient.execute(httpGet);
			// 判断返回状态是否为200
			if (response.getStatusLine().getStatusCode() == 200) {
				resultString = EntityUtils.toString(response.getEntity(), "UTF-8");
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if (response != null) {
					response.close();
				}
				httpclient.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return resultString;
	}

post调用方式--Content-type:

application/json,plain/text,application/x-www-form-urlencoded,

application/json调用:

public static String callJson(String url,String param) throws IOException {

        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost(url);
        // 设置请求的header
        httppost.addHeader("Content-type", "application/json");

        // 设置请求的参数
        StringEntity entity = new StringEntity(param, "utf-8");
        httppost.setEntity(entity);

        HttpResponse response = httpclient.execute(httppost);
        if (response.getStatusLine().getStatusCode() == 200) {
		                        /*读返回数据*/
            String conResult = EntityUtils.toString(response.getEntity());
            if(conResult!=null && !conResult.equals("[]") && StringUtils.isNotBlank(conResult) && StringUtils.isNotBlank(conResult)){
                return conResult;
            }else{
                return "返回数据为空";
            }
        } else {
            String err = response.getStatusLine().getStatusCode()+"";
            return "发送失败"+err;
        }
    }

plain/text调用:

public static String callPlainText(String url,String param) throws IOException {

        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost(url);
        // 设置请求的header
        httppost.addHeader("Content-type", "plain/text");

        // 设置请求的参数
        StringEntity entity = new StringEntity(param, "utf-8");
        httppost.setEntity(entity);

        HttpResponse response = httpclient.execute(httppost);
        if (response.getStatusLine().getStatusCode() == 200) {
		                        /*读返回数据*/
            String conResult = EntityUtils.toString(response.getEntity());
            if(conResult!=null && !conResult.equals("[]") && StringUtils.isNotBlank(conResult) && StringUtils.isNotBlank(conResult)){
                return conResult;
            }else{
                return "返回数据为空";
            }
        } else {
            String err = response.getStatusLine().getStatusCode()+"";
            return "发送失败"+err;
        }
    }

application/x-www-form-urlencoded调用:

public static String callFormData(String url,Map param) throws IOException {

        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost(url);
        // 设置请求的header
        httppost.addHeader("Content-type", "application/x-www-form-urlencoded");

        // 设置请求的参数
        List nameValuePairs = new ArrayList();
        for (String key : param.keySet()) {
            nameValuePairs.add(new BasicNameValuePair(key, param.get(key))); //请求接口的参数
        }

        StringEntity entity = new UrlEncodedFormEntity(nameValuePairs, "UTF-8");
        httppost.setEntity(entity);

        HttpResponse response = httpclient.execute(httppost);
        if (response.getStatusLine().getStatusCode() == 200) {
		                        /*读返回数据*/
            String conResult = EntityUtils.toString(response.getEntity());
            if(conResult!=null && !conResult.equals("[]") && StringUtils.isNotBlank(conResult) && StringUtils.isNotBlank(conResult)){
                return conResult;
            }else{
                return "返回数据为空";
            }
        } else {
            String err = response.getStatusLine().getStatusCode()+"";
            return "发送失败"+err;
        }
    }

 

 

 

 

 

你可能感兴趣的:(优雅代码,java)