问题描述:

When I run the same code under windows and linux,I get different results! In other word, it works well in windows, while it failed in linux! Can anyone help me to solve this problem?

The web page is I want to fetch is "http://www.linkedin.com/pub/song-xuemeng/4b/683/565".

the code is listed as follows:

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"));

}

}

相关阅读:
Top