依赖JAR:
commons-cli-1.2.jar
commons-codec-1.10.jar
commons-io-2.4.jar
commons-logging-1.2.jar
fluent-hc-4.5.5.jar
httpclient-4.5.5.jar
httpclient-cache-4.5.5.jar
httpclient-win-4.5.5.jar
httpcore-4.4.9.jar
httpcore-ab-4.4.9.jar
httpcore-nio-4.4.9.jar
httpmime-4.5.5.jar
jdom.jar
jna-4.4.0.jar
jna-platform-4.4.0.jar
下载地址:https://gitee.com/kiboy/http_request_calls_webservice.git
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.http.HttpEntity;
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.BasicHeader;
import org.apache.http.util.EntityUtils;
public class HttpRequestUtil2 {
private static Log log=LogFactory.getLog(HttpRequestUtil2.class);
/**
*
* <p>描述: 使用HttpPost传输 有用户名密码验证的xml格式的WebService调用</p>
* @author WK-kiboy
* @date 2018年9月18日
* @param url webservice的URL
* @param xmlStr xml格式的数据字符串
* @param username 用户名
* @param password 密码
* @return 返回传输响应的结果字符串
*/
public static String postXMLAuthWithHttpPost(String url, String xmlStr, String username,String password){
CloseableHttpClient httpclient = HttpClients.createDefault();
HttpEntity entity = null;
HttpEntity Rentity = null;
String retStr="";
CloseableHttpResponse response=null;
try {
entity = new StringEntity(xmlStr,"UTF-8");
HttpPost hp = new HttpPost(url);
hp.addHeader("Content-Type","application/soap+xml;charset=UTF-8");
hp.addHeader(new BasicHeader("Authorization","Basic " + Base64.encodeBase64String((username+":"+password).getBytes())));
hp.setEntity(entity);
response = httpclient.execute(hp);
Rentity = response.getEntity();
if (Rentity != null) {
retStr=EntityUtils.toString(Rentity, "UTF-8");
}
} catch (Exception e) {
log.error("使用HttpPost传输XML格式字符串失败:"+e);
} finally {
// 关闭连接,释放资源
try {
if(response!=null) response.close();
if(httpclient!=null) httpclient.close();
} catch (IOException e) {
}
}
return retStr;
}
/**
*
* <p>描述: 使用HttpURLConnection传输 有用户名密码验证的xml格式的WebService调用</p>
* @author WK-kiboy
* @date 2018年9月18日
* @param url webservice的URL
* @param xmlStr xml格式的数据字符串
* @param username 用户名
* @param password 密码
* @return 返回传输响应的结果字符串
*/
public static String postXMLAuthWithUrlConn(String url,String xmlStr,String username,String password){
String encoding =new String(Base64.encodeBase64(new String(username+":"+password).getBytes()));
URL urlObj =null;
HttpURLConnection conn=null;
String retStr="";
DataOutputStream dos=null;
BufferedReader reader=null;
try {
urlObj = new URL(url);
conn = (HttpURLConnection) urlObj.openConnection();
conn.setRequestProperty("Content-Type", "application/soap+xml;charset=utf-8");
conn.setRequestProperty("Authorization","Basic "+encoding);
conn.setRequestMethod("POST");
conn.setUseCaches(false);
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setConnectTimeout(10000);
conn.setReadTimeout(10000);
dos = new DataOutputStream(conn.getOutputStream());
dos.write(xmlStr.getBytes("utf-8"));
dos.flush();
reader = new BufferedReader(new InputStreamReader(conn.getInputStream(), "utf-8"));
String line = null;
StringBuffer strBuf = new StringBuffer();
while ((line = reader.readLine()) != null) {
strBuf.append(line);
}
retStr = strBuf.toString();
} catch (Exception e) {
log.error("使用HttpURLConnection POST传输XML数据异常==="+e);
}finally {
try {
if(dos!=null) dos.close();
if(reader!=null) reader.close();
} catch (Exception e2) { }
}
return retStr;
}
}
调用
package com.kiboy.test;
import java.util.Map;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.Namespace;
import org.jdom.output.XMLOutputter;
import com.kiboy.util.HttpRequestUtil;
public class TestHttpWebService {
public static void main(String[] args) {
String xmlStr=makeXml("",null);
String username="_SAP_ACC";
String password="Welcome1";
String url="https://my6010358.sapbyd.cn/sap/bc/srt/scs/sap/accountingentryreplicationin?sap-vhost=my6100358.sapbyd.cn";
try {
HttpRequestUtil.postXMLAuthWithHttpPost(url, xmlStr, username, password);
HttpRequestUtil.postXMLAuthWithUrlConn(url, xmlStr, username, password);
} catch (Exception e) {
System.out.println("异常"+e);
}
}
/**
* 生成请求xml数据
* @param methodName 方法名 本例为"ummWaitMessageAdd"
* @param params 数据 (key为wsdl文件中参数的name值注意大小写和顺序都要保持一致,value为实际值)
* @return
*/
private static String makeXml(String methodName, Map<String,String> params) {
StringBuffer sb = new StringBuffer();
sb.append(""
+ "<soap-env:Envelope xmlns:soap-env=\"http://www.w3.org/20103/05/soap-envelope\" xmlns:glob=\"http://sap.com/xi/SAPGlobal20/Global\" xmlns:a3k=\"http://sap.com/xi/AP/CustomerExtension/BYD/A3KKT\">" +
// " <soap-env:Header/>" +
" <soap-env:Body>" +
" <glob:AccountingEntryBundleReplicateRequest>" +
" <MessageHeader>" +
" <ID>OA</ID>" +
" <CreationDateTime>2018-09-18T01:00:00Z</CreationDateTime>" +
" <SenderBusinessSystemID>_LOCAL_SYSTEM_ALIAS_SAP_INTERNAL_CONSTANT_VALUE_</SenderBusinessSystemID>" +
" <RecipientBusinessSystemID>_LOCAL_SYSTEM_ALIAS_SAP_INTERNAL_CONSTANT_VALUE_</RecipientBusinessSystemID>" +
" <BusinessScope>" +
" <TypeCode>3</TypeCode>" +
" <ID >264</ID>" +
" </BusinessScope>" +
" </MessageHeader>" +
" <AccountingEntry>" +
" <ExternalID>OA0305</ExternalID>" +
" <CompanyID>2000</CompanyID>" +
" <Note languageCode=\"ZH\">抬头备注001</Note>" +
" <AccountingDocumentTypeCode>00047</AccountingDocumentTypeCode>" +
" <EntryDate>2018-09-18</EntryDate>" +
" <PostingDate>2018-09-18</PostingDate>" +
" <AccountingClosingStepCode>10</AccountingClosingStepCode>" +
" <BusinessTransactionTypeCode>601</BusinessTransactionTypeCode>" +
" <TransactionCurrencyCode>CNY</TransactionCurrencyCode>" +
" <DraftIndicator>true</DraftIndicator>" +
" <Item>" +
" <DebitCreditCode>2</DebitCreditCode>" +
" <ChartOfAccountsItemCode>10020101</ChartOfAccountsItemCode>" +
" <Note languageCode=\"ZH\">银行付款</Note>" +
" <TransactionCurrencyAmount currencyCode=\"CNY\">10</TransactionCurrencyAmount>" +
" <LocalCurrencyAmount currencyCode=\"CNY\">10</LocalCurrencyAmount>" +
" </Item>" +
" <Item>" +
" <DebitCreditCode>1</DebitCreditCode>" +
" <ChartOfAccountsItemCode>65031901</ChartOfAccountsItemCode>" +
" <OverheadCostsLedgerAccountItem>" +
" <CostCentreID>M2100</CostCentreID>" +
" </OverheadCostsLedgerAccountItem>" +
" <Note languageCode=\"ZH\">费用报销 </Note>" +
" <TransactionCurrencyAmount currencyCode=\"CNY\">10</TransactionCurrencyAmount>" +
" <LocalCurrencyAmount currencyCode=\"CNY\">10</LocalCurrencyAmount>" +
" <a3k:Ex_FeeName>费用名称test</a3k:Ex_FeeName>" +
" <a3k:Ex_ProjectName>项目名称test</a3k:Ex_ProjectName>" +
" <a3k:Ex_EmployeeName>员工名称test</a3k:Ex_EmployeeName>" +
" </Item>" +
" </AccountingEntry>" +
" </glob:AccountingEntryBundleReplicateRequest>" +
" </soap-env:Body>" +
"</soap-env:Envelope>");
System.out.println(sb);
return sb.toString();
}
public static String getSOPAXML() {
Namespace soapenv = org.jdom.Namespace.getNamespace("soapenv", "http://www.w3.org/20103/05/soap-envelope");
Namespace glob = Namespace.getNamespace("glob", "http://sap.com/xi/SAPGlobal20/Global");
Namespace a3k = Namespace.getNamespace("a3k", "http://sap.com/xi/AP/CustomerExtension/BYD/A3KKT");
Element Envelope = new Element("Envelope");
Envelope.setNamespace(soapenv);
Envelope.addNamespaceDeclaration(glob);
Envelope.addNamespaceDeclaration(a3k);
Element Header = new Element("Header");
Header.setNamespace(soapenv);
Element Body = new Element("Body");
Body.setNamespace(soapenv);
Element AccountingEntryBundleReplicateRequest = new Element("AccountingEntryBundleReplicateRequest");
AccountingEntryBundleReplicateRequest.setNamespace(glob);
Element MessageHeader = new Element("MessageHeader");
Element ID = new Element("ID").setText("OA");
Element CreationDateTime = new Element("CreationDateTime").setText("2018-09-17T01:00:00Z");
Element SenderBusinessSystemID = new Element("SenderBusinessSystemID")
.setText("_LOCAL_SYSTEM_ALIAS_SAP_INTERNAL_CONSTANT_VALUE_");
Element RecipientBusinessSystemID = new Element("RecipientBusinessSystemID")
.setText("_LOCAL_SYSTEM_ALIAS_SAP_INTERNAL_CONSTANT_VALUE_");
Element BusinessScope = new Element("BusinessScope");
Element TypeCode = new Element("TypeCode").setText("3");
Element ID1 = new Element("ID").setText("264");
BusinessScope.addContent(TypeCode).addContent(ID1);
MessageHeader.addContent(ID).addContent(CreationDateTime).addContent(SenderBusinessSystemID)
.addContent(RecipientBusinessSystemID).addContent(BusinessScope);
Element AccountingEntry = new Element("AccountingEntry");
Element ExternalID = new Element("ExternalID").setText("OA0199");
Element CompanyID = new Element("CompanyID").setText("2000");
Element Note = new Element("Note").setAttribute("languageCode", "ZH").setText("抬头备注001");
Element AccountingDocumentTypeCode = new Element("AccountingDocumentTypeCode").setText("00047");
Element EntryDate = new Element("EntryDate").setText("2018-09-17");
Element PostingDate = new Element("PostingDate").setText("2018-09-17");
Element AccountingClosingStepCode = new Element("AccountingClosingStepCode").setText("10");
Element BusinessTransactionTypeCode = new Element("BusinessTransactionTypeCode").setText("601");
Element TransactionCurrencyCode = new Element("TransactionCurrencyCode").setText("CNY");
Element DraftIndicator = new Element("DraftIndicator").setText("true");
Element Item1 = new Element("Item");
Element DebitCreditCode1 = new Element("DebitCreditCode").setText("2");
Element ChartOfAccountsItemCode1 = new Element("ChartOfAccountsItemCode").setText("10020101");
Element Note1 = new Element("Note").setAttribute("languageCode", "ZH").setText("银行付款");
Element TransactionCurrencyAmount1 = new Element("TransactionCurrencyAmount")
.setAttribute("currencyCode", "CNY").setText("10");
Element LocalCurrencyAmount1 = new Element("LocalCurrencyAmount").setAttribute("currencyCode", "CNY")
.setText("10");
Item1.addContent(DebitCreditCode1).addContent(ChartOfAccountsItemCode1).addContent(Note1)
.addContent(TransactionCurrencyAmount1).addContent(LocalCurrencyAmount1);
Element Item2 = new Element("Item");
Element DebitCreditCode2 = new Element("DebitCreditCode").setText("1");
Element ChartOfAccountsItemCode2 = new Element("ChartOfAccountsItemCode").setText("65031901");
Element CostCentreID2 = new Element("CostCentreID").setText("M2100");
Element OverheadCostsLedgerAccountItem2 = new Element("OverheadCostsLedgerAccountItem")
.addContent(CostCentreID2);
Element Note2 = new Element("Note").setAttribute("languageCode", "ZH").setText("费用报销");
Element TransactionCurrencyAmount2 = new Element("TransactionCurrencyAmount")
.setAttribute("currencyCode", "CNY").setText("10");
Element LocalCurrencyAmount2 = new Element("LocalCurrencyAmount").setAttribute("currencyCode", "CNY")
.setText("10");
Element Ex_FeeName = new Element("Ex_FeeName").setNamespace(a3k).setText("费用名称test");
Element Ex_ProjectName = new Element("Ex_ProjectName").setNamespace(a3k).setText("项目名称test");
Element Ex_EmployeeName = new Element("Ex_EmployeeName").setNamespace(a3k).setText("员工名称test");
Item2.addContent(DebitCreditCode2).addContent(ChartOfAccountsItemCode2)
.addContent(OverheadCostsLedgerAccountItem2).addContent(Note2).addContent(TransactionCurrencyAmount2)
.addContent(LocalCurrencyAmount2).addContent(Ex_FeeName).addContent(Ex_ProjectName)
.addContent(Ex_EmployeeName);
AccountingEntry.addContent(ExternalID).addContent(CompanyID).addContent(Note)
.addContent(AccountingDocumentTypeCode).addContent(EntryDate).addContent(PostingDate)
.addContent(AccountingClosingStepCode).addContent(BusinessTransactionTypeCode)
.addContent(TransactionCurrencyCode).addContent(DraftIndicator).addContent(Item1).addContent(Item2);
AccountingEntryBundleReplicateRequest.addContent(MessageHeader).addContent(AccountingEntry);
Body.addContent(AccountingEntryBundleReplicateRequest);
Envelope.addContent(Header).addContent(Body);
Document soapDoc = new Document(Envelope);
XMLOutputter xmloutput = new XMLOutputter();
String xml = xmloutput.outputString(soapDoc).replaceAll("&","&");
return xml;
}
}
出现如下报错,说明你当前项目中有低版本的jar包,可以删除对应的低版本的jar,或者自己重新打包新版的jar,修改jar的路径