java httpclient post xml demo

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

 

 

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

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

 

基於httpclient 4.x的demopost

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);
    }
}

 

後記:spa

基於歷史jar包來寫代碼時,首先忘掉需求。code

先去看對應版本的doc。 而後反過來再寫!component

不然很房費時間!xml

 

選對版本,選對文檔,java也能夠簡單暴力!htm

 

來源:http://www.cnblogs.com/Tommy-Yu/p/6402751.htmlblog

相關文章
相關標籤/搜索