JAVA利用HttpClient进行POST请求

论坛 期权论坛 编程之家     
选择匿名的用户   2021-6-2 21:02   5091   0

具体代码实现参考网址:

JAVA利用HttpClient进行POST请求(HTTPS)

通过

httpClient.getHostConfiguration().setHost(host, port, protocol);

来设置请求的host、端口和协议,可以是http,也可以是https。

public static JSONObject get(String url, String path, NameValuePair[] params) {
        return request(url, path, params, "get");
    }

    public static JSONObject post(String url, String path, NameValuePair[] params) {
        return request(url, path, params, "post");
    }

    private static JSONObject request(String url, String path,  NameValuePair[] params, String type) {

        JSONObject jsonObject = new JSONObject();
        try {
            URL urlObj = new URL(url);

            String protocol = urlObj.getProtocol();
            String host = urlObj.getHost();
            Integer port = urlObj.getPort();
            String urlpath = urlObj.getPath().replaceAll("//", "");

            if (protocol.equals("https") && port.equals(-1)) {
                port = 443;
            } else if (protocol.equals("http") && port.equals(-1)) {
                port = 80;
            }

            // 处理path
            Integer urlpath_len = urlpath.length();
            if (urlpath_len>0 && urlpath.substring(urlpath_len-1, urlpath_len).equals("/")) {
                urlpath = urlpath.substring(0, urlpath_len-1);
            }
            if (urlpath_len>1) {
                path = urlpath + path;
            }

            // System.out.println(protocol);
            // System.out.println(host);
            // System.out.println(port);
            // System.out.println(path);
            try {
                HttpClient httpClient = new HttpClient();
                httpClient.getHostConfiguration().setHost(host, port, protocol);
                HttpMethod method;
                String response;

                switch (type.toLowerCase()) {
                    case "get":
                        method = getMethod(path, params);
                        httpClient.executeMethod(method);
                        response = method.getResponseBodyAsString();
                        jsonObject = JSONObject.fromObject(response);
                        break;
                    case "post":
                        method = postMethod(path, params);
                        httpClient.executeMethod(method);
                        response = method.getResponseBodyAsString();
                        //System.out.println(response);
                        logger.info("This is post debug message.");
                        logger.info(response);
                        jsonObject = JSONObject.fromObject(response);
                        break;
                    default:
                        jsonObject.put("status", 500);
                        jsonObject.put("message", "非法URL");
                        break;
                }

                return jsonObject;
            } catch (IOException e) {
                e.printStackTrace();
            }

        } catch (Exception e) {
            System.out.println("Exception");
            System.out.println(e);
            jsonObject.put("message", e);
        }
        jsonObject.put("status", 500);
        return jsonObject;
    }

    private static HttpMethod getMethod(String url, NameValuePair[] params) throws IOException{
        String param_str = "";
        for(NameValuePair kv: params) {
            param_str += kv.getName()+"="+kv.getValue()+"&";
        }
        GetMethod get = new GetMethod(url+"?"+param_str);
        get.releaseConnection();
        return get;
    }

    private static HttpMethod postMethod(String url,NameValuePair[] params) throws IOException{
        PostMethod post = new PostMethod(url);
        post.setRequestHeader("Content-Type","application/x-www-form-urlencoded;charset=utf-8");
        post.setRequestBody(params);
        post.releaseConnection();
        return post;
    }

分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:3875789
帖子:775174
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP