当我在windows和linux下运行相同的代码时,我会得到不同的结果!换句话说,它在windows中运行良好,而在linux中却失败了!任何人都可以帮我解决这个问题吗?
我想要提取的网页是“http://www.linkedin.com/pub/song-xuemeng/4b/683/565”。
代码如下:
import java.io.BufferedReader;
import java.io.InputStreamReader;
import org.apache.commons.httpclient.DefaultHttpMethodRetryHandler;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.cookie.CookiePolicy;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.params.HttpMethodParams;
public class api {
public String fetchAPI(String url) {
GetMethod getMethod;
HttpClient client = new HttpClient();
int statusCode;
getMethod = new GetMethod(url);
getMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
new DefaultHttpMethodRetryHandler());
getMethod.getParams().setParameter("http.protocol.cookie-policy",
CookiePolicy.BROWSER_COMPATIBILITY);
getMethod.getParams().setParameter(HttpMethodParams.SO_TIMEOUT, 5000);
BufferedReader reader = null;
String result = "";
String line = "";
try {
statusCode = client.executeMethod(getMethod);
if (statusCode != HttpStatus.SC_OK) {
System.err.println("Method failed: "
+ getMethod.getStatusLine());
}
reader = new BufferedReader(new InputStreamReader(
getMethod.getResponseBodyAsStream(), "utf-8"));
if (reader != null) {
while ((line = reader.readLine()) != null) {
// System.out.println(line);
result += line;
}
reader.close();
getMethod.releaseConnection();
return result;
} else{
reader.close();
getMethod.releaseConnection();
return "";
}
} catch (Exception e) {
getMethod.releaseConnection();
System.out.println("opsss");
return "";
}
}
public static void main(String[] arg){
System.out.println(fetchAPI("http://www.linkedin.com/pub/song-xuemeng/4b/683/565"));
}
}