您的位置:首页 > 理论基础 > 计算机网络

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

2016-04-08 10:18 876 查看
在通过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类中提供了很多方法,需要我们好好研究一波。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: