读取在线网络源 HTML 的奇怪行为
Strange behaviour reading the source HTML of an online web
我有一个问题,我想,这是因为我用来阅读网页的对象,在这种情况下,Retrofit2
和 HttpURLConnection
。
情况是:我需要阅读没有 API(不是我的)的网页并提取整个 HTML 页面,但我尝试使用这两种工具时都遇到了问题(前面提到的)因为网络格式。
网络本身有这个元标记:
<meta http-equiv="content-type" content="text/html; charset=ISO-8859-1">
所以它显示了带有它们的单词的重音标记(它是西班牙语)。您可以在网络上清楚地看到 Chrome、Mozilla 或任何其他浏览器很好地解释了重音符号:
您还可以在 HTML 文件中看到重音符号:
但这是当问题刺痛我的时候:
执行:
原始:
现在,我将向您展示我到目前为止所做的尝试。
第一个电话是 Retrofit2
。
客户端(没有任何转换器,因为我想要原始的(听起来很糟糕,顺便说一句)):
public static Retrofit getRaiaApi() {
if (raiaRetrofit == null) {
raiaRetrofit = new Retrofit.Builder()
.baseUrl(RAIA_URL)
.build();
}
return raiaRetrofit;
}
POST方法:
@Headers({
"Content-Type: application/x-www-form-urlencoded;charset=utf-8"
})
@FormUrlEncoded
@POST("index.php?operacion=consulta")
Call<ResponseBody> postRaiaSearch(@Header("Cookie") String cookie, @Field("microchip") String microchip);
和调用:
private void nextRaiaSearch(String sessionCookie) {
callRaiaSearch = apiInterfaceRaia.postRaiaSearch(sessionCookie, chipInput);
callRaiaSearch.enqueue(new Callback<ResponseBody>() {
@Override
public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
Log.v("call", "onResponse");
try {
String html = response.body().string();
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void onFailure(Call<ResponseBody> call, Throwable t) {
Log.v("call", "onFailure");
}
});
}
但是正如我之前解释的那样,这给了我 HTML 和那些错误。
然后,我想:"Well, maybe Retrofit is converting something and this is not really the raw source of the web, so let's try something else"。
并尝试使用简单的 HttpURLConnection
。
private void nextRaiaSearch(String sessionCookie) throws IOException {
URL url = new URL("https://www.raia.es/index.php?operacion=consulta");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
OutputStreamWriter request;
StringBuilder response = new StringBuilder();
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
connection.setRequestProperty("Cookie", sessionCookie);
connection.setRequestMethod("POST");
connection.setConnectTimeout(60000);
connection.setReadTimeout(10000);
request = new OutputStreamWriter(connection.getOutputStream());
request.write("microchip=" + chipInput);
request.flush();
request.close();
String line;
InputStreamReader input = new InputStreamReader(connection.getInputStream());
BufferedReader reader = new BufferedReader(input);
while ((line = reader.readLine()) != null) {
response.append(line).append("\n");
}
input.close();
reader.close();
String html = response.toString();
}
但是,结果完全一样:
我是不是漏掉了什么?我应该使用其他工具吗?
你试过输出字符串吗?
类似于
String html = new String(response.toString().getBytes(), "UTF-8");
您可以使用InputStreamReader
指定服务器提供的编码。
例如:
InputStreamReader input = new InputStreamReader(connection.getInputStream(), Charset.forName("ISO-8859-1"));
希望有用
我有一个问题,我想,这是因为我用来阅读网页的对象,在这种情况下,Retrofit2
和 HttpURLConnection
。
情况是:我需要阅读没有 API(不是我的)的网页并提取整个 HTML 页面,但我尝试使用这两种工具时都遇到了问题(前面提到的)因为网络格式。
网络本身有这个元标记:
<meta http-equiv="content-type" content="text/html; charset=ISO-8859-1">
所以它显示了带有它们的单词的重音标记(它是西班牙语)。您可以在网络上清楚地看到 Chrome、Mozilla 或任何其他浏览器很好地解释了重音符号:
您还可以在 HTML 文件中看到重音符号:
但这是当问题刺痛我的时候:
执行:
原始:
现在,我将向您展示我到目前为止所做的尝试。
第一个电话是 Retrofit2
。
客户端(没有任何转换器,因为我想要原始的(听起来很糟糕,顺便说一句)):
public static Retrofit getRaiaApi() {
if (raiaRetrofit == null) {
raiaRetrofit = new Retrofit.Builder()
.baseUrl(RAIA_URL)
.build();
}
return raiaRetrofit;
}
POST方法:
@Headers({
"Content-Type: application/x-www-form-urlencoded;charset=utf-8"
})
@FormUrlEncoded
@POST("index.php?operacion=consulta")
Call<ResponseBody> postRaiaSearch(@Header("Cookie") String cookie, @Field("microchip") String microchip);
和调用:
private void nextRaiaSearch(String sessionCookie) {
callRaiaSearch = apiInterfaceRaia.postRaiaSearch(sessionCookie, chipInput);
callRaiaSearch.enqueue(new Callback<ResponseBody>() {
@Override
public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
Log.v("call", "onResponse");
try {
String html = response.body().string();
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void onFailure(Call<ResponseBody> call, Throwable t) {
Log.v("call", "onFailure");
}
});
}
但是正如我之前解释的那样,这给了我 HTML 和那些错误。
然后,我想:"Well, maybe Retrofit is converting something and this is not really the raw source of the web, so let's try something else"。
并尝试使用简单的 HttpURLConnection
。
private void nextRaiaSearch(String sessionCookie) throws IOException {
URL url = new URL("https://www.raia.es/index.php?operacion=consulta");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
OutputStreamWriter request;
StringBuilder response = new StringBuilder();
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
connection.setRequestProperty("Cookie", sessionCookie);
connection.setRequestMethod("POST");
connection.setConnectTimeout(60000);
connection.setReadTimeout(10000);
request = new OutputStreamWriter(connection.getOutputStream());
request.write("microchip=" + chipInput);
request.flush();
request.close();
String line;
InputStreamReader input = new InputStreamReader(connection.getInputStream());
BufferedReader reader = new BufferedReader(input);
while ((line = reader.readLine()) != null) {
response.append(line).append("\n");
}
input.close();
reader.close();
String html = response.toString();
}
但是,结果完全一样:
我是不是漏掉了什么?我应该使用其他工具吗?
你试过输出字符串吗?
类似于
String html = new String(response.toString().getBytes(), "UTF-8");
您可以使用InputStreamReader
指定服务器提供的编码。
例如:
InputStreamReader input = new InputStreamReader(connection.getInputStream(), Charset.forName("ISO-8859-1"));
希望有用