2012-06-26 4 views
3

여러분 중 일부는 저의 "두뇌 교착 상태"를 도울 수 있기를 바랍니다.Android, HttpsUrlConnection : POST 매개 변수 및 InputStream 수신 비밀번호

(이해를 돕기 위해) PHP 스크립트를 사용하여 데이터베이스를 받으려고합니다. 따라서 유효한 사용자 이름/pwd-combo를 사용하고 올바른 매개 변수를 보내는 경우에만 php-script에 대한 액세스 권한을 부여하는 webadress/server를 호출해야합니다.

HttpClient 및 HttpPost를 사용할 때 내 코드가 정상적으로 작동합니다. (좋은 코드 아래)

InputStream myInputDB = null; 
    OutputStream myOutputDB = null; 
    HttpResponse response = null; 
    // Path to the just created empty db 
    String dbFilePath = DB_PATH + KeyConstants.DB_NAME; 

    try { 
     // Creating HTTP client 
     HttpClient httpClient = new DefaultHttpClient(); 
     // Creating HTTP Post 
     List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2); 
     nameValuePairs.add(new BasicNameValuePair("version", "20110516140000")); 
     HttpPost httppost = new HttpPost("http://USERNAME:[email protected]/u/db2.php"); 
     httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 
     response = httpClient.execute(httppost); 
     //Open your local db as the input stream 
     myInputDB = response.getEntity().getContent(); 
     //Open the empty db as the output stream 
     myOutputDB = new FileOutputStream(dbFilePath); 
     //transfer bytes from the inputfile to the outputfile 
      byte[] buffer = new byte[1024]; 
     int length; 
     while ((length = myInputDB.read(buffer)) > 0) { 
      myOutputDB.write(buffer, 0, length); 
     } 
     //Close the streams 
     myOutputDB.flush(); 
     myOutputDB.close(); 
     myInputDB.close(); 

위의 솔루션 대신 HttpURLConnection/HttpsURLConnection을 사용하려고합니다. 왜 분명해야하는지, 라이브 시스템은 HTTPS (서버 인증)를 사용합니다. 지금까지

나는 방법 ...

  1. POST 매개 변수 HttpURLConnection의와
  2. 하여 인증 서버에
  3. 일반적으로 인증 검사 (테스트!)
  4. 을 알아 내려고 노력의 일 낭비

나는 어디에서 실수를하고 있는지 알지 못합니다. 지금까지 내 코드 ... 아래

String urlString = "http://USERNAME:[email protected]/u/db2.php"; 
    String params = "version=20110516140000"; 
    InputStream stream = null; 
    url = new URL(urlString); 
    if (url.getProtocol().toLowerCase().equals("https")) { 
     trustAllHosts(); 

     HttpsURLConnection urlconn = (HttpsURLConnection) url.openConnection(); 
     urlconn.setHostnameVerifier(DO_NOT_VERIFY); 
     urlconn.setDoInput(true); 
     urlconn.setDoOutput(true); 
     urlconn.setRequestMethod("POST"); 
     urlconn.setRequestProperty("Content-Type", "text/xml"); 
     urlconn.setRequestProperty("Content-Length", String.valueOf(params.getBytes().length)); 

     OutputStreamWriter wr = new OutputStreamWriter(urlconn.getOutputStream()); 
     wr.write(params); 
     wr.flush(); 

     stream = urlconn.getInputStream(); 
     if ("gzip".equals(urlconn.getContentEncoding())) { 
      stream = new GZIPInputStream(stream); 
     } 

     wr.close(); 

    } else {    
     // Almost the same as if-path, but without trustAllHosts() and with HttpURLConnection instead of HttpsURLConnection   
    } 

    return stream; 

은 내가 필요한 것은 그때 (작업 코드와 같이) 안드로이드 전화에 복사/저장할 수의 InputStream이다. 지금까지 내가 얻은 결과는 IOException입니다. urlconn.getInputStream()이 호출되는 즉시 작업이 중단됩니다. 그러나 getOutputStream()에도 유효한 값이 없습니다.

지금 질문드립니다. 누구든지 내 코드를 검토하고 올바른 방법으로 POST 매개 변수를 알려주시겠습니까? 그 외에도 URL을 통해 인증을 보낼 수 있는지 알고 있습니까? 특히 HTTP 'S'부분은 저에게 흥미 롭습니다. 비보안 모드의 경우 이미 실행중인 솔루션이 있습니다.

도움 주셔서 감사합니다.

+0

내가 지금까지 사용해 온 것 ... http://stackoverflow.com/questions/2793150/how-to-use-java-net-urlconnection-to-fire-and-handle-http-requests http://stackoverflow.com/questions/5859667/how-to-send-requestparameter-in-post-request-using-httpurlconnection – njsmecho

+0

해당 링크 ... http://stackoverflow.com/questions/995514/https -connection-android http://home-1.worldonline.nl/~bmc88/java/sbook/045.html http://www.xyzws.com/Javafaq/how-to-use-httpurlconnection-post- 데이터 - 웹 서버/139 http://digitallibraryworld.com/?p=189 – njsmecho

답변

0

브라우저를 사용하여 수동으로 HTTPS 연결을 수행 할 수있는 경우. Firefox를 사용하고 있다면 브라우저에서 보낸 요청 (예 : Firebug Firefox 확장 프로그램 사용)을 살펴보고 필요한 헤더를 복사하여 코드에 넣으십시오.

아이디어는 표준 도구 (이 경우 브라우저)를 사용하여 먼저 로그인하고 성공시 보내고받는 것을 봅니다.