用http请求方式调用sap接口,以xml格式数据交换


 

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;

import org.apache.commons.net.util.Base64;

import com.alibaba.fastjson.JSONObject;
import com.egzosn.pay.common.util.XML;

/**
* <p>Title: Test5</p>
* <p>Description: </p>
* @author xujunsuo
* @date 2018年5月8日
*/
public class HttpConnSap {

public static void main(String[] args) throws IOException {
// 创建指定链接的url对象
URL url = new URL("http://xxx.xxx.xxx.xxx:xxxx/XISOAPAdapter/MessageServlet?senderParty=&senderService=xxxxxx&receiverParty=&receiverService=&interface=xxxxxxx&interfaceNamespace=urn:xxxxc:xxx");

// 建立到url对象之间的链接
HttpURLConnection con = (HttpURLConnection) url.openConnection();
//设置用户名和密码
String user = "xxxx";
String pwd = "xxxxx";
String auth = user+":"+pwd;
//对其进行加密
byte[] rel = Base64.encodeBase64(auth.getBytes());
String res = new String(rel);
//设置认证属性
con.setRequestProperty("Authorization","Basic " + res);
// 如果打算使用 URL 连接进行输出,则将 DoOutput 标志设置为 true
con.setDoOutput(true);
// 设置第一次请求的数据内容不被存储
// con.setRequestProperty("Pragma:", "no-cache");
// 设置请求的数据内容不被存储
con.setRequestProperty("Cache-Control", "no-cache");
// 设置请求的字符集编码格式
con.setRequestProperty("Content-Type", "application/xml;charset=utf-8");
con.setRequestMethod("POST");
// 构造向指定链接写入数据的的输出流
OutputStreamWriter out = new OutputStreamWriter(con.getOutputStream());

// 获取xml数据
String xmlinfo1="<SOAP-ENV:Envelope xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:SOAP-ENC=\"http://schemas.xmlsoap.org/soap/encoding/\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"><SOAP-ENV:Body>";
String xmlinfo2="<m:ZSDIF0061_PROXY xmlns:m=\"urn:sap-com:document:sap:rfc:functions\"><CT_CREDIT><item><KUNNR>2</KUNNR><SKFOR>1</SKFOR><SSOBL>1</SSOBL><OEIKW>0</OEIKW><OLIKW>1</OLIKW><OFAKW>11</OFAKW><OBLIG>12</OBLIG></item></CT_CREDIT></m:ZSDIF0061_PROXY></SOAP-ENV:Body></SOAP-ENV:Envelope>";
String xmlInfo =xmlinfo1+xmlinfo2 ;
System.out.println("【请求SAP报文】" + xmlInfo);
// 向指定链接写入数据
out.write(new String(xmlInfo));
out.flush();
out.close();

// 将从服务端返回的数据读取到内存中
BufferedReader br = new BufferedReader(new InputStreamReader(con.getInputStream()));

String line = "";


// 构造一个空的StringBuffer对象,用于存储内存中的数据
StringBuffer buf = new StringBuffer();
for (line = br.readLine(); line != null; line = br.readLine())
{
// 由于服务端返回的数据的字符集编码有可能不是UTF-8,需要对返回的数据通过指定的字符集进行解码
buf.append(new String(line.getBytes(), "UTF-8"));
}
String retmsg = buf.toString();
System.out.println("【返回报文】" + buf.toString());
br.close();

JSONObject xmlJSONObj = XML.toJSONObject(retmsg);
//return retmsg;
// XMLSerializer xmlSerializer = new XMLSerializer();
// //将xml转为json
// String result = xmlSerializer.read(xml).toString();
System.out.println(xmlJSONObj);
}

}


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM