0

.asmx SOAP Web サービスを java から送信してオブジェクトで使用したいと考えています。このサービスは、基本認証も使用します。オブジェクトを 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);

サーバーは次のエラーを返します。

soap:クライアントの不正な引数

どの引数が間違っているかを見つけることができません。エラーメッセージの正確な説明を見つける方法はありますか? SoapUIでテストしましたが、同じエラーが発生しましたが、そのエラーに関する説明も表示されたため、SoapUIでエラーを解決して正しい応答を得ました。しかし、コードを使用して同じことを行うことはできません。

于 2013-05-09T07:27:10.640 に答える