zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    centos 7 服务器网卡做bond
    python 函数参数
    python 文件操作
    Linux 150命令之 文件和目录操作命令 cd pwd cp mv touch
    创建https证书
    Linux 150命令之 文件和目录操作命令 ls
    zabbix 2.2.2 安装部署
    NTP错误总结
    NTP时间服务器
    150命令之线上查询及帮助命令 man hellp
  • 原文地址:https://www.cnblogs.com/Tommy-Yu/p/6402751.html
Copyright © 2011-2022 走看看