使用J2ME的WebDAV

时间:2009-04-04 07:08:20

标签: http sockets java-me webdav midp

有没有办法将WebDAV与J2ME(某些库或手动编码)一起使用?

我试过了:
- javax.microedition.io.HttpConnection,但不支持“SEARCH”方法 - javax.microedition.io.SocketConnectionHttp request - 没有任何回复作为回复
我的代码或HTTP标头可能有问题:

    String response = "";
    String query = "<?xml version='1.0'?> " 
            + "<g:searchrequest xmlns:g='DAV:'> "
            + "<g:sql> "
            + "SELECT 'DAV:displayname' "
            + "FROM 'http://exchangeserver.com/Public/' "
            + "</g:sql> "
            + "</g:searchrequest> ";
    String len = String.valueOf(query.length());
    SocketConnection hc = (SocketConnection) Connector
            .open("socket://exchangeserver.com:8080");
    DataOutputStream dout = 
            new DataOutputStream(hc.openOutputStream());
    DataInputStream din = new DataInputStream(hc.openInputStream());
    String userPass = "username" + ":" + "password";
    byte[] encoded = 
            Base64OutputStream.encode(userPass.getBytes(), 0,
            userPass.length(), false, false);
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    String request = "SEARCH /Public/ HTTP/1.1\r\n"
            +"Content-Type:text/xml\r\nContent-Length:"
            + len
            + "\r\nAuthorization:Basic "
            + new String(encoded)
            + "\r\n\r\n";
    bos.write(request.getBytes());
    bos.write(query.getBytes());
    dout.write(bos.toByteArray());
    dout.flush();
    dout.close();
    byte[] bs = new byte[900];
    din.readFully(bs);
    bos = new ByteArrayOutputStream();
    bos.write(bs);
    din.close();
    hc.close();
    response = bos.toString();

3 个答案:

答案 0 :(得分:4)

“没有回报”是什么意思?没有反应机构?没有状态代码?

我建议跟踪“电线”上发生的事情......

更新:您是否尝试过添加主机标头?

答案 1 :(得分:2)

朱利安+1你是对的主机财产,QRSO +1,感谢所有! 所以,
- 我找到了免费的WebDAV服务MyDisk.se(不允许搜索,所以我使用了PROPFIND)
- 使用WFetch来解决WebDAV请求问题 - 使用Network Monitor来比较来自WFetch和我的应用的请求 :)最后它的工作! 结果代码:

String response = "";
String query = "<?xml version='1.0' encoding='UTF-8'?>\r\n"
        + "<d:propfind xmlns:d='DAV:'>\r\n"
        + "<d:prop><d:getcontenttype/></d:prop>\r\n"
        + "<d:prop><d:getcontentlength/></d:prop>\r\n"
        + "</d:propfind>\r\n";

String len = String.valueOf(query.length());
SocketConnection hc = (SocketConnection) Connector
        .open("socket://79.99.7.153:80");
DataOutputStream dout = new DataOutputStream(hc.openOutputStream());
DataInputStream din = new DataInputStream(hc.openInputStream());
String userPass = "login" + ":" + "password";
byte[] encoded = Base64OutputStream.encode(userPass.getBytes(), 0,
        userPass.length(), false, false);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
String request = "PROPFIND /mgontar/ HTTP/1.1\r\n" 
        + "Depth: 1\r\n"
        + "Host: mydisk.se:80\r\n" 
        + "Accept: */*\r\n"
        + "Content-Type: text/xml\r\n" 
        + "Content-Length: " + len
        + "\r\nAuthorization: Basic " + new String(encoded)
        + "\r\n\r\n";
bos.write(request.getBytes());
bos.write(query.getBytes());
dout.write(bos.toByteArray());
dout.flush();
dout.close();
byte[] bs = new byte[900];
din.readFully(bs);
bos = new ByteArrayOutputStream();
bos.write(bs);
din.close();
hc.close();
response = bos.toString();

答案 2 :(得分:1)

仅供参考如果您在实际的手机上进行测试,那么您的移动网络运营商很可能会阻止非HTTP流量。

您可能需要先检查是否可以先向服务器发出GET和POST请求。