Java的HttpURLConnection不支持REPORT / PROPFIND - 我该怎么办?

时间:2010-08-11 14:50:24

标签: java caldav

HttpURLConnection只支持GET,POST和HEAD之类的东西 - 但没有REPORT / PROPFIND。我将实现一个CalDAV-Client,但没有theese操作(如果我想使用它们,我得到一个ProtocolException)我必须用auth编写/交付一个完整而庞大的HTTP库,等等。

“过度破坏”。

如何使用PROPFIND和REPORT发送请求?

6 个答案:

答案 0 :(得分:4)

我在WebDav上遇到类似PROPFIND方法的问题。

通过实施此解决方案解决了这个问题: 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);

            }
     }

答案 1 :(得分:3)

您可能希望为此找一个WebDAV库,而不是HTTP库。

也许看看Apache Jackrabbit

答案 2 :(得分:3)

您可以使用https://github.com/square/okhttp

示例代码

    // using OkHttp
    public class PropFindExample {        
    private final OkHttpClient client = new OkHttpClient();
    String run(String url) throws IOException {
        String credential = Credentials.basic(userName, password);
        // body
        String body = "<d:propfind xmlns:d=\"DAV:\" xmlns:cs=\"http://calendarserver.org/ns/\">\n" +
                "  <d:prop>\n" +
                "     <d:displayname />\n" +
                "     <d:getetag />\n" +
                "  </d:prop>\n" +
                "</d:propfind>";
        Request request = new Request.Builder()
                .url(url)
                .method("PROPFIND", RequestBody.create(MediaType.parse(body), body))
                .header("DEPTH", "1")
                .header("Authorization", credential)
                .header("Content-Type", "text/xml")
                .build();

        Response response = client.newCall(request).execute();
        return response.body().string();
    }
 }

或玩套接字

示例代码

String host = "example.com";
int port = 443;
String path = "/placeholder";
String userName = "username";
String password = "password";

SSLSocketFactory ssf = (SSLSocketFactory) SSLSocketFactory.getDefault();
Socket socket = ssf.createSocket(host, port);
PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())));

// xml data to be sent in body
String xmlData = "<?xml version=\"1.0\"?> <d:propfind xmlns:d=\"DAV:\" xmlns:cs=\"http://calendarserver.org/ns/\"> <d:prop> <d:displayname /> <d:getetag /> </d:prop> </d:propfind>";
// append headers
out.println("PROPFIND path HTTP/1.1");
out.println("Host: "+host);
String userCredentials = username+":"+password;
String basicAuth = "Basic " + new String(Base64.encode(userCredentials.getBytes(), Base64.DEFAULT));
String authorization = "Authorization: " + basicAuth;
out.println(authorization.trim());
out.println("Content-Length: "+ xmlData.length());
out.println("Content-Type: text/xml");
out.println("Depth: 1");
out.println();
// append body
out.println(xmlData);
out.flush();

// get response
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
String inputLine;

System.out.println("--------------------------------------------------------");
System.out.println("---------------Printing response--------------------------");
System.out.println("--------------------------------------------------------");
while ((inputLine = in.readLine()) != null) {
    System.out.println(inputLine);
}

in.close();

答案 3 :(得分:2)

您可以尝试使用其他HTTP库,例如Apache HTTP client并扩展其HttpRequestBase(例如,请参阅HttpGetHttpPost)。

或者,您可以直接使用WebDAV客户端库。

答案 4 :(得分:0)

考虑使用Caldav4j

它支持:

  • 轻松生成报告请求
  • 基于httpclient 3.0
  • android app
  • 目前正在迁移到jackrabbit

答案 5 :(得分:0)

BookTagLinks