您好我正在尝试通过代理servlet使用ajax调用发送XML有效内容。 url是SOAP Web服务。客户端javascript代码看起来像这样。
var sURL = "https://username:password@my11111.crm.example.com/sap/bc/srt/scs/sap/managecustomerin1?sap-vhost=my11111.crm.example.com"
$.ajax({
url : sURL,
beforeSend: function(xhrObj){
xhrObj.setRequestHeader("Content-Type","text/xml; charset=UTF-8");
},
type : "POST",
dataType : "xml",
data : sEnv,
processData: false,
contentType: "text/xml; charset=UTF-8",
success : handleSuccessCallback,
error : handleErrorCallback
});
function handleSuccessCallback(data, resp){
alert("Success: "+data);
}
function handleErrorCallback(msg){
alert("Error: "+msg);
}
变量sEnv保存有效载荷xml数据,例如:
<?xml version='1.0' encoding='utf-8'?>
<soapenv:Envelope
xmlns:soapenv='http://schemas.xmlsoap.org/soap/envelope/'
xmlns:glob='http://sap.com/xi/SAPGlobal20/Global'
xmlns:a00='http://sap.com/xi/AP/CustomerExtension/BYD/A0014'
xmlns:glob1='http://sap.com/xi/AP/Globalization'>
<soapenv:Header/>
<soapenv:Body>
<glob:CustomerBundleMaintainRequest_sync_V1>
<BasicMessageHeader></BasicMessageHeader>
<Customer actionCode='01'>
<CategoryCode>2</CategoryCode>
<CustomerIndicator>true</CustomerIndicator>
<LifeCycleStatusCode>2</LifeCycleStatusCode>
<Organisation>
<FirstLineName>SAP Madrid </FirstLineName>
</Organisation>
<ABCClassificationCode>A</ABCClassificationCode>
<AddressInformation actionCode='01' addressUsageListCompleteTransmissionIndicator='true'>
<ObjectNodeSenderTechnicalID>002</ObjectNodeSenderTechnicalID>
<Address actionCode='01' telephoneListCompleteTransmissionIndicator='true'>
<PreferredCommunicationMediumTypeCode>LET</PreferredCommunicationMediumTypeCode>
<EmailURI>info@sap.com</EmailURI>
<FacsimileFormattedNumberDescription>9111111111</FacsimileFormattedNumberDescription>
<WebURI>www.sap.com</WebURI>
<Telephone>
<ObjectNodeSenderTechnicalID>004</ObjectNodeSenderTechnicalID>
<FormattedNumberDescription>9111111111</FormattedNumberDescription>
</Telephone>
<PostalAddress>
<CountryCode>ES</CountryCode>
<CityName>Madrid</CityName>
<StreetPostalCode>28043</StreetPostalCode>
<StreetName>Torrelaguna</StreetName>
<HouseID>2</HouseID>
<POBoxDeviatingRegionCode>28</POBoxDeviatingRegionCode>
</PostalAddress>
</Address>
</AddressInformation>
<DirectResponsibility actionCode='01'>
<PartyRoleCode>142</PartyRoleCode>
<EmployeeID>E1019</EmployeeID>
</DirectResponsibility>
</Customer>
</glob:CustomerBundleMaintainRequest_sync_V1>
</soapenv:Body>
当我使用chrome flag “ - disable-web-security”并且没有任何代理servlet(请参阅上面的sUrl变量)来调用服务时,上面的代码可以工作,我可以发送有效负载成功到Web服务,我可以看到后端添加的新数据。但我不想要Chrome安全标志。所以我尝试使用代理Servlet。
现在我在这个
的ajax调用中使用URLvar sURL = "/tenantSystemUi5/TechProxyServletPost?username:password@https://my11111.crm.example.com/sap/bc/srt/scs/sap/managecustomerin1?sap-vhost=my11111.crm.example.com"
doPost()中的类TechProxyServlet代码片段如下
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String logString="TECHPROXY POST : START :\n";
try{
String reqEncoding = request.getCharacterEncoding();
if( reqEncoding == null || reqEncoding.trim().length()==0 )
reqEncoding = "UTF-8";
String payload = this.getDataFromStreamPost(request.getInputStream(),reqEncoding);
String xmlStreamInput = payload; // XML Input
String inputString = request.getQueryString();
String requestURL = "";
String userpass = "";
if(inputString.contains("@")) // Contains User Name and Password
{
String temp[] = inputString.split("@");
userpass = temp[0];
requestURL = temp[1];
}
else
{
requestURL = inputString;
}
String xCsrfToken = null;
HttpURLConnection connection = null;
List<String> cookies = null;
try {
URL serviceUrl = new URL(requestURL);
System.setProperty("https.proxyHost", "proxy");
System.setProperty("https.proxyPort", "8080");
logString = logString+"TECHPROXY POST URL:"+requestURL+"\n";
connection = (HttpURLConnection) serviceUrl.openConnection();
connection.setRequestMethod("GET");
// connection.setRequestMethod("POST");
if(userpass.length() > 0)
{
connection.setRequestProperty("Authorization", this.getBasicAuth(userpass));
}
connection.setRequestProperty("Content-Type", "text/xml; charset="+reqEncoding);
connection.setRequestProperty("x-csrf-token", "fetch");
connection.connect(); // SUSPECT 1
System.out.println("RESPONSE CODE: "+connection.getResponseCode()); //SUSPECT 2
/*This if condition results false becoz 200 != 500 :( */
if (HttpURLConnection.HTTP_OK == connection.getResponseCode())
{
// Do Stuff...... like writing payload to the server
}
else {
response.setStatus(connection.getResponseCode());
response.setContentType("application/json; charset=TIS-620");
String servletErrResponse = this.getDataFromStream(connection.getErrorStream());
response.getWriter().println(servletErrResponse);
logString = logString+"TECHPROXY ServletResponse XSCRF GET:ERROR:"+servletErrResponse+"\n";
}
}
catch (Exception e) {
e.printStackTrace();
response.setStatus(connection.getResponseCode());
response.setContentType("application/json; charset=TIS-620");
String servletErrResponse = this.getDataFromStream(connection.getErrorStream());
response.getWriter().println(servletErrResponse);
logString = logString+"TECHPROXY GET ServletResponse:ERROR:"+servletErrResponse+"\n";
}
}catch (Exception e) {
logString = logString+"TECHPROXY EXCEPTION:"+e.toString()+"\n";
e.printStackTrace();
}
}
当我通过上述代理servlet请求服务时, connection.getResponseCode()会返回响应代码 500(内部服务器错误)。和servletErrResponse字符串为
<soap-env:Envelope
xmlns:soap-env="http://schemas.xmlsoap.org/soap/envelope/">
<soap-env:Header/>
<soap-env:Body>
<soap-env:Fault>
<faultcode>soap-env:Server</faultcode>
<faultstring xml:lang="en">Web service processing error; more details in the web service error log on provider side (UTC timestamp 20141211053452; Transaction ID 00163E087A571ED4A09EEDD46BAC158F)</faultstring>
<detail/>
</soap-env:Fault>
</soap-env:Body>
在上面的servlet中,我试图绕过浏览器的访问源控制策略。但我无法弄清楚为什么会出现500错误。我该怎么做才能纠正它。现在我可能无法访问服务器日志文件。 是行
connection.setRequestProperty("x-csrf-token", "fetch");
必要? 非常感谢任何帮助。
谢谢,
新