I am trying to perform the OAuth 1.0a authentication flow using Java. OAuth 1.0a requires "All parameter names and values [to be] escaped using the RFC3985 percent-encoding mechanism... Text names and values MUST be encoded as UTF-8 octets." I would like to use the HttpsURLConnection class and use the setRequestProperty method to add the oauth parameters to the Authentication header. However, Java internally stores strings with UTF-16 encoding. I built a method that performs the percent encoding for me, so the string "Hello World!" is successfully changed to "Hello%20World%21". Even though it follows percent-encoding, it is still internally stored in UTF-16. If I pass this string into the setRequestProperty under an arbitrary key, this would be unreadable to a server accepting UTF-8 data. How do I add UTF-8 text to an HTTP header? The setRequestProperty method takes two Strings, so no matter what, I can only pass in data stored in UTF-16. I have also converted the text data to a byte array that is in UTF-8 format, but I cannot use an array as input to the setRequestProperty.
You are overthinking it. The way java stores strings internally is completely irrelevant to the matter and is transparent to you as a programmer. Just set your string as a header value and it will work. I wrote my own simplified Http client based on HttpsURLConnection
class and I used it it to set auth header for authentication as well as many other headers and it works just fine. Here is a Javadoc for my HttpClient and here is a GitHub page for my MgntUtils Library. There you can see the source code. Here is the HttpClient class source code. See the line 344.