2015-01-04 4 views
3

HTTP 기본 인증 정보를 Intent.ACTION_VIEW으로 전달하는 방법은 무엇입니까? 여기에 내가 의도를 발사하고있어 어디 : 나는 또한, 같은 거래를 Uri.fromParts()을 시도Android 인 텐트 .ACTION_VIEW 기본 인증

public class OutageListFragment extends ListFragment implements LoaderManager.LoaderCallbacks<Cursor> { 

    // ... 

    @Override 
    public void onListItemClick(ListView listView, View view, int position, long id) { 
     super.onListItemClick(listView, view, position, id); 

     // Get a URI for the selected item, then start an Activity that displays the URI. Any 
     // Activity that filters for ACTION_VIEW and a URI can accept this. In most cases, this will 
     // be a browser. 
     String outageUrlString = "http://demo:[email protected]/opennms/outage/detail.htm?id=204042"; 
     Log.i(TAG, "Opening URL: " + outageUrlString); 
     // Get a Uri object for the URL string 
     Uri outageURI = Uri.parse(outageUrlString); 
     Intent i = new Intent(Intent.ACTION_VIEW, outageURI); 
     startActivity(i) 
    } 

} 

. 컬은 잘 작동합니다.

+0

의도적 인 URI를받는쪽에 달려 있습니다. 당신의 로그는 당신이 원하는 것을 인쇄합니까? 이 다음 브라우저에 갈 것인가? – Blundell

+0

@Blundell Log는 내가 원하는 것을 인쇄합니다. outageURI.getUserInfo()는 사용자 ID와 암호를 표시합니다. 안드로이드 브라우저는 curl이하는 "Authorization : Basic ZGVtbzpkZW1v \ r \ n"을 보내지 않는 것 같습니다. – Wrolf

+0

하지만 cURL 명령에 헤더로 추가 하시겠습니까? Uri – Blundell

답변

4

번들을 통해 인 텐트에 HTTP 헤더를 추가 할 수 있으며, 특히 Authorization 헤더에 Base64로 인코딩 된 사용자 ID를 추가 할 수 있습니다.

Intent i = new Intent(Intent.ACTION_VIEW, outageURI); 

    String authorization = user + ":" + password; 
    String authorizationBase64 = Base64.encodeToString(authorization.getBytes(), 0); 

    Bundle bundle = new Bundle(); 
    bundle.putString("Authorization", "Basic " + authorizationBase64); 
    i.putExtra(Browser.EXTRA_HEADERS, bundle); 
    Log.d(TAG, "intent:" + i.toString()); 

    startActivity(i); 
+0

안녕하세요, 저는 이것을 사용했지만'Authorization' 헤더가 서버에 도착하지 않습니다. 도와 드릴까요? http://stackoverflow.com/questions/42244271/showing -remote-pdf-via-intent-with-basic-auth –