Java 通过httpClient Post方式提交xml,并从服务端返回数据

在通过http请求连接服务端程序时,有两种方式httpClient这个不是标准的java库,但是是开源项目,能够快捷的开发,但如果做Android的开发,推荐使用httpUrlConnect这个工具。但是httpClient确实也是一个比较好用的工具。
这里面只是做个demo,方便自己学习,也仅为大家做点参考。

客户端代码:PostXml.java,但在之前需要导入httpClient的包,可以去官网下,百度一下就能够找到,将包添加到工程里面。

package PostPager;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.CoreProtocolPNames;
import org.apache.http.util.EntityUtils;

public class PostXml {

    static String xml = "<?xml version='1.0' encoding='UTF-8'?><group><name>周成林</name><age>22</age><Image>我们</Image></group>";

    public static void main(String args[]) {
        try {

            CloseableHttpClient httpclient = HttpClients.createDefault();
            System.out.println(xml);

            HttpPost httpPost = new HttpPost("http://119.29.85.118//finance.php");          
            httpPost.addHeader("Content-Type","text/html;charset=UTF-8");

            //解决中文乱码问题  
            StringEntity stringEntity = new StringEntity(xml,"UTF-8");
                stringEntity.setContentEncoding("UTF-8");  

            httpPost.setEntity(stringEntity);

            //CloseableHttpResponse response = httpclient.execute(httpPost);




            System.out.println("Executing request " + httpPost.getRequestLine());

        //   Create a custom response handler
            ResponseHandler<String> responseHandler = new ResponseHandler<String>() {
                @Override
                public String handleResponse(final HttpResponse response)
                        throws ClientProtocolException, IOException {//                 
                    int status = response.getStatusLine().getStatusCode();
                    if (status >= 200 && status < 300) {

                        HttpEntity entity = response.getEntity();


                        return entity != null ? EntityUtils.toString(entity) : null;
                    } else {
                        throw new ClientProtocolException(
                                "Unexpected response status: " + status);
                    }
                }
            };          
            String responseBody = httpclient.execute(httpPost, responseHandler);
            System.out.println("----------------------------------------");
            System.out.println(responseBody);

        } catch (Exception e) {
            System.out.println(e);
        }
    }
}

finance.php
服务但代码:

 <?php

         @header("Content-type: text/html; charset=utf-8");
         $file_in = file_get_contents("php://input");
         $request=simplexml_load_string($file_in);
         foreach($request->children() as $childItem) {
          //输出xml节点名称和值
                 echo $childItem->getName() . "->".$childItem."<br />";
          //其他操作省略
         }
         $xml    ="我们";
         echo $xml;
 ?>

在这里面有个中文编码的问题,全部设为UTF-8,一开始由于放松xml的方式中编码没有设置,默认不是UTF-8编码,导致中文一直有问题。

最后的输出结果为:
name->周成林
age->22
Image->我们
我们

httpClient类中提供了很多方法,需要我们好好研究一波。

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值