post代码

public static String httpPost(String url, String input) throws Exception {
        // 返回body
        String body = "";
        // 1、创建一个htt客户端
        HttpClient httpClient = new DefaultHttpClient();
        // 2、创建一个HttpPost请求
        HttpPost post = new HttpPost(url);

        post.addHeader("Content-Type", "application/json;charset=UTF-8");
        post.setHeader("Accept", "application/json");
        // 设置参数
        StringEntity stringEntity = new StringEntity(input,Charset.forName("UTF-8"));
        post.setEntity(stringEntity);
        // 7、执行post请求操作,并拿到结果
        HttpResponse httpResponse = httpClient.execute(post);
        // 获取结果实体
        HttpEntity entity = httpResponse.getEntity();
        if (entity != null) {
            // 按指定编码转换结果实体为String类型
            body = EntityUtils.toString(entity, HTTP.UTF_8);
        }
        // EntityUtils.consume(entity);
        httpClient.getConnectionManager().shutdown();
        System.out.println(body.length());
        return body;
    }
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容