0

I have these variables:

private String name = "name";
private String password = "password";

private String authString = name + ":" + password;
private byte[] authEncBytes = Base64.encodeBase64(authString.getBytes());
private String authStringEnc = new String(authEncBytes);

And in my Java program I have the below code:

HttpURLConnection.setRequestProperty("Authorization", "Basic " + authStringEnc);

This gives me the following error:

Cannot make a static reference to the non-static field authStringEnc

If I then change my variables to static I get the below error:

Cannot make a static reference to the non-static method setRequestProperty(String, String) from the type URLConnection

How do I resolve this?

runnerpaul
  • 5,942
  • 8
  • 49
  • 118
  • The name and password? I made all static: private static String name = "name"; private static String password = "password"; private static String authString = name + ":" + password; private static byte[] authEncBytes = Base64.encodeBase64(authString.getBytes()); private static String authStringEnc = new String(authEncBytes); – runnerpaul May 07 '17 at 13:37
  • Apologies for the dupe. Think it's resolved now with : HttpURLConnection con = (HttpURLConnection) obj.openConnection(); con.setRequestProperty("Authorization", "Basic " + authStringEnc); – runnerpaul May 07 '17 at 13:45

0 Answers0