1

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

我尝试过:
- javax.microedition.io.HttpConnection,但那里不支持“SEARCH”方法
-带有Http 请求的javax.microedition.io.SocketConnection - 没有任何返回响应 可能我的代码或 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();
4

3 回答 3

4

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

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

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

于 2009-04-04T08:07:47.710 回答
2

Julian +1 你是正确的 Host 属性,QRSO +1,谢谢大家!所以,
- 我找到了免费的 WebDAV 服务MyDisk.se(不允许搜索,所以我使用了 PROPFIND)
- 使用WFetch来处理 WebDAV 请求
- 使用网络监视器来比较来自 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();
于 2009-04-04T16:48:54.027 回答
1

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

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

于 2009-04-04T12:47:26.697 回答