簡體   English   中英

Java Web服務調用返回HTML而不是XML

[英]Java web service call returning HTML instead of XML

在嘗試使用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請求。 知道這里有什么問題嗎?

假設您有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();
    } 


....

它可能會幫助您或找到正確的方向

您似乎在將Web服務稱為“艱難的方式”(TM)。

有許多框架(Metro,CXF等)可以為您生成存根類,您可以很輕松地調用它們,並且處理當前問題的可能性大大降低。 wsimport是您的朋友,它將告訴您所涉及的WSDL是否不正確。

有關某些相關鏈接,請參見此答案

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM