The issue is, im trying to access a Webservice on the server, and the server has NTLM Auth. Everything works fine in Postman, since i just specified a username and password and there are no issues getting the JSON i want. I have tried adding all the same headers to my android code, and i have checked multiple times that both the URL and the username/password are correct, i am not sure why im still getting a 400 Bad Request Error, im pretty lost, any help is appreciated.
package com.example.recumasapp.Conexión;
import okhttp3.*;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Call;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
import retrofit2.http.GET;
import java.io.IOException;
public class GetJson {
private static final String BASE_URL = "the url";
private static final String USERNAME = "username";
private static final String PASSWORD = "password";
private static final String DOMAIN = "";
private static final String AUTH_HEADER_PREFIX = "NTLM";
public interface ApiService {
@GET
Call<String> getData(@retrofit2.http.Url String url);
}
public String access(String lista) throws IOException {
String authHeaderValue = createNtlmHeader();
OkHttpClient.Builder httpClient = new OkHttpClient.Builder();
httpClient.addInterceptor(new NtlmAuthInterceptor(authHeaderValue));
httpClient.addInterceptor(new HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BODY));
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.client(httpClient.build())
.build();
ApiService apiService = retrofit.create(ApiService.class);
String url = BASE_URL + "Company('Recumas')" + lista;
Call<String> call = apiService.getData(url);
try {
retrofit2.Response<String> response = call.execute();
if (response.isSuccessful()) {
return response.body();
} else {
throw new IOException("Unexpected code " + response.code() + ": " + response.message());
}
} catch (IOException e) {
throw new IOException("Error executing API request: " + e.getMessage());
}
}
private String createNtlmHeader() {
String domainUser = (DOMAIN != null && !DOMAIN.isEmpty()) ? DOMAIN + "\\" + USERNAME : USERNAME;
String authString = domainUser + ":" + PASSWORD;
byte[] authBytes = authString.getBytes();
String encodedAuth = AUTH_HEADER_PREFIX + " " + android.util.Base64.encodeToString(authBytes, android.util.Base64.NO_WRAP);
return encodedAuth.trim();
}
private static class NtlmAuthInterceptor implements Interceptor {
private final String authHeaderValue;
public NtlmAuthInterceptor(String authHeaderValue) {
this.authHeaderValue = authHeaderValue;
}
@Override
public okhttp3.Response intercept(Chain chain) throws IOException {
Request authenticatedRequest = chain.request().newBuilder()
.header("Authorization", authHeaderValue)
.header("Accept", "*/*")
.header("Accept-Encoding", "gzip, deflate, br")
.header("Connection", "keep-alive")
.build();
return chain.proceed(authenticatedRequest);
}
}
}
I have tried using different libraries, including jcifs, okhttp and retrofit, but i still get the same 400 Bad Request Error, i used all the same headers as postman showed me, but same issue once again. I have also checked that the url and credentials are right, but i cannot think of any other solution nor what i might be doing wrong.