Android:使用HttpsUrlConnection和令牌的curl请求

时间:2017-01-27 13:02:31

标签: android curl

我正在研究android api 23,我需要执行以下curl请求:

curl -u token: -T test.txt "https://own.cloud.server/public.php/webdav/test.txt"

其中token是共享链接中的标记。我在网上搜索,但我大多发现过时的答案,而不是使用HttpsUrlConnection。基本上,我需要做的是使用HttpsUrlConnection执行curl请求,并将令牌作为用户名和密码为空。请注意,test.txt是一个文件。 这是我尝试的代码,但它抛出了一个NetworkOnMainThreadException

try {
    URL url = new URL("https://drive.switch.ch/public.php/webdav/test.txt");
    HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();
    conn.setRequestProperty("Authorization", "Token token=" + getString(R.string.token));
    conn.setRequestMethod("POST");
    conn.setRequestProperty( "Content-type", "application/x-www-form-urlencoded");
    conn.setRequestProperty( "Accept", "*/*" );
    int httpStatus = conn.getResponseCode();

    if (httpStatus == 200) {
        Log.d("HTTP", "AAAAA");
    }
} catch (MalformedURLException me) {

} catch (IOException ioe) {

谢谢

0 个答案:

没有答案