2016-06-14 3 views
1

cUrl에서 Android로 이동하는 데는 몇 가지 질문이 있지만 찾은 파일 업로드는 다루지 않습니다.파일과 함께 CUrl을 Android HttpUrlConnection으로 변환하십시오.

이 컬 요청 작동 : 내가 노력하고

$ curl -X PUT -u user:password --data-binary @myfile.pdf "https://example.com/myurl?id=myid&filename=myfile.pdf&title=My+Title&mimetype=application/pdf" 

:

String url = "https://example.com/myurl?id=myid&filename=myfile.pdf&title=My+Title&mimetype=application/pdf"; 
String fileUrl = "myfile.pdf"; 
String userPassword = "user:password"; 
String userpass = Base64.encodeToString(userPassword.getBytes(), Base64.DEFAULT); 

File f = new File(fileUrl); 
    if(f.isFile()) { 
     HttpURLConnection c = null; 
     URL u = null; 
     try { 
      u = new URL(url); 
       c = (HttpURLConnection) u.openConnection(); 
      c.setRequestProperty("Authorization", "Basic " + userpass); 
      c.setDoOutput(true); 
      c.setRequestMethod("PUT"); 
      c.setRequestProperty("Content-Type", "multipart/form-data"); 

      FileInputStream fis = new FileInputStream(f); 
      long fl = f.length(); 
      output(fis, c.getOutputStream(), fl); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
output(fis, c.getOutputStream(), fl);가 inputStream을이 OutputStream에 기록

다음/모두를 플러시 닫습니다.

나는 method not allowed의 응답을 받고 있는데, 누구나 내가 뭘 잘못하고 있는지 알아?

편집 : 그것은 (나에게) 정말 이상한 그들의 응답 헤더를 가지고 : Allow: POST,OPTIONS,GET,HEAD하지만 PUT 컬 작품을 사용.

편집 두 :

컬을 사용하는 경우 실제로 얻을 :

Access-Control-Allow-Methods: POST, GET, OPTIONS, PUT, DELETE 

을하지만 컬 사용자 에이전트 사용자 에이전트를 설정 한 경우에도, HttpURLConnection의를 사용하는 경우, 더 Access-Control-Allow-Methods 없다 c.getHeaderField()을 사용하십시오. 대신 필드는 단순히 Allow이며 PUT이 없습니다.

답변

0

좋아, 이것은 바보 같았다. 위의 코드에는 아무런 문제가 없지만 API에서는 PUT을 허용하도록 구성되지 않은 .net 도메인을 사용하는 것으로 나타났습니다. dev는 .com 도메인을 사용하는 컬 (curl) 예제를 보냈습니다.

하나의 문제는 mySecretWeirdProtocolHandler(), NastyBugNobodyCaresAbout.class 및 과 같은 것으로 가득 찼습니다. 그래서 나는 스트레스가 많은 즐거운 시간 동안, 그것이 나인 지 알지 못하는 모든 것을 즉시 보았습니다.