Kullanarak benim istek için başlık eklemek çalışıyorum HttpUrlConnectionama yöntem setRequestProperty()çalışmıyor gibi görünüyor. Sunucu tarafı üstbilgimle herhangi bir istek almıyor.
HttpURLConnection hc;
try {
String authorization = "";
URL address = new URL(url);
hc = (HttpURLConnection) address.openConnection();
hc.setDoOutput(true);
hc.setDoInput(true);
hc.setUseCaches(false);
if (username != null && password != null) {
authorization = username + ":" + password;
}
if (authorization != null) {
byte[] encodedBytes;
encodedBytes = Base64.encode(authorization.getBytes(), 0);
authorization = "Basic " + encodedBytes;
hc.setRequestProperty("Authorization", authorization);
}
connect()URLConnection'ı nereye çağırıyorsunuz ?
connection.setRequestMethod("GET");(veya POST veya istediğinizi)?
authorizationBoş dizeye başlangıç değeri . Ya usernameda passwordnull olursa , null authorizationdeğil boş dize olur. Bu nedenle, final ifidam edilecek, ancak "Authorization"mülkiyet boş olarak ayarlanacak, bana göre.