Android:没有从具有基本身份验证的 http get 请求中获取 xml
Android: not getting xml out of http get request with basic authentication
我的目标是从 API. The API uri I use, including parameters is http://webservices.ns.nl/ns-api-treinplanner?fromStation=Roosendaal&toStation=Eindhoven 获得 xml。我得到了用户名和密码,我认为这可能是基本授权。
我尝试了各种东西,比如带有身份验证器的东西,格式 http://username:password@webservices.ns.nl/ns-api-treinplanner,但在大量 SO 搜索结束时,我最终得到了带有 setRequestProperty
和基本授权的东西。
我将代码放入似乎可以正常工作的 AsyncTask 中,所以我将把 doInBackground
中的代码放在这里。
由于我第一次得到的javaFileNotFoundException
没有给我太多信息,所以我找到了如何使用getErrorStream
来了解更多。
InputStream in;
int resCode;
try {
URL url = new URL("http://webservices.ns.nl/ns-api-treinplanner?fromStation=Roosendaal&toStation=Eindhoven");
String userCredentials = "username:password";
String encoding = new String(android.util.Base64.encode(userCredentials.getBytes(), Base64.DEFAULT));
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestProperty("Authorization", "Basic " + encoding);
urlConnection.setRequestMethod("GET");
urlConnection.setDoOutput(true);
try {
resCode = urlConnection.getResponseCode();
if (resCode == HttpURLConnection.HTTP_OK) {
Log.i("rescode","ok");
in = urlConnection.getInputStream();
} else {
Log.i("rescode","not ok");
in = urlConnection.getErrorStream();
}
BufferedReader bufferedReader = new BufferedReader(
new InputStreamReader(in));
StringBuilder stringBuilder = new StringBuilder();
String line;
while ((line = bufferedReader.readLine()) != null) {
stringBuilder.append(line).append("\n");
}
bufferedReader.close();
return stringBuilder.toString();
}
finally{
urlConnection.disconnect();
}
}
catch(Exception e) {
Log.e("ERROR", e.getMessage(), e);
return null;
}
然后,在 onPostExecute
我打印了响应,但我得到的响应是
<?xml version="1.0"?>
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" soap:encodingStyle="http://www.w3.org/2003/05/soap-encoding">
<soap:Header></soap:Header>
<soap:Body><soap:Fault>
<faultcode>soap:Server</faultcode>
<faultstring>006:No customer found for the specified username and password</faultstring></soap:Fault>
</soap:Body></soap:Envelope>
这当然是不对的,它应该给出一个完整的xml在这种情况下的火车旅行建议。
我用我的浏览器进行了测试,还使用了一个名为 Postman 的 HTTP 请求工具,它返回了正确的 xml 所以所有的 uri、参数、用户名和密码都是正确的。
使用的编码错误。 base64编码中间随机使用了returns个空格,加上encoding = encoding.replaceAll("\s+","");
居然修复了
我的目标是从 API. The API uri I use, including parameters is http://webservices.ns.nl/ns-api-treinplanner?fromStation=Roosendaal&toStation=Eindhoven 获得 xml。我得到了用户名和密码,我认为这可能是基本授权。
我尝试了各种东西,比如带有身份验证器的东西,格式 http://username:password@webservices.ns.nl/ns-api-treinplanner,但在大量 SO 搜索结束时,我最终得到了带有 setRequestProperty
和基本授权的东西。
我将代码放入似乎可以正常工作的 AsyncTask 中,所以我将把 doInBackground
中的代码放在这里。
由于我第一次得到的javaFileNotFoundException
没有给我太多信息,所以我找到了如何使用getErrorStream
来了解更多。
InputStream in;
int resCode;
try {
URL url = new URL("http://webservices.ns.nl/ns-api-treinplanner?fromStation=Roosendaal&toStation=Eindhoven");
String userCredentials = "username:password";
String encoding = new String(android.util.Base64.encode(userCredentials.getBytes(), Base64.DEFAULT));
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestProperty("Authorization", "Basic " + encoding);
urlConnection.setRequestMethod("GET");
urlConnection.setDoOutput(true);
try {
resCode = urlConnection.getResponseCode();
if (resCode == HttpURLConnection.HTTP_OK) {
Log.i("rescode","ok");
in = urlConnection.getInputStream();
} else {
Log.i("rescode","not ok");
in = urlConnection.getErrorStream();
}
BufferedReader bufferedReader = new BufferedReader(
new InputStreamReader(in));
StringBuilder stringBuilder = new StringBuilder();
String line;
while ((line = bufferedReader.readLine()) != null) {
stringBuilder.append(line).append("\n");
}
bufferedReader.close();
return stringBuilder.toString();
}
finally{
urlConnection.disconnect();
}
}
catch(Exception e) {
Log.e("ERROR", e.getMessage(), e);
return null;
}
然后,在 onPostExecute
我打印了响应,但我得到的响应是
<?xml version="1.0"?>
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" soap:encodingStyle="http://www.w3.org/2003/05/soap-encoding">
<soap:Header></soap:Header>
<soap:Body><soap:Fault>
<faultcode>soap:Server</faultcode>
<faultstring>006:No customer found for the specified username and password</faultstring></soap:Fault>
</soap:Body></soap:Envelope>
这当然是不对的,它应该给出一个完整的xml在这种情况下的火车旅行建议。
我用我的浏览器进行了测试,还使用了一个名为 Postman 的 HTTP 请求工具,它返回了正确的 xml 所以所有的 uri、参数、用户名和密码都是正确的。
使用的编码错误。 base64编码中间随机使用了returns个空格,加上encoding = encoding.replaceAll("\s+","");
居然修复了