0

在使用 POST 请求后尝试从 Web 服务读取响应时遇到问题。我期待得到一些有用的 XML,但我得到的只是 HTML。

    URL url = new URL("https://abc.co.uk/someWS");
    String pKeyPassword = "xxxxxx";
    String xmlOutput = "someXML...";

    HttpsURLConnection con = (HttpsURLConnection) url.openConnection();

    //Load authentication certificate.
    KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance("SunX509");
    KeyStore keyStore = KeyStore.getInstance("PKCS12");

    InputStream keyInput = new FileInputStream("/home/keystore.p12");
    keyStore.load(keyInput, pKeyPassword.toCharArray());
    keyInput.close();

    keyManagerFactory.init(keyStore, pKeyPassword.toCharArray());

    SSLContext context = SSLContext.getInstance("TLS");
    context.init(keyManagerFactory.getKeyManagers(), null, new SecureRandom());

    con.setSSLSocketFactory(context.getSocketFactory());

    // Tell the connection that we will be sending information
    con.setDoInput(true);
    con.setDoOutput(true);
    con.setRequestProperty("Content-Length", "" + xmlOutput.length());
    con.setRequestProperty("Content-Type", "text/xml; UTF-8");
    con.setRequestMethod("POST");

    con.connect();

    // Send the POST stream data
    DataOutputStream outputStream = new DataOutputStream(con.getOutputStream());
    outputStream.writeBytes(xmlOutput);

    // Read the response
    InputStream inputstream = con.getInputStream();
    InputStreamReader inputstreamreader = new InputStreamReader(inputstream);
    BufferedReader bufferedreader = new BufferedReader(inputstreamreader);

    // format response to a string
    String string = null;
    String response = "";
    while ((string = bufferedreader.readLine()) != null) {
      response += string;
    }
    con.disconnect();
    System.out.println(response);

我确信我正在连接的 Web 服务没有任何问题,显然从他们的角度来看,我正在尝试执行 GET 请求(它将返回 HTML)而不是 POST 请求。知道这里有什么问题吗?

4

2 回答 2

0

您似乎将您的网络服务称为“艰难的方式”(TM)。

有许多框架(Metro、CXF 等)可以为您生成存根类,您可以很容易地调用它们,而且您处理当前问题的可能性要小得多。wsimport是您的朋友,如果有问题的 WSDL 不正确,他会告诉您。

有关一些相关链接,请参阅此答案

于 2012-11-02T23:29:16.070 回答
0

假设如果你有 HTML,它可能是一些服务器错误。请阅读服务器日志以检测该问题。无论如何,您可以使用soapUI应用程序来验证您的Web 服务是否已定义并正常工作。

这是对我有用的代码片段(只需添加密码):

import org.apache.axis.AxisFault;
import org.apache.axis.SOAPPart;
import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import org.apache.axis.message.SOAPEnvelope;
import org.apache.axis.soap.MessageFactoryImpl;
....
String mHostAddr = "yourURL";
try {

        String envelope = getXML(); //redFromXmlFile();
        //String envelope = "";
        byte[] reqBytes = envelope.getBytes();
        ByteArrayInputStream bis = new ByteArrayInputStream(reqBytes);
        StreamSource ss = new StreamSource(bis);

        //Create a SOAP Message Object
        MessageFactoryImpl messageFactory = new MessageFactoryImpl();
        SOAPMessage msg = messageFactory.createMessage();
        SOAPPart soapPart = (SOAPPart) msg.getSOAPPart();

        //Set the soapPart Content with the stream source
        soapPart.setContent(ss);

        //Create a WebService Call
        Service service = new Service();
        Call call = (Call)service.createCall();
        call.setTargetEndpointAddress(mHostAddr);

        //Invoke the WebService.
        SOAPEnvelope resp = call.invoke(((org.apache.axis.SOAPPart)soapPart).getAsSOAPEnvelope());






    } catch (AxisFault ex) {
        System.out.println(ex.getMessage());
    } catch (ServiceException ex) {
        System.out.println(ex.getMessage());
    } catch (SOAPException ex) {
        System.out.println(ex.getMessage());
    } catch (RemoteException e) {
        e.printStackTrace();
    } 


....

它可能会帮助您或找到正确的方向

于 2012-11-02T22:49:30.123 回答