0

我想通过发送和对象来使用来自 java 的 .asmx SOAP Web 服务。该服务还使用基本身份验证。我已成功将对象编组为 xml 并尝试使用以下方法发送。

我尝试了以下方法来做同样的事情:

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一起工作。请帮忙。

4

1 回答 1

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 中的错误并得到正确的响应。但我无法使用代码做同样的事情。

于 2013-05-09T07:27:10.640 回答