后台post发送XML
来源:互联网 发布:德哈维兰 知乎 编辑:程序博客网 时间:2024/05/29 12:30
后台post发送XML
这里使用了微信平台模拟请-
import java.io.BufferedReader;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import java.io.OutputStream;import java.io.OutputStreamWriter;import java.net.HttpURLConnection;import java.net.MalformedURLException;import java.net.URL;public class HttpClientAndXml {/** * @param args * @throws Exception */public static void main(String[] args) throws Exception {// TODO Auto-generated method stubString remoteUrl="https://api.mch.weixin.qq.com/pay/unifiedorder";String xmlStr="<xml>"+"<appid>1</appid>"+"<mch_id>1</mch_id>"+"<nonce_str>1</nonce_str>"+"<sign><![CDATA[1]]></sign>"+"<body><![CDATA[1]]></body>"+"<attach><![CDATA[1]]></attach>"+"<out_trade_no>1</out_trade_no>"+"<total_fee>1</total_fee>"+"<spbill_create_ip>1</spbill_create_ip>"+"<notify_url>1</notify_url>"+"<trade_type>1</trade_type>"+"<openid>1</openid>"+"</xml>";String encoding="utf-8";HttpClientAndXml hcax=new HttpClientAndXml();String relStr=hcax.sendXML(remoteUrl, xmlStr, encoding);System.out.println(relStr);}/** * post向某地址发送xml报文 * * @param remoteUrl 发送的地址 * @param reqStr 发送的xml报文 * @param encoding 发送的字符编码格式-一般写utf-8 * @return * @throws Exception */public String sendXML(String remoteUrl, String xmlStr, String encoding) throws Exception{String strResponse = ""; String strMessage = ""; InputStream inputStream; OutputStream outputStream; OutputStreamWriter writer; URL url; HttpURLConnection connection; try { url = new URL(remoteUrl); connection = (HttpURLConnection) url.openConnection(); connection.setDoInput(true); connection.setDoOutput(true); connection.setRequestMethod("POST"); connection.setAllowUserInteraction(true); //设置超时 String strTimeOut = "6000"; if (strTimeOut != null) { connection.setConnectTimeout(Integer.parseInt(strTimeOut)); connection.setReadTimeout(Integer.parseInt(strTimeOut)); } connection.connect(); outputStream = connection.getOutputStream(); writer = new OutputStreamWriter(outputStream, encoding); writer.write(xmlStr); writer.flush(); writer.close(); inputStream = connection.getInputStream(); int status = connection.getResponseCode(); if (status == 200) { BufferedReader reader; reader = new BufferedReader(new InputStreamReader(inputStream, encoding)); while ((strMessage = reader.readLine()) != null) { strResponse += strMessage; } reader.close(); } else { strResponse = "error"; } inputStream.close(); } catch (MalformedURLException e) { } catch (IOException e) { } catch (Exception e) { } return strResponse; }}
0 0
- 后台post发送XML
- post 发送xml
- POST方式发送XML数据
- C# Post接收发送XML
- Http post 请求发送 xml
- Java 后台模拟发送 POST GET 请求
- AngularJS 发送POST请求到后台
- Java后台发送Post和get请求
- php接受xml和发送(post)xml
- php接受xml和发送(post)xml
- php接受xml和发送(post)xml
- java发送xml post请求(2)
- php如何以post形式发送xml
- HttpWebRequest Post 方式发送 XML数据
- http post方式发送xml格式消息
- php如何以post形式发送xml
- C# Post接收发送XML(转载)
- HttpClient4.5.2通过POST发送XML
- 标题和图片的兼容性
- flyway初始化脚本存在默认特殊字符的问题处理
- Python——debug
- 小米push
- android 短信添加附件
- 后台post发送XML
- jquery validation 小结1
- Win10/UWP开发—凭据保险箱PasswordVault
- 本地保存图片的几种思路
- JAVA+Selenium 开发环境搭建
- HDU 1864 —— 最大报销额 01背包
- 杭电ACM5512
- IOS8 AutoLayout和size classes 详解
- iOS自定义控件的问题及解决方法