2013-05-08 107 views
0

我想通過發送和對象使用來自java的.asmx SOAP web服務。 該服務還使用基本身份驗證。 我已經成功地將對象編組爲xml,並嘗試使用下面的方法發送。java soap asmx基本認證

我曾嘗試以下方法做同樣的:

URL url = new URL("https://api.sandbox.ewaypayments.com/Soap.asmx?wsdl"); 
httpURLConnection = (HttpURLConnection) url.openConnection(); 
httpURLConnection.setRequestProperty("Authorization", "Basic " + encodedString); 
httpURLConnection.setRequestMethod("POST"); 
httpURLConnection.setRequestProperty("Content-Length", String.valueOf(xml.length())); 
httpURLConnection.setRequestProperty("Content-Type", "text/xml"); 
httpURLConnection.setRequestProperty("SoapAction", ""); 
httpURLConnection.setDoInput(true); 
httpURLConnection.setDoOutput(true); 
httpURLConnection.setUseCaches(false); 
httpURLConnection.setRequestProperty("Content-Type", "text/xml"); 
PrintWriter pw = new PrintWriter(httpURLConnection.getOutputStream()); 
pw.write(postParameter); 
pw.flush(); 
BufferedReader br = new BufferedReader(new InputStreamReader(httpURLConnection.getInputStream())); 
String line = ""; 
while ((line = br.readLine()) != null) 
{ 
    responseMessage.append(line); 
} 


Call call = new Call("https://api.sandbox.ewaypayments.com/Soap.asmx"); 
call.setOperationName(new QName("https://api.sandbox.ewaypayments.com/", "CreateAccessCode")); 
call.setSOAPActionURI("https://api.sandbox.ewaypayments.com/CreateAccessCode"); 
String strResult = (String) call.invoke(new Object[] { new CreateAccessCodeRequest()}); 

但我從上面的方法正在從服務器500錯誤。 我知道在服務器端沒有問題,因爲PHP調用工作正常。我只是不能讓它與java一起工作。 請幫忙。

回答

0

我已經通過使用以下方法取得了一些進展,並設法刪除錯誤。

String loginPassword = username + ":" + password; 
String encodedString = Base64.encodeBase64String(loginPassword.getBytes()); 
String xmlData = ""; 
String data = ""; 
BufferedReader br1 = new BufferedReader(new FileReader("D:\\reqfile.xml")); 
while ((data = br1.readLine()) != null) 
{ 
    xmlData += data; 
    System.out.println(data); 
} 

DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); 
dbFactory.setNamespaceAware(true); 
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder(); 
Document doc = dBuilder.parse(new File("D:\\reqfile.xml")); 

SOAPConnectionFactory soapConnectionFactory = SOAPConnectionFactory.newInstance(); 
SOAPConnection soapConnection = soapConnectionFactory.createConnection(); 

MessageFactory messageFactory = MessageFactory.newInstance(); 
SOAPMessage soapMessage = messageFactory.createMessage(); 

soapMessage.getMimeHeaders().addHeader("Authorization", "Basic " + encodedString); 
soapMessage.getMimeHeaders().addHeader("SOAPAction", "https://api.ewaypayments.com/CreateAccessCode"); 

SOAPPart soapPart = soapMessage.getSOAPPart(); 
SOAPEnvelope soapEnvelope = soapPart.getEnvelope(); 

SOAPBody soapBody = soapEnvelope.getBody(); 
soapBody.addDocument(doc); 

soapMessage.saveChanges(); 

SOAPMessage reply = soapConnection.call(soapMessage, "https://api.sandbox.ewaypayments.com/Soap.asmx"); 

TransformerFactory transformerFactory = TransformerFactory.newInstance(); 
Transformer transformer = transformerFactory.newTransformer(); 
Source sourceContent = reply.getSOAPPart().getContent(); 
StreamResult result = new StreamResult(System.out); 
transformer.transform(sourceContent, result); 

現在,服務器返回我以下錯誤:

肥皂:客戶 錯誤參數

我無法找到其論據是錯誤的。有沒有辦法找出確切的錯誤信息描述? 我已經在SoapUI中測試了它,並且得到了同樣的錯誤,但它也給了我一些關於該錯誤的描述,並且由於我需要解決SoapUI中的錯誤並獲得了正確的響應。但我無法使用代碼做同樣的事情。

+0

你有沒有找到什麼? – ihebiheb 2015-11-03 11:44:56