Recently I am working on a request with HTTPPost, and I need to set header with Content-Type: application/json, and it requires basic authentication, Here is may way:
A:
httpPost.setHeader("Content-type","application/json");
String authenData = String.format(Locale.getDefault(), "%s:%s", "xxx@xxx.com", "xxx");
String base64EncodedCredentials = Base64.encodeToString(authenData.getBytes(), Base64.DEFAULT);
httpPost.addHeader("Authentication", "Basic " + base64EncodedCredentials);
B:
httpPost.setHeader("Content-type","application/json");
httpPost.addHeader(BasicScheme.authenticate(new UsernamePasswordCredentials("xxx@xxxx.com", "xxxx"), "UTF-8", false));
And, Guess what? With method A, the request header will be only Authentication left, yes, "Content-type" is gone
Only B works perfectly.
Has any one meet similar issue before? Please please help me out, thanks :)