org.apache.commons.httpclient

private static HttpMethod getPostJsonMethod(String address, String str) throws UnsupportedEncodingException {
        PostMethod post = new PostMethod(address);
        //post提交  url中带参数的例如  param_json={'companyCode':'UH7959','netKey':'admin123'}
        /*post.addRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=utf-8");
        NameValuePair[] values = new NameValuePair[1];
        values[0] = new NameValuePair("param_json", str);
        post.setRequestBody(str);
        
        
        new StringRequestEntity(str, "application/json;charse=UTF-8", "UTF8");
        StringPart[] part =  {new StringPart("param_json", str)};
        post.setRequestEntity(new MultipartRequestEntity(part, post.getParams()));
        */
        //post 提交url不带参的 
        StringRequestEntity requestEntity = new StringRequestEntity(str, "application/json;charse=UTF-8", "UTF8");
        post.setRequestEntity(requestEntity);
        return post;
    }
    public static String simplePostJsonJYZX(String address, String str) throws Exception {
        
        String response = null;
        HttpClient client = new HttpClient();
        HttpConnectionManagerParams managerParams = client.getHttpConnectionManager().getParams();
        HttpMethod method = getPostJsonMethod(address, str);// 使用POST方式提交数据
        client.setConnectionTimeout(1800000);
        client.setTimeout(1800000);
        // 设置连接超时时间(单位毫秒)
        managerParams.setConnectionTimeout(1800000);
        // 设置读数据超时时间(单位毫秒)
        managerParams.setSoTimeout(1800000);
        client.getHttpConnectionManager().setParams(managerParams);
        client.executeMethod(method);
        response = method.getResponseBodyAsString();
        method.releaseConnection();
        return response;
    }

你可能感兴趣的:(java基础)