【问题标题】:WebDAV with J2ME带有 J2ME 的 WebDAV
【发布时间】:2009-04-04 07:08:20
【问题描述】:

有没有办法在 J2ME 中使用 WebDAV(一些库或手动编码)?

我试过了:
- javax.microedition.io.HttpConnection,但那里不支持“搜索”方法
- 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();

【问题讨论】:

    标签: http sockets java-me webdav midp


    【解决方案1】:

    “没有回报”是什么意思?没有响应体?没有状态码?

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

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

    【讨论】:

    • 感谢朱利安的回答。 SocketConnection InputStream(代表响应)中没有返回任何内容。好的,我会尝试嗅探流量。
    • 不,我没有尝试添加主机头,我也会尝试。
    【解决方案2】:

    Julian +1 你是正确的 Host 属性,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();
    

    【讨论】:

      【解决方案3】:

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

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-05-26
        • 1970-01-01
        • 2012-04-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多