HttpClient无效的接口url仍可访问的问题

HttpClient尝试调用一个无效的接口url(故意污染了端口号),如http://192.168.1.78:18388---/api/register,是否仍然可以调通。
示例代码如下:

public static void main(String[] args) throws Exception {
    HttpPost httpPost = null;
    log.info("ready!!");
    try {
        String url = "http://192.168.1.78:18388---/api/register";
        HttpClient httpClient = HttpClients.createDefault();
        httpPost = new HttpPost(url);
        httpPost.setHeader("Content-Type", "application/json;charset=utf-8");
        httpPost.setHeader("Accept", "application/json");
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("appKey", "");
        jsonObject.put("appSecret", "");
        jsonObject.put("bspServerUrl", "");
        httpPost.setEntity(new StringEntity(JSON.toJSONString(jsonObject), "utf-8"));
        HttpResponse httpResponse = httpClient.execute(httpPost);
        HttpEntity httpEntity = httpResponse.getEntity();
        String result = "";
        if (httpEntity != null) {
            result = EntityUtils.toString(httpEntity, "utf-8");
            log.info("result: {}", result);
        }
    } catch (Exception e) {
        log.error("err: {}", e.getMessage());
    } finally {
        if (httpPost != null) {
            httpPost.releaseConnection();
        }
    }
}

运行发现可以正常调通,简单调试发现其内部会摘取出主机名和端口号,端口号之后的非数字字符将被跳过。
HttpClient无效的接口url仍可访问的问题_第1张图片

你可能感兴趣的:(J2SE,HttpClient,接口,url,端口号)