Java 通过httpClient Post方式提交xml,并从服务端返回数据
2016-04-08 10:18
876 查看
在通过http请求连接服务端程序时,有两种方式httpClient这个不是标准的java库,但是是开源项目,能够快捷的开发,但如果做Android的开发,推荐使用httpUrlConnect这个工具。但是httpClient确实也是一个比较好用的工具。
这里面只是做个demo,方便自己学习,也仅为大家做点参考。
客户端代码:PostXml.java,但在之前需要导入httpClient的包,可以去官网下,百度一下就能够找到,将包添加到工程里面。
finance.php
服务但代码:
在这里面有个中文编码的问题,全部设为UTF-8,一开始由于放松xml的方式中编码没有设置,默认不是UTF-8编码,导致中文一直有问题。
最后的输出结果为:
name->周成林
age->22
Image->我们
我们
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类中提供了很多方法,需要我们好好研究一波。
相关文章推荐
- HTTP响应头和请求头信息对照表
- Apache网络爬虫框架nutch安装教程
- linux网络基础
- 《网络工程师》 Part1 操作系统
- Http协议详解
- 非常好用的网络请求调试工具(chrome postman)
- http协议中:GET/POST/PUT/DELETE/INPUT/TRACE/OPTIONS/HEAD方法
- TCP协议和UDP协议的区别 (有无链接,传输速度,有序无序,可靠性,对资源的占用)
- DataSnap Server HTTP json格式修改 返回图片
- 非常简单的Python HTTP服务
- POCO库下访问http获取json串
- Linux 上重启tomcat 【转】 http://www.cnblogs.com/tovep/articles/2473147.html
- 网络通信 - TCP
- https工作原理
- 没有终结点在侦听可以接受消息的 http://192.168.1.63:8085/LoginService。这通常是由于不正确的地址或者 SOAP 操作导致的
- 网络通信 - IP
- 中转Http请求
- JAVA Socket 底层是怎样基于TCP/IP 实现的
- AndroidHttp通信 HTTP Client与HttpURLConnection的区别
- 网络层概述.虚电路网络和数据报网络