【プロキシ】プロキシ認証が実装されていない場合
エラー内容
javax.xml.ws.WebServiceException:
... 略 ...
Caused by: java.io.IOException: Server returned HTTP response code: 407 for URL: http://...
... 略 ...
解決策
* プロキシ認証が実装する。詳細は、以下の関連記事を参照のこと。
https://blogs.yahoo.co.jp/dk521123/36966230.html
より
Authenticator.setDefault(new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("【ユーザ名】", "【パスワード】".toCharArray());
}
});
エラー内容
javax.xml.ws.WebServiceException: 次の場所でWSDLへのアクセスに失敗しました: ... 略 ...
sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target。
... 略 ...
Caused by: javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
... 略 ...
Caused by: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
... 略 ...
Caused by: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
... 略 ...
解決策
* オレオレ証明書を受け入れるように証明書チェックを行わない
* オレオレ証明書の公開鍵をクライアント側のキーストアにインポートする
エラー内容
com.sun.xml.internal.ws.client.ClientTransportException:
... 略 ...
Caused by: java.net.SocketTimeoutException: connect timed out
... 略 ...
解決策
* 接続情報(ホスト名やポート)に正しい値を設定する
【その他通信】URLに誤りがある場合
エラー内容
com.sun.xml.internal.ws.wsdl.parser.InaccessibleWSDLException: 2 counts of InaccessibleWSDLException.
java.io.FileNotFoundException: http://... 略 ...
解決策
* 正しいURL値を設定する
URL url = new URL("【WSDLのURL】");
【その他通信】URLのポートに誤りがある場合
エラー内容
javax.xml.ws.WebServiceException: 次の場所でWSDLへのアクセスに失敗しました:
... 略 ...
Caused by: java.io.IOException: Server returned HTTP response code: 503 for URL: http://... 略 ...
解決策
* 正しいURLのポート値を設定する
■ 例外ハンドリング
* 上記を踏まえて、例外をハンドリングを行う
* サーバは、以下の関連記事を参照のこと。
https://blogs.yahoo.co.jp/dk521123/36139336.html
サンプル
もっといい方法ないかな...
import java.io.IOException;
import java.net.Authenticator;
import java.net.ConnectException;
import java.net.MalformedURLException;
import java.net.PasswordAuthentication;
import java.net.ProtocolException;
import java.net.SocketTimeoutException;
import java.net.URL;
import java.util.Map;
import javax.net.ssl.SSLHandshakeException;
import javax.xml.ws.BindingProvider;
import javax.xml.ws.WebServiceException;
public class Main {
public static void main(String[] args) {
System.out.println("Start!");
try {
System.setProperty("java.net.useSystemProxies", "true");
System.setProperty("http.proxyHost", "XXX.XXX.XXX.XXX");
System.setProperty("http.proxyPort", "3128");
System.setProperty("https.proxyHost", "XXX.XXX.XXX.XXX");
System.setProperty("https.proxyPort", "3128");
Authenticator.setDefault(new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("dkAdmin", "password".toCharArray());
}
});
URL url = new URL("https://XXX.XXX.XXX.XXX:8443/SampleWebService/services/SampleWebService.ws?wsdl");
SampleWebServiceService service = new SampleWebServiceService(url);
SampleWebService proxy = service.getSampleWebServicePort();
Map<String, Object> context = ((BindingProvider) proxy).getRequestContext();
// コネクションを確立するまでの時間[ミリ秒]
context.put("com.sun.xml.internal.ws.request.timeout", 10000);
// レスポンスを受け取るまでの時間[ミリ秒]
context.put("com.sun.xml.internal.ws.connect.timeout", 10000);
long start = System.currentTimeMillis();
String result = proxy.sayYourAge("Ken", 11L);
long end = System.currentTimeMillis();
System.out.println((end - start) + "ms");
System.out.println("Result : " + result);
} catch (WebServiceException ex) {
Throwable causeEx = ex.getCause();
ex.printStackTrace();
if (causeEx == null) {
ex.printStackTrace();
} else if (causeEx instanceof SSLHandshakeException) {
System.out.println("SSL証明書エラー");
} else if (causeEx instanceof SocketTimeoutException) {
System.out.println("タイムアウト");
} else if (causeEx instanceof ConnectException) {
System.out.println("接続エラー");
} else if (causeEx instanceof ProtocolException) {
System.out.println("認証データミス?");
} else if (causeEx instanceof IOException) {
String error = causeEx.getMessage();
if (error != null && error.contains(" 407 ")) {
System.out.println("認証エラー?");
} else {
System.out.println("その他のエラー");
}
} else {
ex.printStackTrace();
}
} catch (MalformedURLException ex) {
ex.printStackTrace();
} catch (Exception ex) {
ex.printStackTrace();
}
System.out.println("Done");
}
}