我有一项服务可以下载某个网站的HTML源代码,然后将其与之前下载的网站进行比较,以查看是否发生了任何更改。
我昨天遇到的问题:我公开连接到WiFi,我的服务开始下载代码,因为它没有显示没有连接。但是你首先必须在使用公共WiFi时登录,因此它被重定向到他们的登录页面,我的服务下载了登录页面的HTML代码。
我如何能够获得我想要的网站的源代码?
这是源代码(顺便说一句,我将源代码转换为md5以便于比较,但这并不重要):
public class DownloadHandler{
public String getMd5(String url){
HttpClient client = new DefaultHttpClient();
HttpGet request = new HttpGet(url);
HttpResponse response = null;
try {
response = client.execute(request);
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
InputStream in = null;
try {
in = response.getEntity().getContent();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
StringBuilder str = new StringBuilder();
String line = null;
try {
while ((line = reader.readLine()) != null) {
str.append(line);
}
} catch (IOException e) {
e.printStackTrace();
}
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
String HTML = str.toString();
try {
String md5 = stringToMd5(html);
return md5;
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
return null;
}
}
public String stringToMd5(String s) throws NoSuchAlgorithmException {
MessageDigest md5 = MessageDigest.getInstance("MD5");stringa
md5.update(s.getBytes(), 0, s.length());
String md5String = new BigInteger(1, md5.digest()).toString(16);
return md5String;
}
检查连接的功能:
boolean isNetworkAvailable() {
ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetworkInfo = connectivityManager
.getActiveNetworkInfo();
return activeNetworkInfo != null;
}
答案 0 :(得分:1)
您可以使用
禁用重定向HttpGet httpGet = new HttpGet("www.google.com");
HttpParams params = httpGet.getParams();
params.setParameter(ClientPNames.HANDLE_REDIRECTS, Boolean.FALSE);
httpGet.setParams(params);
但您仍应检查http状态代码,该代码应为200
HttpResponse httpResp = client.execute(response);
int code = httpResp.getStatusLine().getStatusCode();
答案 1 :(得分:0)
使用SSL访问网站并检查证书的身份。