在java中如何实现http/post/xml发送数据报文?麻烦高手赐教!
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了在java中如何实现http/post/xml发送数据报文?麻烦高手赐教!相关的知识,希望对你有一定的参考价值。
合作商提供的接口规则和形式如下:
接口通过Http + Post + XML 的形式,通过Http做为协议,POST做为传输方式,XML作为传输数据格式。
谢谢您的问答,请问有具体的事例代码吗?
追答现在没有。晚上回去我找找给你贴一个。
本回答被提问者采纳Java URLConnection实现HTTP的GET/POST
public static String doGet(String url, String param) { String result = ""; BufferedReader in = null; try { String urlNameString = url + "?" + param; URL realUrl = new URL(urlNameString); URLConnection connection = realUrl.openConnection(); connection.setRequestProperty("accept", "*/*"); connection.setRequestProperty("connection", "Keep-Alive"); connection.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)"); connection.connect(); in = new BufferedReader(new InputStreamReader(connection.getInputStream())); String line; while ((line = in.readLine()) != null) { result += line; } } catch (Exception e) { try {in.close();} catch (Exception ex) {} return null; } try {in.close();} catch (Exception e) {} return result; } public static String doPost(String url, String param) { PrintWriter out = null; BufferedReader in = null; String result = ""; try { URL realUrl = new URL(url); URLConnection conn = realUrl.openConnection(); conn.setRequestProperty("accept", "*/*"); conn.setRequestProperty("connection", "Keep-Alive"); conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)"); conn.setDoOutput(true); conn.setDoInput(true); out = new PrintWriter(conn.getOutputStream()); out.print(param); out.flush(); in = new BufferedReader(new InputStreamReader(conn.getInputStream())); String line; while ((line = in.readLine()) != null) { result += line; } } catch (Exception e) { try{out.close();}catch(Exception ex){} try{in.close();}catch(Exception ex){} return null; } try{out.close();}catch(Exception ex){} try{in.close();}catch(Exception ex){} return result; }
以上是关于在java中如何实现http/post/xml发送数据报文?麻烦高手赐教!的主要内容,如果未能解决你的问题,请参考以下文章
Java实现一个HTTP基于xml的post请求 如何在struts2 action获取
PHP 如何保存/打印 整个Http请求头和附带的post数据?