HttpURLConnectionは、GET、POST、HEADなどのみをサポートしますが、REPORT/PROPFINDはサポートしません。CalDAV-Clientを実装しますが、これらの操作を使用しないと(ProtocolExceptionが発生します)、authなどを使用して完全で巨大なHTTPライブラリを作成/配信する必要があります。
「やり過ぎ」。
PROPFINDとREPORTを使用してリクエストを送信するにはどうすればよいですか?
PROPFIND メソッドの WebDav でも同様の問題がありました。
このソリューションを実装して問題を解決しました: https://java.net/jira/browse/JERSEY-639
try {
httpURLConnection.setRequestMethod(method);
} catch (final ProtocolException pe) {
try {
final Class<?> httpURLConnectionClass = httpURLConnection
.getClass();
final Class<?> parentClass = httpURLConnectionClass
.getSuperclass();
final Field methodField;
// If the implementation class is an HTTPS URL Connection, we
// need to go up one level higher in the heirarchy to modify the
// 'method' field.
if (parentClass == HttpsURLConnection.class) {
methodField = parentClass.getSuperclass().getDeclaredField(
"method");
} else {
methodField = parentClass.getDeclaredField("method");
}
methodField.setAccessible(true);
methodField.set(httpURLConnection, method);
} catch (final Exception e) {
throw new RuntimeException(e);
}
}
これについては、HTTPライブラリではなく、WebDAVライブラリを探すことをお勧めします。
たぶん、ApacheJackrabbitを見てください。
Apache HTTPクライアントなどの別のHTTPライブラリを使用して、その拡張を試みることができますHttpRequestBase
(たとえばを参照)HttpGet
。HttpPost
または、WebDAVクライアントライブラリを直接使用することもできます。
private static void setRequestMethod(HttpURLConnection conn, String method) throws Throwable {
try {
conn.setRequestMethod(method);
} catch (ProtocolException e) {
Class<?> c = conn.getClass();
Field methodField = null;
Field delegateField = null;
try {
delegateField = c.getDeclaredField("delegate");
} catch (NoSuchFieldException nsfe) {
}
while (c != null && methodField == null) {
try {
methodField = c.getDeclaredField("method");
} catch (NoSuchFieldException nsfe) {
}
if (methodField == null) {
c = c.getSuperclass();
}
}
if (methodField != null) {
methodField.setAccessible(true);
methodField.set(conn, method);
}
if (delegateField != null) {
delegateField.setAccessible(true);
HttpURLConnection delegate = (HttpURLConnection) delegateField.get(conn);
setRequestMethod(delegate, method);
}
}
}