使用POST請求後嘗試從Web服務讀取響應時出現問題。我期望能夠獲得一些有用的XML,但是我得到的只是HTML。返回HTML而不是XML的Java Web服務調用
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你有 –
它只是一個表的所有網頁可以從這個服務器,其中有一個指向我連接過於相同頁面的URL服務的列表。它返回200 OK響應。 – user1795416
看看我的迴應。安裝soapUI並加載所有Web服務(只需放入WSDL路徑)。之後,你可以玩每個WS,並驗證*清除*網絡服務之前工作定義。 –