Java发送Http请求的两种方式HttpClient(方式二)

来源:互联网 发布:数据库医院管理系统 编辑:程序博客网 时间:2024/05/14 09:42


HttpClient方法:

private static void httpClientPost() {
    HttpClient client = new DefaultHttpClient();
    HttpPost post = new HttpPost(TEST_URL);

    try {
        ContentProducer cp = new ContentProducer() {
            public void writeTo(OutputStream outstream) throws IOException {
                Writer writer = new OutputStreamWriter(outstream, "UTF-8");
                writer.write("");
                writer.flush();
            }
        };

        post.setEntity(new EntityTemplate(cp));
        HttpResponse response = client.execute(post);
   
        System.out.println(EntityUtils.toString(response.getEntity()));
    } catch (ClientProtocolException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

0 0
原创粉丝点击