URLから画像を読み込んで更新する必要があり、URLは.htaccessによって認証されています
この画像ファイルにアクセスする方法を知っている人はいますか
私の質問は:-そのようなファイルにアクセスするときに( HttpUrlConnection で) Credentials を設定する方法
URLから画像を読み込んで更新する必要があり、URLは.htaccessによって認証されています
この画像ファイルにアクセスする方法を知っている人はいますか
私の質問は:-そのようなファイルにアクセスするときに( HttpUrlConnection で) Credentials を設定する方法
Bitmap bmImg;
public Bitmap downloadFile(String stringURL)
{
try
{
DefaultHttpClient client = new DefaultHttpClient();
client.getCredentialsProvider().setCredentials(new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT), new UsernamePasswordCredentials(myHtaccessUsername, myHTaccessPassword));
HttpGet request = new HttpGet(stringURL);
HttpResponse response = client.execute(request);
HttpEntity entity = response.getEntity();
InputStream inputStream = entity.getContent();
bmImg = BitmapFactory.decodeStream(inputStream);
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
return bmImg;
}
これは、この問題を解決するために使用したコードです。HttpUrlConnection を使用して公開 URL から写真を取得していましたが、基本レベルのアクセス (htaccess 資格情報) を必要とする特定のサーバーになると、HttpUrlConnection がハンドシェイクなどを処理する方法が原因で、HttpUrlConnection を使用できませんでした。 . HttpGet、HttpResponse、および HttpEntity オブジェクトと一緒に DefaultHttpClient を使用すると、問題なく写真リソースを取得できました。
基本認証の場合は、オーセンティケーターを使用してください (注、これは GET 要求専用です)。
Authenticator.setDefault(new Authenticator(){
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("myuser","mypass".toCharArray());
}});
次に、その下に HttpURLConnection を作成します。