java httpclient post xml demo

jar archive: http://archive.apache.org/dist/httpcomponents/

 

 

基于httpclient 2.0 final的demo(for jdk1.5/1.6): 

http://alvinalexander.com/java/jwarehouse/commons-httpclient-2.0/src/examples/PostXML.shtml

 

基于httpclient 4.x的demo

import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.util.EntityUtils;
import org.apache.http.entity.ByteArrayEntity;

public class Test{
    public static void main(String[] args) throws Exception
    {
        post();
    }

    public static void  post() throws Exception{
        //HttpClient client = new DefaultHttpClient();
        CloseableHttpClient client = HttpClients.createDefault();
        HttpPost post = new HttpPost("http://xxx.xxx.xxx/csc/maintaining_info");
        String xml = "<xml>xxxx</xml>";
        HttpEntity entity = new ByteArrayEntity(xml.getBytes("UTF-8"));
        post.setEntity(entity);
        HttpResponse response = client.execute(post);
        String result = EntityUtils.toString(response.getEntity());
        System.out.println(result);
    }
}

 

后记:

基于历史jar包来写代码时,首先忘掉需求。

先去看对应版本的doc。 然后反过来再写!

否则很房费时间!

 

选对版本,选对文档,java也可以简单暴力!

 

来源:http://www.cnblogs.com/Tommy-Yu/p/6402751.html

posted @ 2017-02-15 18:31  tommy.yu  阅读(681)  评论(0编辑  收藏  举报