• 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

  • 相关阅读:
    javascript Date.prototype
    Mac 安装node.js
    element-ui适配pad 遇到的问题
    GCD实现异步任务同步的两种方式
    颜色判断
    ARC下方法重复问题
    检查IDFA的方法
    mac 下安装ecplise
    注释使用
    Xcode 8.0 控制台打印问题解决办法
  • 原文地址:https://www.cnblogs.com/Tommy-Yu/p/6402751.html
Copyright © 2020-2023  润新知