首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >UnknownHostException WebDav JackRabbit

UnknownHostException WebDav JackRabbit
EN

Stack Overflow用户
提问于 2015-01-20 18:56:49
回答 1查看 252关注 0票数 1

我正试图在安卓系统中实现一个WebDav客户端。为此,我使用了一个为安卓修改的JackRabbit版本,这里 (2.2.6版)。

我想连接到我的帐户在box.com和上传一个文件。老实说,我不介意盒子或其他东西,我只是碰巧用了这个。

那么,继续使用box.com,根据(这个链接)[https://support.box.com/hc/en-us/articles/200519748-Does-Box-support-WebDAV-],我应该使用"https://dav.box.com/dav“作为服务器。

我遵循这些链接来构建我的代码:

  • http://jackrabbit.apache.org/api/2.1/org/apache/jackrabbit/webdav/client/methods/package-summary.html
  • http://wiki.apache.org/jackrabbit/WebDAV

我得到了一个UnknownHostException告诉我我用于服务器的URL ("https://dav.box.com/dav")是找不到的。

你知道它为什么不起作用吗?否则,您是否尝试过其他服务器并成功了?

我的密码在这里:

代码语言:javascript
复制
Thread t = new Thread() {
        public void run(){
            try {
                String uri = "https://app.box.com/files";
                HostConfiguration hostConfig = new HostConfiguration();
                hostConfig.setHost(uri);

                HttpConnectionManager connectionManager = new MultiThreadedHttpConnectionManager();
                HttpConnectionManagerParams params = new HttpConnectionManagerParams();
                int maxHostConnections = 20;
                params.setMaxConnectionsPerHost(hostConfig, maxHostConnections);
                connectionManager.setParams(params);

                HttpClient client = new HttpClient(connectionManager);
                client.setHostConfiguration(hostConfig);
                Credentials creds = new UsernamePasswordCredentials("USER", "PASSWORD");
                client.getState().setCredentials(AuthScope.ANY, creds);

                String baseUrl = "/";
                File f = new File(Environment.getExternalStorageDirectory() + "/working-draft.txt");
                PutMethod method = new PutMethod(baseUrl + "/" + f.getName());
                RequestEntity requestEntity = new InputStreamRequestEntity(
                    new FileInputStream(f));
                method.setRequestEntity(requestEntity);
                client.executeMethod(method);
            }
            catch (FileNotFoundException fnfe){
                Log.i("SERVICE", "FileNotFoundException");
            }
            catch (HttpException he){
                Log.i("SERVICE", "HttpException");
            }
            catch (IOException ioe){
                Log.i("SERVICE", "IOException");
            }
            catch (Exception e){
                Log.i("SERVICE", "Other Exception");
            }
        }
    };
    t.start();
EN

回答 1

Stack Overflow用户

发布于 2015-08-14 10:56:23

我尝试了很长一段时间使用JackRabbit和另一个webDav库,但是无法让它工作。这就是我最终如何成功地将图像发送到windows服务器上托管在WebDav中的IIS7。希望这能帮上忙。

SendImage.java

代码语言:javascript
复制
import android.content.Context;
import android.os.AsyncTask;
import android.widget.Toast;

import org.apache.http.HttpResponse;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.NTCredentials;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.entity.FileEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.HttpConnectionParams;

import java.io.BufferedReader;
import java.io.File;
import java.io.InputStreamReader;

import ntlm.NTLMSchemeFactory;

//import org.apache.http.client.HttpClient;

public class SendImage extends AsyncTask<String, Context, String> {
    Context cxt;

    public SendImage(Context cxtIn){
        cxt = cxtIn;
    }

    @Override
    protected String doInBackground(String... params) {
        if (!Globals.sendImagesBeingPerformed) {

            Globals.sendImagesBeingPerformed = true;
            String filepath = cxt.getExternalFilesDir("/MyFileStorage/qrscans/").getAbsolutePath();
            File myExternalFile = new File(filepath.toString());
            File[] sdDirList = myExternalFile.listFiles();

            if(sdDirList != null && sdDirList.length>0){
                for(int x=0;x<sdDirList.length;x++){
                    if(sdDirList[x].toString().endsWith(".jpg")){

                        File myExternalFile2 = new File(cxt.getExternalFilesDir("/MyFileStorage/qrscans/"), sdDirList[x].getName());
                        //String URL="";
                        System.out.println("SENDING QR4");
                        if(myExternalFile2.exists()) {
                            System.out.println("ScannedExists");
                            DefaultHttpClient httpclient = new DefaultHttpClient();
                            httpclient.getAuthSchemes().register("ntlm", new NTLMSchemeFactory());

                            String url = Globals.getWebDavUrl().trim();
                            String u = Globals.getWebDavUser().trim();
                            String p = Globals.getWebDavPass().trim();
                            String d = Globals.getWebDavDomain().trim();
                            if(d!=null && !d.isEmpty() && (d.length()>0)){
                                //use value of d as domain
                            }else{
                                //use a space as domain
                                d = " ";
                            }

                            String device = Globals.deviceId;
                            httpclient.getCredentialsProvider().setCredentials(
                                    new AuthScope(null, -1),
                                    new NTCredentials(u, p, device, d));
                            HttpConnectionParams.setConnectionTimeout(httpclient.getParams(),5000);

                            if(url.endsWith("/") || url.endsWith("\\")){
                                url = url.substring(0, url.length()-1);
                            }

                            HttpPut put = new HttpPut(url.trim()+"/"+sdDirList[x].getName());
                            put.setEntity(new FileEntity(sdDirList[x],"application/octet-stream"));
                            HttpResponse response = null;

                            try {
                                response = httpclient.execute(put);
                            }catch(Exception e){
                                return "Error Sending Image:\n"+e.getMessage()+" " + e.getCause();
                            }
                            System.out.println("execute done");

                            BufferedReader in = null;
                            String webResponse="";
                            try {
                                in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
                                String stringLine="";
                                StringBuilder stringBuilder = new StringBuilder();
                                while ((stringLine = in.readLine()) != null) {
                                    //stringBuilder.append("\n");
                                    stringBuilder.append(stringLine);
                                }
                                webResponse=stringBuilder.toString()+"s";
                                if(webResponse.toString().trim().equalsIgnoreCase("s")){
                                    myExternalFile2.delete();
                                }

                                System.out.println("webResponse:" + webResponse);
                                return null; //webResponse;
                            }catch(Exception e){
                                return "Error Sending Image:\n"+e.getMessage()+" " + e.getCause();
                            }
                        }
                    }
                }
            }
        }
        return null;
    }

    @Override
    protected void onPostExecute(String result) {
        if(result!=null){
            Toast.makeText(cxt, result, Toast.LENGTH_LONG).show();
        }

        Globals.sendImagesBeingPerformed = false;


super.onPostExecute(result);
        }
    }

NTLMSchemeFactory.java

代码语言:javascript
复制
import org.apache.http.auth.AuthScheme;
import org.apache.http.auth.AuthSchemeFactory;
import org.apache.http.impl.auth.NTLMScheme;
import org.apache.http.params.HttpParams;

public class NTLMSchemeFactory implements AuthSchemeFactory {

    public AuthScheme newInstance(final HttpParams params) {
        return new NTLMScheme(new JCIFSEngine());
    }

}

JCIFSEngine.java

代码语言:javascript
复制
package ntlm;

import java.io.IOException;

import jcifs.ntlmssp.Type1Message;
import jcifs.ntlmssp.Type2Message;
import jcifs.ntlmssp.Type3Message;
import jcifs.util.Base64;

import org.apache.http.impl.auth.NTLMEngine;
import org.apache.http.impl.auth.NTLMEngineException;

public class JCIFSEngine implements NTLMEngine {

    public String generateType1Msg(
            String domain, 
            String workstation) throws NTLMEngineException {

        Type1Message t1m = new Type1Message(
                Type1Message.getDefaultFlags(),
                domain,
                workstation);
        return Base64.encode(t1m.toByteArray());
    }

    public String generateType3Msg(
            String username, 
            String password, 
            String domain,
            String workstation, 
            String challenge) throws NTLMEngineException {
        Type2Message t2m;
        try {
            t2m = new Type2Message(Base64.decode(challenge));
        } catch (IOException ex) {
            throw new NTLMEngineException("Invalid Type2 message", ex);
        }
        Type3Message t3m = new Type3Message(
                t2m, 
                password, 
                domain, 
                username, 
                workstation,0);
        return Base64.encode(t3m.toByteArray());
    }

}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/28053095

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档