当我尝试点击推送网址时,我收到401错误。我使用HTTP BASIC身份验证,“Application Key”作为用户名,“Application Master Secret”作为密码。我正在使用JAVA HttpsUrlConnection类。我不知道我的代码有什么问题。
` URL url = new URL("https://go.urbanairship.com/api/push");
HttpsURLConnection connection = (HttpsURLConnection)url.openConnection();
connection.setRequestMethod("POST");
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setUseCaches(false);
connection.setRequestProperty("Content-Type","application/json");
connection.setRequestProperty("Content-Length", Integer.toString(data.length()));
String authString = "xxxxxxxxxxxxxxxxxx:yyyyyyyyyyyyyyyyy";
authString = Base64Coder.encodeString(authString);
connection.setRequestProperty("Authorization","Basic "+ authString);
OutputStreamWriter wr = new OutputStreamWriter(connection.getOutputStream());
wr.write(data);
wr.flush();
int responseCode = connection.getResponseCode();
//Get the response
String responseLine = new BufferedReader(new InputStreamReader(connection.getInputStream())).readLine();`
答案 0 :(得分:2)
您的authString应由<application-key>:<application-master-secret>
组成。您的authstring也可能无法正确编码。尝试使用Apache Commons Codec或ostermiller库对authstring进行编码