用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